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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006 Novell, Inc.
23 // Jonathan Chambers (jonathan.chambers@ansys.com)
28 using System.Drawing.Printing;
29 using System.Reflection;
31 namespace System.Windows.Forms
34 public class PrintControllerWithStatusDialog : PrintController {
35 #region Local variables
36 PrintController underlyingController;
38 PrintingDialog dialog;
40 #endregion // Local variables
42 #region Public Constructors
44 [MonoTODO("Localize Dialog Title")]
45 public PrintControllerWithStatusDialog(PrintController underlyingController) {
46 this.underlyingController = underlyingController;
47 dialog = new PrintingDialog();
48 dialog.Text = "Printing";
51 public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) : this(underlyingController) {
52 dialog.Text = dialogTitle;
54 #endregion // Public Constructors
56 #region Protected Instance Methods
57 public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) {
58 if (dialog.DialogResult == DialogResult.Cancel) {
63 underlyingController.OnEndPage (document, e);
66 public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
68 underlyingController.OnEndPrint (document, e);
71 public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
72 if (dialog.DialogResult == DialogResult.Cancel) {
77 dialog.LabelText = string.Format("Page {0} of document", ++currentPage);
78 return underlyingController.OnStartPage (document, e);
81 void Set_PrinterSettings_PrintFileName (PrinterSettings settings, string filename)
83 PropertyInfo PrintFileName = typeof (PrinterSettings).GetProperty ("PrintFileName", BindingFlags.NonPublic | BindingFlags.Instance);
85 PrintFileName.SetValue (settings, filename, null);
88 public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
91 if (document.PrinterSettings.PrintToFile) {
92 SaveFileDialog d = new SaveFileDialog ();
93 if (d.ShowDialog () != DialogResult.OK)
94 // Windows throws a Win32Exception here.
95 throw new Exception ("The operation was canceled by the user");
96 Set_PrinterSettings_PrintFileName (document.PrinterSettings, d.FileName);
98 underlyingController.OnStartPrint (document, e);
101 #endregion // Protected Instance Methods
103 #region Internal Class
104 class PrintingDialog : Form {
105 private Button buttonCancel;
108 public PrintingDialog() {
109 buttonCancel = new System.Windows.Forms.Button();
110 label = new System.Windows.Forms.Label();
113 buttonCancel.Location = new System.Drawing.Point(88, 88);
114 buttonCancel.Name = "buttonCancel";
115 buttonCancel.TabIndex = 0;
116 buttonCancel.Text = "Cancel";
118 label.Location = new System.Drawing.Point(0, 40);
119 label.Name = "label";
120 label.Size = new System.Drawing.Size(257, 23);
122 label.Text = "Page 1 of document";
123 label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
125 AutoScaleBaseSize = new System.Drawing.Size(5, 13);
126 CancelButton = buttonCancel;
127 ClientSize = new System.Drawing.Size(258, 124);
130 Controls.Add(buttonCancel);
131 FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
132 Name = "PrintingDialog";
133 ShowInTaskbar = false;
138 public string LabelText {
139 get { return label.Text; }
140 set { label.Text = value; }
143 #endregion Internal Class