2006-06-28 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PrintDialog.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) 2005-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Someone
24 //      Jonathan Chambers (jonathan.chambers@ansys.com)
25 //      Jordi Mas i Hernandez (jordimash@gmail.com)
26 //
27
28 using System;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
32 using System.Collections;
33 using System.Diagnostics;
34 using System.Drawing;
35 using System.Drawing.Printing;
36 using System.Reflection;
37
38 namespace System.Windows.Forms
39 {
40         [DefaultProperty("Document")]
41         public sealed class PrintDialog : CommonDialog {
42                 PrintDocument document;
43                 PrinterSettings printer_settings;
44                 bool allow_current_page;
45                 bool allow_print_to_file;
46                 bool allow_selection;
47                 bool allow_some_pages;
48                 bool show_help;
49                 bool show_network;
50                 bool print_to_file;
51                 PrinterSettings current_settings;
52
53                 private Button cancel_button;
54                 private Button accept_button;
55                 private Button help_button;
56                 private ComboBox printer_combo;
57                 private RadioButton radio_all;
58                 private RadioButton radio_pages;
59                 private RadioButton radio_sel;
60                 private PrinterSettings.StringCollection installed_printers;
61                 private PrinterSettings default_printer_settings;
62                 private TextBox txtFrom;
63                 private TextBox txtTo;
64                 private Label labelTo;
65                 private Label labelFrom;
66                 private CheckBox chkbox_print;
67                 private NumericUpDown updown_copies;
68                 private CheckBox chkbox_collate;
69                 private Label label_status;
70                 private Label label_type;
71                 private Label label_where;
72                 private Label label_comment;
73
74                 public PrintDialog ()
75                 {
76                         help_button = null;
77                         installed_printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
78
79                         form.Text = "Print";
80                         CreateFormControls ();
81                         Reset ();
82                 }
83
84                 public override void Reset ()
85                 {
86                         current_settings = null;
87                         AllowPrintToFile = true;
88                         AllowSelection = false;
89                         AllowSomePages = false;
90                         PrintToFile = false;
91                         ShowHelp = false;
92                         ShowNetwork = true;
93                 }
94
95 #if NET_2_0
96                 public bool AllowCurrentPage {
97                         get {
98                                 return allow_current_page;
99                         }
100
101                         set {
102                                 allow_current_page = value;
103                                 radio_pages.Enabled = value;
104                         }
105                 }
106 #endif
107
108                 [DefaultValue(true)]
109                 public bool AllowPrintToFile {
110                         get {
111                                 return allow_print_to_file;
112                         }
113
114                         set {
115                                 allow_print_to_file = value;
116                                 chkbox_print.Enabled = value;
117                         }
118                 }
119
120                 [DefaultValue(false)]
121                 public bool AllowSelection {
122                         get {
123                                 return allow_selection;
124                         }
125
126                         set {
127                                 allow_selection = value;
128                                 radio_sel.Enabled = value;
129                         }
130                 }
131
132                 [DefaultValue(false)]
133                 public bool AllowSomePages {
134                         get {
135                                 return allow_some_pages;
136                         }
137
138                         set {
139                                 allow_some_pages = value;
140                                 radio_pages.Enabled = value;
141                                 txtFrom.Enabled = value;
142                                 txtTo.Enabled = value;
143                                 labelTo.Enabled = value;
144                                 labelFrom.Enabled = value;
145
146                                 if (current_settings != null) {
147                                         txtFrom.Text = current_settings.FromPage.ToString ();
148                                         txtTo.Text = current_settings.ToPage.ToString ();
149                                 }
150                         }
151                 }
152
153                 [DefaultValue(null)]
154                 public PrintDocument Document {
155                         get {
156                                 return document;
157                         }
158
159                         set {
160                                 if (value == null)
161                                         return;
162
163                                 document = value;
164                                 current_settings = value.PrinterSettings;
165                                 printer_settings  = null;
166                         }
167                 }
168
169                 [Browsable(false)]
170                 [DefaultValue(null)]
171                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
172                 public PrinterSettings PrinterSettings {
173                         get {
174                                 return printer_settings;
175                         }
176
177                         set {
178                                 current_settings = printer_settings = value;
179                                 document = null;
180                         }
181                 }
182
183                 [DefaultValue(false)]
184                 public bool PrintToFile {
185                         get {
186                                 return print_to_file;
187                         }
188
189                         set {
190                                 print_to_file = value;
191                         }
192                 }
193
194                 [DefaultValue(true)]
195                 public bool ShowNetwork {
196                         get {
197                                 return show_network;
198                         }
199
200                         set {
201                                 show_network = value;
202                         }
203                 }
204
205                 [DefaultValue(false)]
206                 public bool ShowHelp {
207                         get {
208                                 return show_help;
209                         }
210
211                         set {
212                                 show_help = value;
213                                 ShowHelpButton ();
214                         }
215                 }
216
217                 protected override bool RunDialog (IntPtr hwnd)
218                 {
219                         if (Document == null && PrinterSettings == null)
220                                 throw new ArgumentException ("PrintDialog needs a Docouement or PrinterSettings object to display");
221
222                         if (allow_some_pages && current_settings.FromPage > current_settings.ToPage)
223                                 throw new ArgumentException ("FromPage out of range");
224
225                         if (allow_some_pages && current_settings != null) {
226                                 txtFrom.Text = current_settings.FromPage.ToString ();
227                                 txtTo.Text = current_settings.ToPage.ToString ();
228                         }
229
230                         updown_copies.Value = current_settings.Copies;
231                         chkbox_collate.Enabled = (updown_copies.Value > 0) ? true : false;
232
233                         if (show_help) {
234                                 ShowHelpButton ();
235                         }
236
237                         SetPrinterDetails ();
238
239                         return true;
240                 }
241
242                 private void OnClickCancelButton (object sender, EventArgs e)
243                 {
244                         form.DialogResult = DialogResult.Cancel;
245                 }
246
247                 private void OnClickOkButton (object sender, EventArgs e)
248                 {
249                         int from, to;
250
251                         try {
252                                 from = Int32.Parse (txtFrom.Text);
253                                 to = Int32.Parse (txtTo.Text);
254                         }
255
256                         catch {
257                                 MessageBox.Show ("From/To values should be numeric", "Print",
258                                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);
259                                 return;
260                         }
261
262                         if (allow_some_pages) {
263                                 if (from > to) {
264                                         MessageBox.Show ("From value cannot be greater than To value", "Print",
265                                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);
266                                         return;
267                                 }
268
269                                 if (to < current_settings.MinimumPage || to > current_settings.MaximumPage) {
270                                         MessageBox.Show ("To value is not within the page range\n" +
271                                                 "Enter a number between " + current_settings.MinimumPage +
272                                                 " and " + current_settings.MaximumPage, "Print",
273                                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);
274                                         return;
275                                 }
276
277                                 if (from < current_settings.MinimumPage || from > current_settings.MaximumPage) {
278                                         txtTo.Focus ();
279                                         MessageBox.Show ("From value is not within the page range\n" +
280                                                 "Enter a number between " + current_settings.MinimumPage +
281                                                 " and " + current_settings.MaximumPage, "Print",
282                                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);
283                                         return;
284                                 }
285                         }
286
287                         if (radio_all.Checked == true) {
288                                 current_settings.PrintRange = PrintRange.AllPages;
289                         } else {
290                                 if (radio_pages.Checked == true) {
291                                         current_settings.PrintRange = PrintRange.SomePages;
292                                 } else {
293                                         if (radio_sel.Checked == true) {
294                                                 current_settings.PrintRange = PrintRange.Selection;
295                                         }
296                                 }
297                         }
298
299                         current_settings.Copies = (short) updown_copies.Value;
300                         current_settings.FromPage = from;
301                         current_settings.ToPage = to;
302                         current_settings.Collate = chkbox_collate.Checked;
303
304                         if (allow_print_to_file) {
305                                 current_settings.PrintToFile = chkbox_print.Checked;
306                         }
307
308                         form.DialogResult = DialogResult.OK;
309
310                         if (printer_combo.SelectedItem != null)
311                                 current_settings.PrinterName = (string) printer_combo.SelectedItem;
312                 }
313
314                 private void ShowHelpButton ()
315                 {
316                         if (help_button == null) {
317                                 help_button = new Button ();
318                                 help_button.TabIndex = 60;
319
320                                 help_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
321                                 help_button.FlatStyle = FlatStyle.System;
322                                 help_button.Location = new Point (20, 270);
323                                 help_button.Text = "&Help";
324                                 help_button.FlatStyle = FlatStyle.System;
325                                 form.Controls.Add (help_button);
326                         }
327
328                         help_button.Visible = show_help;
329                 }
330
331                 private void OnUpDownValueChanged (object sender, System.EventArgs e)
332                 {
333                         chkbox_collate.Enabled = (updown_copies.Value > 0) ? true : false;
334                 }
335
336                 private void CreateFormControls ()
337                 {
338                         form.SuspendLayout ();
339
340                         GroupBox group_box_prn = new GroupBox ();
341                         group_box_prn.Location = new Point (10, 8);
342                         group_box_prn.Text = "Printer";
343                         group_box_prn.Size = new Size (420, 145);
344
345                         GroupBox group_box_range = new GroupBox ();
346                         group_box_range.Location = new Point (10, 155);
347                         group_box_range.Text = "Print range";
348                         group_box_range.Size = new Size (240, 100);
349
350                         GroupBox group_box_copies = new GroupBox ();
351                         group_box_copies.Location = new Point (265, 155);
352                         group_box_copies.Text = "Copies";
353                         group_box_copies.Size = new Size (165, 100);
354
355                         // Accept button
356                         accept_button = new Button ();
357                         form.AcceptButton = accept_button;
358                         accept_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
359                         accept_button.FlatStyle = FlatStyle.System;
360                         accept_button.Location = new Point (265, 270);
361                         accept_button.Text = "OK";
362                         accept_button.FlatStyle = FlatStyle.System;
363                         accept_button.Click += new EventHandler (OnClickOkButton);
364
365                         // Cancel button
366                         cancel_button = new Button ();
367                         form.CancelButton = cancel_button;
368                         cancel_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
369                         cancel_button.FlatStyle = FlatStyle.System;
370                         cancel_button.Location = new Point (350, 270);
371                         cancel_button.Text = "Cancel";
372                         cancel_button.FlatStyle = FlatStyle.System;
373                         cancel_button.Click += new EventHandler (OnClickCancelButton);
374
375                         // Static controls
376                         Label label = new Label ();
377                         label.AutoSize = true;
378                         label.Text = "&Name:";
379                         label.Location = new Point (20, 33);
380                         group_box_prn.Controls.Add (label);
381
382                         label = new Label ();
383                         label.Text = "Status:";
384                         label.AutoSize = true;
385                         label.Location = new Point (20, 60);
386                         group_box_prn.Controls.Add (label);
387
388                         label_status = new Label ();
389                         label_status.AutoSize = true;
390                         label_status.Location = new Point (80, 60);
391                         group_box_prn.Controls.Add (label_status);
392
393                         label = new Label ();
394                         label.Text = "Type:";
395                         label.AutoSize = true;
396                         label.Location = new Point (20, 80);
397                         group_box_prn.Controls.Add (label);
398
399                         label_type = new Label ();
400                         label_type.AutoSize = true;
401                         label_type.Location = new Point (80, 80);
402                         group_box_prn.Controls.Add (label_type);
403
404                         label = new Label ();
405                         label.Text = "Where:";
406                         label.AutoSize = true;
407                         label.Location = new Point (20, 100);
408                         group_box_prn.Controls.Add (label);
409
410                         label = new Label ();
411                         label.Text = "Comment:";
412                         label.AutoSize = true;
413                         label.Location = new Point (20, 120);
414                         group_box_prn.Controls.Add (label);
415
416                         label_comment = new Label ();
417                         label_comment.AutoSize = true;
418                         label_comment.Location = new Point (80, 120);
419
420                         radio_all = new RadioButton ();
421                         radio_all.TabIndex = 21;
422                         radio_all.Location = new Point (20, 20);
423                         radio_all.Text = "&All";
424                         radio_all.Checked = true;
425                         group_box_range.Controls.Add (radio_all);
426
427                         radio_pages = new RadioButton ();
428                         radio_pages.TabIndex = 22;
429                         radio_pages.Location = new Point (20, 46);
430                         radio_pages.Text = "Pa&ges";
431                         radio_pages.Width = 60;
432                         group_box_range.Controls.Add (radio_pages);
433
434                         radio_sel = new RadioButton ();
435                         radio_sel.TabIndex = 23;
436                         radio_sel.Location = new Point (20, 72);
437                         radio_sel.Text = "&Selection";
438                         group_box_range.Controls.Add (radio_sel);
439
440                         labelFrom = new Label ();
441                         labelFrom.Text = "&from:";
442                         labelFrom.AutoSize = true;
443                         labelFrom.Location = new Point (80, 50);
444                         group_box_range.Controls.Add (labelFrom);
445
446                         txtFrom = new TextBox ();
447                         txtFrom.TabIndex = 24;
448                         txtFrom.Location = new Point (120, 50);
449                         txtFrom.Width = 40;
450                         group_box_range.Controls.Add (txtFrom);
451
452                         labelTo = new Label ();
453                         labelTo.Text = "&to:";
454                         labelTo.AutoSize = true;
455                         labelTo.Location = new Point (170, 50);
456                         group_box_range.Controls.Add (labelTo);
457
458                         txtTo = new TextBox ();
459                         txtTo.TabIndex = 25;
460                         txtTo.Location = new Point (190, 50);
461                         txtTo.Width = 40;
462                         group_box_range.Controls.Add (txtTo);
463
464                         chkbox_print = new CheckBox ();
465                         chkbox_print.Location = new Point (305, 115);
466                         chkbox_print.Text = "Print to fil&e";
467
468                         label = new Label ();
469                         label.Text = "Number of &copies:";
470                         label.AutoSize = true;
471                         label.Location = new Point (20, 20);
472                         group_box_copies.Controls.Add (label);
473
474                         updown_copies = new NumericUpDown ();
475                         updown_copies.TabIndex = 31;
476                         updown_copies.Location = new Point (120, 20);
477                         group_box_copies.Controls.Add (updown_copies);
478                         updown_copies.ValueChanged += new System.EventHandler (OnUpDownValueChanged);
479                         updown_copies.Size = new System.Drawing.Size (40, 20);
480
481                         chkbox_collate = new CheckBox ();
482                         chkbox_collate.TabIndex = 32;
483                         chkbox_collate.Location = new Point (20, 40);
484                         chkbox_collate.Text = "C&ollate";
485                         chkbox_collate.Width = 80;
486                         group_box_copies.Controls.Add (chkbox_collate);
487
488
489
490                         // Printer combo
491                         printer_combo = new ComboBox ();
492                         printer_combo.DropDownStyle = ComboBoxStyle.DropDownList;
493                         printer_combo.Location = new Point (80, 32);
494                         printer_combo.Width = 220;
495                         printer_combo.SelectedIndexChanged += new EventHandler (OnPrinterSelectedIndexChanged);
496
497                         default_printer_settings = new PrinterSettings ();
498                         for (int i = 0; i < installed_printers.Count; i++) {
499                                 printer_combo.Items.Add (installed_printers[i]);
500                                 if (installed_printers[i] == default_printer_settings.PrinterName)
501                                         printer_combo.SelectedItem = installed_printers[i];
502                         }
503                         printer_combo.TabIndex = 11;
504                         chkbox_print.TabIndex = 12;
505                         group_box_prn.Controls.Add (printer_combo);
506                         group_box_prn.Controls.Add (chkbox_print);
507
508                         form.Size =  new Size (450, 327); // 384
509                         form.FormBorderStyle = FormBorderStyle.FixedDialog;
510                         form.MaximizeBox = false;
511                         group_box_prn.TabIndex = 10;
512                         group_box_range.TabIndex = 20;
513                         group_box_copies.TabIndex = 30;
514                         accept_button.TabIndex = 40;
515                         cancel_button.TabIndex = 50;
516                         form.Controls.Add (group_box_prn);
517                         form.Controls.Add (group_box_range);
518                         form.Controls.Add (group_box_copies);
519                         form.Controls.Add (accept_button);
520                         form.Controls.Add (cancel_button);
521                         form.ResumeLayout (false);
522                 }
523
524                 private void OnPrinterSelectedIndexChanged (object sender,  System.EventArgs e)
525                 {                       
526                         SetPrinterDetails ();
527                 }
528
529                 private void SetPrinterDetails ()
530                 {
531                         try
532                         {
533                                 string printer, port = string.Empty, type = string.Empty;
534                                 string status = string.Empty, comment = string.Empty;
535                                 Type sysprn = Type.GetType ("System.Drawing.Printing.SysPrn, System.Drawing");
536                                 MethodInfo dlg_info = sysprn.GetMethod ("GetPrintDialogInfo", BindingFlags.Static | BindingFlags.NonPublic);
537
538                                 printer = (string) printer_combo.SelectedItem;
539
540                                 if (printer != null) {
541                                         object[] args  = new object [5];
542                                         args[0] = printer;
543                                         args[1] = port;
544                                         args[2] = type;
545                                         args[3] = status;
546                                         args[4] = comment;
547                                         dlg_info.Invoke (null, args);
548                                         port = (string) args[1];
549                                         type = (string) args[2];
550                                         status = (string) args[3];
551                                         comment = (string) args[4];
552                                 }
553
554                                 label_status.Text = status;
555                                 label_type.Text = type;
556                                 label_where.Text = port;
557                                 label_comment.Text = comment;
558
559                         }
560                         catch  {
561                         }
562                 }
563         }
564 }