2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PrintControllerWithStatusDialog.cs
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 (jonathan.chambers@ansys.com)
24 //
25
26 using System;
27 using System.Drawing;
28 using System.Drawing.Printing;
29 using System.Reflection;
30
31 namespace System.Windows.Forms
32 {
33         
34         public class PrintControllerWithStatusDialog : PrintController {
35                 #region Local variables
36                 PrintController underlyingController;
37                 PrintingDialog dialog;
38                 int currentPage;
39                 #endregion // Local variables
40
41                 #region Public Constructors
42
43                 [MonoTODO("Localize Dialog Title")]
44                 public PrintControllerWithStatusDialog(PrintController underlyingController) {
45                         this.underlyingController = underlyingController;
46                         dialog = new PrintingDialog();
47                         dialog.Text = "Printing";
48                 }
49
50                 public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) : this(underlyingController) {
51                         dialog.Text = dialogTitle;
52                 }
53                 #endregion // Public Constructors
54                 
55                 #region Protected Instance Methods
56                 public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) {
57                         if (dialog.DialogResult == DialogResult.Cancel) {
58                                 e.Cancel = true;
59                                 dialog.Hide();
60                                 return;
61                         }
62                         underlyingController.OnEndPage (document, e);
63                 }
64
65                 public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
66                         dialog.Hide();
67                         underlyingController.OnEndPrint (document, e);
68                 }
69
70                 public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
71                         if (dialog.DialogResult == DialogResult.Cancel) {
72                                 e.Cancel = true;
73                                 dialog.Hide();
74                                 return null;
75                         }
76                         dialog.LabelText = string.Format("Page {0} of document", ++currentPage);
77                         return underlyingController.OnStartPage (document, e);
78                 }
79
80                 void Set_PrinterSettings_PrintFileName (PrinterSettings settings, string filename)
81                 {
82                         PropertyInfo PrintFileName = typeof (PrinterSettings).GetProperty ("PrintFileName", BindingFlags.NonPublic | BindingFlags.Instance);
83
84                         PrintFileName.SetValue (settings, filename, null);
85                 }
86
87                 public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
88                         try {
89                                 currentPage = 0;
90                                 dialog.Show();
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);
97                                 }
98                                 underlyingController.OnStartPrint (document, e);
99                         }
100                         catch {
101                                 dialog.Hide ();
102                                 throw;
103                         }
104                 }
105
106                 #endregion      // Protected Instance Methods
107
108                 #region Internal Class
109                 class PrintingDialog : Form {
110                         private Button buttonCancel;
111                         private Label label;
112
113                         public PrintingDialog() {
114                                 buttonCancel = new System.Windows.Forms.Button();
115                                 label = new System.Windows.Forms.Label();
116                                 SuspendLayout();
117
118                                 buttonCancel.Location = new System.Drawing.Point(88, 88);
119                                 buttonCancel.Name = "buttonCancel";
120                                 buttonCancel.TabIndex = 0;
121                                 buttonCancel.Text = "Cancel";
122
123                                 label.Location = new System.Drawing.Point(0, 40);
124                                 label.Name = "label";
125                                 label.Size = new System.Drawing.Size(257, 23);
126                                 label.TabIndex = 1;
127                                 label.Text = "Page 1 of document";
128                                 label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
129
130                                 AutoScaleBaseSize = new System.Drawing.Size(5, 13);
131                                 CancelButton = buttonCancel;
132                                 ClientSize = new System.Drawing.Size(258, 124);
133                                 ControlBox = false;
134                                 Controls.Add(label);
135                                 Controls.Add(buttonCancel);
136                                 FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
137                                 Name = "PrintingDialog";
138                                 ShowInTaskbar = false;
139                                 Text = "Printing";
140                                 ResumeLayout(false);
141                         }
142
143                         public string LabelText {
144                                 get { return label.Text; }
145                                 set { label.Text = value; }
146                         }
147                 }
148                 #endregion Internal Class
149         }
150 }