PrintControllerWithStatusDialog.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jonathan Chambers ([email protected])
  24. //
  25. using System;
  26. using System.Drawing;
  27. using System.Drawing.Printing;
  28. using System.Reflection;
  29. namespace System.Windows.Forms
  30. {
  31. public class PrintControllerWithStatusDialog : PrintController {
  32. #region Local variables
  33. PrintController underlyingController;
  34. string dialogTitle;
  35. PrintingDialog dialog;
  36. int currentPage;
  37. #endregion // Local variables
  38. #region Public Constructors
  39. [MonoTODO("Localize Dialog Title")]
  40. public PrintControllerWithStatusDialog(PrintController underlyingController) {
  41. this.underlyingController = underlyingController;
  42. dialog = new PrintingDialog();
  43. dialog.Text = "Printing";
  44. }
  45. public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) : this(underlyingController) {
  46. dialog.Text = dialogTitle;
  47. }
  48. #endregion // Public Constructors
  49. #region Protected Instance Methods
  50. public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) {
  51. if (dialog.DialogResult == DialogResult.Cancel) {
  52. e.Cancel = true;
  53. dialog.Hide();
  54. return;
  55. }
  56. underlyingController.OnEndPage (document, e);
  57. }
  58. public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
  59. dialog.Hide();
  60. underlyingController.OnEndPrint (document, e);
  61. }
  62. public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
  63. if (dialog.DialogResult == DialogResult.Cancel) {
  64. e.Cancel = true;
  65. dialog.Hide();
  66. return null;
  67. }
  68. dialog.LabelText = string.Format("Page {0} of document", ++currentPage);
  69. return underlyingController.OnStartPage (document, e);
  70. }
  71. void Set_PrinterSettings_PrintFileName (PrinterSettings settings, string filename)
  72. {
  73. PropertyInfo PrintFileName = typeof (PrinterSettings).GetProperty ("PrintFileName", BindingFlags.NonPublic | BindingFlags.Instance);
  74. PrintFileName.SetValue (settings, filename, null);
  75. }
  76. public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
  77. try {
  78. currentPage = 0;
  79. dialog.Show();
  80. if (document.PrinterSettings.PrintToFile) {
  81. SaveFileDialog d = new SaveFileDialog ();
  82. if (d.ShowDialog () != DialogResult.OK)
  83. // Windows throws a Win32Exception here.
  84. throw new Exception ("The operation was canceled by the user");
  85. Set_PrinterSettings_PrintFileName (document.PrinterSettings, d.FileName);
  86. }
  87. underlyingController.OnStartPrint (document, e);
  88. }
  89. catch {
  90. dialog.Hide ();
  91. throw;
  92. }
  93. }
  94. #endregion // Protected Instance Methods
  95. #region Internal Class
  96. class PrintingDialog : Form {
  97. private Button buttonCancel;
  98. private Label label;
  99. public PrintingDialog() {
  100. buttonCancel = new System.Windows.Forms.Button();
  101. label = new System.Windows.Forms.Label();
  102. SuspendLayout();
  103. buttonCancel.Location = new System.Drawing.Point(88, 88);
  104. buttonCancel.Name = "buttonCancel";
  105. buttonCancel.TabIndex = 0;
  106. buttonCancel.Text = "Cancel";
  107. label.Location = new System.Drawing.Point(0, 40);
  108. label.Name = "label";
  109. label.Size = new System.Drawing.Size(257, 23);
  110. label.TabIndex = 1;
  111. label.Text = "Page 1 of document";
  112. label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  113. AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  114. CancelButton = buttonCancel;
  115. ClientSize = new System.Drawing.Size(258, 124);
  116. ControlBox = false;
  117. Controls.Add(label);
  118. Controls.Add(buttonCancel);
  119. FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  120. Name = "PrintingDialog";
  121. ShowInTaskbar = false;
  122. Text = "Printing";
  123. ResumeLayout(false);
  124. }
  125. public string LabelText {
  126. get { return label.Text; }
  127. set { label.Text = value; }
  128. }
  129. }
  130. #endregion Internal Class
  131. }
  132. }