Merge pull request #2057 from directhex/monolite-on-jenkins
[mono.git] / mcs / class / System.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                 public PrintControllerWithStatusDialog(PrintController underlyingController) {
44                         this.underlyingController = underlyingController;
45                         dialog = new PrintingDialog();
46                         dialog.Text = "Printing";
47                 }
48
49                 public PrintControllerWithStatusDialog(PrintController underlyingController, string dialogTitle) : this(underlyingController) {
50                         dialog.Text = dialogTitle;
51                 }
52                 #endregion // Public Constructors
53                 
54                 #region Protected Instance Methods
55                 public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) {
56                         if (dialog.DialogResult == DialogResult.Cancel) {
57                                 e.Cancel = true;
58                                 dialog.Hide();
59                                 return;
60                         }
61                         underlyingController.OnEndPage (document, e);
62                 }
63
64                 public override void OnEndPrint(PrintDocument document, PrintEventArgs e) {
65                         dialog.Hide();
66                         underlyingController.OnEndPrint (document, e);
67                 }
68
69                 public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
70                         if (dialog.DialogResult == DialogResult.Cancel) {
71                                 e.Cancel = true;
72                                 dialog.Hide();
73                                 return null;
74                         }
75                         dialog.LabelText = string.Format("Page {0} of document", ++currentPage);
76                         return underlyingController.OnStartPage (document, e);
77                 }
78
79                 void Set_PrinterSettings_PrintFileName (PrinterSettings settings, string filename)
80                 {
81                         settings.PrintFileName = filename;
82                 }
83
84                 public override void OnStartPrint(PrintDocument document, PrintEventArgs e) {
85                         try {
86                                 currentPage = 0;
87                                 dialog.Show();
88                                 if (document.PrinterSettings.PrintToFile) {
89                                         SaveFileDialog d = new SaveFileDialog ();
90                                         if (d.ShowDialog () != DialogResult.OK)
91                                                 // Windows throws a Win32Exception here.
92                                                 throw new Exception ("The operation was canceled by the user");
93                                         Set_PrinterSettings_PrintFileName (document.PrinterSettings, d.FileName);
94                                 }
95                                 underlyingController.OnStartPrint (document, e);
96                         }
97                         catch {
98                                 dialog.Hide ();
99                                 throw;
100                         }
101                 }
102
103                 #endregion      // Protected Instance Methods
104
105                 #region Public Properties
106                 public override bool IsPreview {
107                         get { return underlyingController.IsPreview; }
108                 }
109                 #endregion
110         
111                 #region Internal Class
112                 class PrintingDialog : Form {
113                         private Button buttonCancel;
114                         private Label label;
115
116                         public PrintingDialog() {
117                                 buttonCancel = new System.Windows.Forms.Button();
118                                 label = new System.Windows.Forms.Label();
119                                 SuspendLayout();
120
121                                 buttonCancel.Location = new System.Drawing.Point(88, 88);
122                                 buttonCancel.Name = "buttonCancel";
123                                 buttonCancel.TabIndex = 0;
124                                 buttonCancel.Text = "Cancel";
125
126                                 label.Location = new System.Drawing.Point(0, 40);
127                                 label.Name = "label";
128                                 label.Size = new System.Drawing.Size(257, 23);
129                                 label.TabIndex = 1;
130                                 label.Text = "Page 1 of document";
131                                 label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
132
133                                 AutoScaleBaseSize = new System.Drawing.Size(5, 13);
134                                 CancelButton = buttonCancel;
135                                 ClientSize = new System.Drawing.Size(258, 124);
136                                 ControlBox = false;
137                                 Controls.Add(label);
138                                 Controls.Add(buttonCancel);
139                                 FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
140                                 Name = "PrintingDialog";
141                                 ShowInTaskbar = false;
142                                 Text = "Printing";
143                                 ResumeLayout(false);
144                         }
145
146                         public string LabelText {
147                                 get { return label.Text; }
148                                 set { label.Text = value; }
149                         }
150                 }
151                 #endregion Internal Class
152         }
153 }