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