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) 2005-2006 Novell, Inc.
24 // Jonathan Chambers (jonathan.chambers@ansys.com)
25 // Jordi Mas i Hernandez (jordimash@gmail.com)
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
32 using System.Collections;
33 using System.Diagnostics;
35 using System.Drawing.Printing;
36 using System.Reflection;
38 namespace System.Windows.Forms
40 [DefaultProperty("Document")]
41 public sealed class PrintDialog : CommonDialog {
42 PrintDocument document;
43 bool allow_current_page;
44 bool allow_print_to_file;
46 bool allow_some_pages;
50 PrinterSettings current_settings;
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;
76 installed_printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
79 CreateFormControls ();
83 public override void Reset ()
85 current_settings = new PrinterSettings ();
86 AllowPrintToFile = true;
87 AllowSelection = false;
88 AllowSomePages = false;
95 public bool AllowCurrentPage {
97 return allow_current_page;
101 allow_current_page = value;
102 radio_pages.Enabled = value;
108 public bool AllowPrintToFile {
110 return allow_print_to_file;
114 allow_print_to_file = value;
115 chkbox_print.Enabled = value;
119 [DefaultValue(false)]
120 public bool AllowSelection {
122 return allow_selection;
126 allow_selection = value;
127 radio_sel.Enabled = value;
131 [DefaultValue(false)]
132 public bool AllowSomePages {
134 return allow_some_pages;
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;
145 if (current_settings != null) {
146 txtFrom.Text = current_settings.FromPage.ToString ();
147 txtTo.Text = current_settings.ToPage.ToString ();
153 public PrintDocument Document {
160 current_settings = value == null ? new PrinterSettings () : value.PrinterSettings;
166 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
167 public PrinterSettings PrinterSettings {
169 return current_settings;
173 if (value != null && value == current_settings)
176 current_settings = value == null ? new PrinterSettings () : value;
181 [DefaultValue(false)]
182 public bool PrintToFile {
184 return print_to_file;
188 print_to_file = value;
193 public bool ShowNetwork {
199 show_network = value;
203 [DefaultValue(false)]
204 public bool ShowHelp {
215 protected override bool RunDialog (IntPtr hwnd)
217 if (allow_some_pages && current_settings.FromPage > current_settings.ToPage)
218 throw new ArgumentException ("FromPage out of range");
220 if (allow_some_pages) {
221 txtFrom.Text = current_settings.FromPage.ToString ();
222 txtTo.Text = current_settings.ToPage.ToString ();
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;
230 radio_all.Checked = true;
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;
240 SetPrinterDetails ();
245 private void OnClickCancelButton (object sender, EventArgs e)
247 form.DialogResult = DialogResult.Cancel;
250 void ShowErrorMessage (string message, Control control_to_focus)
252 MessageBox.Show (message, "Print", MessageBoxButtons.OK, MessageBoxIcon.Warning);
253 if (control_to_focus != null)
254 control_to_focus.Focus ();
257 private void OnClickOkButton (object sender, EventArgs e)
259 if (updown_copies.Text.Length < 1) {
260 ShowErrorMessage ("The 'Copies' value cannot be empty and must be a positive value.",
265 int from = -1, to = -1;
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.",
275 from = Int32.Parse (txtFrom.Text);
276 to = Int32.Parse (txtTo.Text);
279 ShowErrorMessage ("From/To values should be numeric", txtFrom);
284 ShowErrorMessage ("'From' value cannot be greater than 'To' value.", txtFrom);
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);
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);
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;
308 current_settings.PrintRange = PrintRange.Selection;
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;
315 current_settings.Collate = chkbox_collate.Checked;
317 if (allow_print_to_file) {
318 current_settings.PrintToFile = chkbox_print.Checked;
321 form.DialogResult = DialogResult.OK;
323 if (printer_combo.SelectedItem != null)
324 current_settings.PrinterName = (string) printer_combo.SelectedItem;
326 if (document != null) {
327 document.PrintController = new PrintControllerWithStatusDialog (document.PrintController);
328 document.PrinterSettings = current_settings;
332 private void ShowHelpButton ()
334 if (help_button == null) {
335 help_button = new Button ();
336 help_button.TabIndex = 60;
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);
346 help_button.Visible = show_help;
349 private void OnUpDownValueChanged (object sender, System.EventArgs e)
351 chkbox_collate.Enabled = (updown_copies.Value > 1) ? true : false;
354 void OnPagesCheckedChanged (object obj, EventArgs args)
356 if (radio_pages.Checked && !txtTo.Focused)
360 private void CreateFormControls ()
362 form.SuspendLayout ();
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
434 label = new Label ();
435 label.Text = "Comment:";
436 label.AutoSize = true;
437 label.Location = new Point (20, 120);
438 group_box_prn.Controls.Add (label);
440 label_comment = new Label ();
441 label_comment.AutoSize = true;
442 label_comment.Location = new Point (80, 120);
444 radio_all = new RadioButton ();
445 radio_all.TabIndex = 21;
446 radio_all.Location = new Point (20, 20);
447 radio_all.Text = "&All";
448 radio_all.Checked = true;
449 group_box_range.Controls.Add (radio_all);
451 radio_pages = new RadioButton ();
452 radio_pages.TabIndex = 22;
453 radio_pages.Location = new Point (20, 46);
454 radio_pages.Text = "Pa&ges";
455 radio_pages.Width = 60;
456 radio_pages.CheckedChanged += new EventHandler (OnPagesCheckedChanged);
457 group_box_range.Controls.Add (radio_pages);
459 radio_sel = new RadioButton ();
460 radio_sel.TabIndex = 23;
461 radio_sel.Location = new Point (20, 72);
462 radio_sel.Text = "&Selection";
463 group_box_range.Controls.Add (radio_sel);
465 labelFrom = new Label ();
466 labelFrom.Text = "&from:";
467 labelFrom.TabIndex = 24;
468 labelFrom.AutoSize = true;
469 labelFrom.Location = new Point (80, 50);
470 group_box_range.Controls.Add (labelFrom);
472 txtFrom = new TextBox ();
473 txtFrom.TabIndex = 25;
474 txtFrom.Location = new Point (120, 50);
476 txtFrom.TextChanged += new EventHandler (OnPagesTextChanged);
477 group_box_range.Controls.Add (txtFrom);
479 labelTo = new Label ();
480 labelTo.Text = "&to:";
481 labelTo.TabIndex = 26;
482 labelTo.AutoSize = true;
483 labelTo.Location = new Point (170, 50);
484 group_box_range.Controls.Add (labelTo);
486 txtTo = new TextBox ();
488 txtTo.Location = new Point (190, 50);
490 txtTo.TextChanged += new EventHandler (OnPagesTextChanged);
491 group_box_range.Controls.Add (txtTo);
493 chkbox_print = new CheckBox ();
494 chkbox_print.Location = new Point (305, 115);
495 chkbox_print.Text = "Print to fil&e";
497 label = new Label ();
498 label.Text = "Number of &copies:";
499 label.AutoSize = true;
500 label.Location = new Point (20, 20);
501 group_box_copies.Controls.Add (label);
503 updown_copies = new NumericUpDown ();
504 updown_copies.TabIndex = 31;
505 updown_copies.Location = new Point (120, 20);
506 updown_copies.Minimum = 1;
507 group_box_copies.Controls.Add (updown_copies);
508 updown_copies.ValueChanged += new System.EventHandler (OnUpDownValueChanged);
509 updown_copies.Size = new System.Drawing.Size (40, 20);
511 chkbox_collate = new CheckBox ();
512 chkbox_collate.TabIndex = 32;
513 chkbox_collate.Location = new Point (20, 40);
514 chkbox_collate.Text = "C&ollate";
515 chkbox_collate.Width = 80;
516 group_box_copies.Controls.Add (chkbox_collate);
521 printer_combo = new ComboBox ();
522 printer_combo.DropDownStyle = ComboBoxStyle.DropDownList;
523 printer_combo.Location = new Point (80, 32);
524 printer_combo.Width = 220;
525 printer_combo.SelectedIndexChanged += new EventHandler (OnPrinterSelectedIndexChanged);
527 default_printer_settings = new PrinterSettings ();
528 for (int i = 0; i < installed_printers.Count; i++) {
529 printer_combo.Items.Add (installed_printers[i]);
530 if (installed_printers[i] == default_printer_settings.PrinterName)
531 printer_combo.SelectedItem = installed_printers[i];
533 printer_combo.TabIndex = 11;
534 chkbox_print.TabIndex = 12;
535 group_box_prn.Controls.Add (printer_combo);
536 group_box_prn.Controls.Add (chkbox_print);
538 form.Size = new Size (450, 327); // 384
539 form.FormBorderStyle = FormBorderStyle.FixedDialog;
540 form.MaximizeBox = false;
541 group_box_prn.TabIndex = 10;
542 group_box_range.TabIndex = 20;
543 group_box_copies.TabIndex = 30;
544 accept_button.TabIndex = 40;
545 cancel_button.TabIndex = 50;
546 form.Controls.Add (group_box_prn);
547 form.Controls.Add (group_box_range);
548 form.Controls.Add (group_box_copies);
549 form.Controls.Add (accept_button);
550 form.Controls.Add (cancel_button);
551 form.ResumeLayout (false);
554 void OnPagesTextChanged (object sender, EventArgs args)
556 radio_pages.Checked = true;
559 private void OnPrinterSelectedIndexChanged (object sender, System.EventArgs e)
561 SetPrinterDetails ();
564 private void SetPrinterDetails ()
568 string printer, port = string.Empty, type = string.Empty;
569 string status = string.Empty, comment = string.Empty;
570 Type sysprn = Type.GetType ("System.Drawing.Printing.SysPrn, System.Drawing");
571 MethodInfo dlg_info = sysprn.GetMethod ("GetPrintDialogInfo", BindingFlags.Static | BindingFlags.NonPublic);
573 printer = (string) printer_combo.SelectedItem;
575 if (printer != null) {
576 object[] args = new object [5];
582 dlg_info.Invoke (null, args);
583 port = (string) args[1];
584 type = (string) args[2];
585 status = (string) args[3];
586 comment = (string) args[4];
589 label_status.Text = status;
590 label_type.Text = type;
591 label_where.Text = port;
592 label_comment.Text = comment;