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;
37 PrintingDialog dialog;
39 #endregion // Local variables
41 #region Public Constructors
43 [MonoTODO("Localize Dialog Title")]
44 public PrintControllerWithStatusDialog(PrintController underlyingController) {
45 this.underlyingController = underlyingController;
46 dialog = new PrintingDialog();
47 dialog.Text = "Printing";
50 public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) : this(underlyingController) {
51 dialog.Text = dialogTitle;
53 #endregion // Public Constructors
55 #region Protected Instance Methods
56 public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) {
57 if (dialog.DialogResult == DialogResult.Cancel) {
62 underlyingController.OnEndPage (document, e);
65 public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
67 underlyingController.OnEndPrint (document, e);
70 public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
71 if (dialog.DialogResult == DialogResult.Cancel) {
76 dialog.LabelText = string.Format("Page {0} of document", ++currentPage);
77 return underlyingController.OnStartPage (document, e);
80 void Set_PrinterSettings_PrintFileName (PrinterSettings settings, string filename)
83 settings.PrintFileName = filename;
86 PropertyInfo printFileName = typeof (PrinterSettings).GetProperty ("PrintFileName", BindingFlags.NonPublic | BindingFlags.Instance);
87 if (printFileName == null) {
88 throw new Exception ("Unable to set filename to print");
90 printFileName.SetValue (settings, filename, null);
94 public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
98 if (document.PrinterSettings.PrintToFile) {
99 SaveFileDialog d = new SaveFileDialog ();
100 if (d.ShowDialog () != DialogResult.OK)
101 // Windows throws a Win32Exception here.
102 throw new Exception ("The operation was canceled by the user");
103 Set_PrinterSettings_PrintFileName (document.PrinterSettings, d.FileName);
105 underlyingController.OnStartPrint (document, e);
113 #endregion // Protected Instance Methods
116 #region Public Properties
117 public override bool IsPreview {
118 get { return underlyingController.IsPreview; }
123 #region Internal Class
124 class PrintingDialog : Form {
125 private Button buttonCancel;
128 public PrintingDialog() {
129 buttonCancel = new System.Windows.Forms.Button();
130 label = new System.Windows.Forms.Label();
133 buttonCancel.Location = new System.Drawing.Point(88, 88);
134 buttonCancel.Name = "buttonCancel";
135 buttonCancel.TabIndex = 0;
136 buttonCancel.Text = "Cancel";
138 label.Location = new System.Drawing.Point(0, 40);
139 label.Name = "label";
140 label.Size = new System.Drawing.Size(257, 23);
142 label.Text = "Page 1 of document";
143 label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
145 AutoScaleBaseSize = new System.Drawing.Size(5, 13);
146 CancelButton = buttonCancel;
147 ClientSize = new System.Drawing.Size(258, 124);
150 Controls.Add(buttonCancel);
151 FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
152 Name = "PrintingDialog";
153 ShowInTaskbar = false;
158 public string LabelText {
159 get { return label.Text; }
160 set { label.Text = value; }
163 #endregion Internal Class