CloseWorkspaceAction.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using PaintDotNet.Base.CommTool;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.Actions
  7. {
  8. internal sealed class CloseWorkspaceAction : AppWorkspaceAction
  9. {
  10. private DocumentWorkspace closeMe;
  11. private bool cancelled;
  12. public bool Cancelled
  13. {
  14. get
  15. {
  16. return this.cancelled;
  17. }
  18. }
  19. public override void PerformAction(AppWorkspace appWorkspace)
  20. {
  21. if (appWorkspace == null)
  22. {
  23. throw new ArgumentNullException("appWorkspace");
  24. }
  25. appWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  26. DocumentWorkspace dw;
  27. if (this.closeMe == null)
  28. {
  29. if (appWorkspace.toRemoveDocumentWorkspaceIndex >= 0 && appWorkspace.toRemoveDocumentWorkspaceIndex < appWorkspace.DocumentWorkspaces.Length)
  30. {
  31. dw = appWorkspace.DocumentWorkspaces[appWorkspace.toRemoveDocumentWorkspaceIndex];
  32. appWorkspace.toRemoveDocumentWorkspaceIndex = -1;
  33. }
  34. else
  35. dw = appWorkspace.ActiveDocumentWorkspace;
  36. }
  37. else
  38. {
  39. dw = this.closeMe;
  40. }
  41. if (dw != null)
  42. {
  43. if (dw.Document == null)
  44. {
  45. appWorkspace.RemoveDocumentWorkspace(dw);
  46. }
  47. else if (!dw.Document.Dirty)
  48. {
  49. appWorkspace.RemoveDocumentWorkspace(dw);
  50. }
  51. else
  52. {
  53. appWorkspace.ActiveDocumentWorkspace = dw;
  54. TaskButton saveTB = new TaskButton(
  55. PdnResources.GetImageResource("Icons.MenuFileSaveIcon.png").Reference,
  56. PdnResources.GetString("CloseWorkspaceAction.SaveButton.ActionText") + "( Y )",
  57. PdnResources.GetString("CloseWorkspaceAction.SaveButton.ExplanationText"));
  58. TaskButton dontSaveTB = new TaskButton(
  59. PdnResources.GetImageResource("Icons.MenuFileCloseIcon.png").Reference,
  60. PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ActionText") + "( N )",
  61. PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ExplanationText"));
  62. TaskButton cancelTB = new TaskButton(
  63. PdnResources.GetImageResource("Icons.CancelIcon.png").Reference,
  64. PdnResources.GetString("CloseWorkspaceAction.CancelButton.ActionText") + "( ESC )",
  65. PdnResources.GetString("CloseWorkspaceAction.CancelButton.ExplanationText"));
  66. string title = PdnResources.GetString("CloseWorkspaceAction.Title");
  67. string introTextFormat = PdnResources.GetString("CloseWorkspaceAction.IntroText.Format");
  68. string introText = string.Format(introTextFormat, dw.GetFriendlyName());
  69. /**
  70. Image thumb = appWorkspace.GetDocumentWorkspaceThumbnail(dw);
  71. if (thumb == null)
  72. {
  73. thumb = new Bitmap(32, 32);
  74. }
  75. Bitmap taskImage = new Bitmap(thumb.Width + 2, thumb.Height + 2, PixelFormat.Format32bppArgb);
  76. using (Graphics g = Graphics.FromImage(taskImage))
  77. {
  78. g.Clear(Color.Transparent);
  79. g.DrawImage(
  80. thumb,
  81. new Rectangle(1, 1, thumb.Width, thumb.Height),
  82. new Rectangle(0, 0, thumb.Width, thumb.Height),
  83. GraphicsUnit.Pixel);
  84. Utility.DrawDropShadow1px(g, new Rectangle(0, 0, taskImage.Width, taskImage.Height));
  85. }**/
  86. Image taskImage = DrawRulerHelper.ResizeImage(appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap(), 115, 86, Base.Enum.ThumbnailMode.Cut);
  87. Form mainForm = appWorkspace.FindForm();
  88. if (mainForm != null)
  89. {
  90. PdnBaseForm asPDF = mainForm as PdnBaseForm;
  91. if (asPDF != null)
  92. {
  93. asPDF.RestoreWindow();
  94. }
  95. }
  96. Icon warningIcon;
  97. ImageResource warningIconImageRes = PdnResources.GetImageResource("Icons.WarningIcon.png");
  98. if (warningIconImageRes != null)
  99. {
  100. Image warningIconImage = warningIconImageRes.Reference;
  101. warningIcon = Utility.ImageToIcon(warningIconImage, false);
  102. }
  103. else
  104. {
  105. warningIcon = null;
  106. }
  107. TaskButton clickedTB = TaskDialog.Show(
  108. appWorkspace,
  109. warningIcon,
  110. title,
  111. taskImage,
  112. false,
  113. introText,
  114. new TaskButton[] { saveTB, dontSaveTB, cancelTB },
  115. saveTB,
  116. cancelTB,
  117. 340,
  118. false,
  119. 0,
  120. out bool unuse);
  121. if (clickedTB == saveTB)
  122. {
  123. if (dw.DoSaveNew())
  124. {
  125. this.cancelled = false;
  126. if(!dw.IsDisposed)
  127. appWorkspace.RemoveDocumentWorkspace(dw);
  128. }
  129. else
  130. {
  131. this.cancelled = true;
  132. }
  133. }
  134. else if (clickedTB == dontSaveTB)
  135. {
  136. this.cancelled = false;
  137. if (!dw.IsDisposed)
  138. appWorkspace.RemoveDocumentWorkspace(dw);
  139. }
  140. else
  141. {
  142. this.cancelled = true;
  143. }
  144. }
  145. }
  146. Utility.GCFullCollect();
  147. }
  148. public CloseWorkspaceAction()
  149. : this(null)
  150. {
  151. }
  152. public CloseWorkspaceAction(DocumentWorkspace closeMe)
  153. {
  154. this.closeMe = closeMe;
  155. this.cancelled = false;
  156. }
  157. }
  158. }