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
41 [Designer ("System.Windows.Forms.Design.PrintDialogDesigner, " + Consts.AssemblySystem_Design,
42 "System.ComponentModel.Design.IDesigner")]
44 [DefaultProperty("Document")]
45 public sealed class PrintDialog : CommonDialog {
46 PrintDocument document;
48 bool allow_current_page;
50 bool allow_print_to_file;
52 bool allow_some_pages;
56 PrinterSettings current_settings;
58 private Button cancel_button;
59 private Button accept_button;
60 private Button help_button;
61 private ComboBox printer_combo;
62 private RadioButton radio_all;
63 private RadioButton radio_pages;
64 private RadioButton radio_sel;
65 private PrinterSettings.StringCollection installed_printers;
66 private PrinterSettings default_printer_settings;
67 private TextBox txtFrom;
68 private TextBox txtTo;
69 private Label labelTo;
70 private Label labelFrom;
71 private CheckBox chkbox_print;
72 private NumericUpDown updown_copies;
73 private CheckBox chkbox_collate;
74 private Label label_status;
75 private Label label_type;
76 private Label label_where;
77 private Label label_comment;
78 private CollatePreview collate;
83 installed_printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
86 CreateFormControls ();
90 public override void Reset ()
92 current_settings = null;
93 AllowPrintToFile = true;
94 AllowSelection = false;
95 AllowSomePages = false;
102 [DefaultValue (false)]
103 public bool AllowCurrentPage {
105 return allow_current_page;
109 allow_current_page = value;
110 radio_pages.Enabled = value;
116 public bool AllowPrintToFile {
118 return allow_print_to_file;
122 allow_print_to_file = value;
123 chkbox_print.Enabled = value;
127 [DefaultValue(false)]
128 public bool AllowSelection {
130 return allow_selection;
134 allow_selection = value;
135 radio_sel.Enabled = value;
139 [DefaultValue(false)]
140 public bool AllowSomePages {
142 return allow_some_pages;
146 allow_some_pages = value;
147 radio_pages.Enabled = value;
148 txtFrom.Enabled = value;
149 txtTo.Enabled = value;
150 labelTo.Enabled = value;
151 labelFrom.Enabled = value;
153 if (PrinterSettings != null) {
154 txtFrom.Text = PrinterSettings.FromPage.ToString ();
155 txtTo.Text = PrinterSettings.ToPage.ToString ();
161 public PrintDocument Document {
168 current_settings = (value == null) ? new PrinterSettings () : value.PrinterSettings;
174 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
175 public PrinterSettings PrinterSettings {
178 if (current_settings == null)
179 current_settings = new PrinterSettings ();
181 return current_settings;
185 if (value != null && value == current_settings)
188 current_settings = (value == null) ? new PrinterSettings () : value;
193 [DefaultValue(false)]
194 public bool PrintToFile {
196 return print_to_file;
200 print_to_file = value;
205 public bool ShowNetwork {
211 show_network = value;
215 [DefaultValue(false)]
216 public bool ShowHelp {
227 protected override bool RunDialog (IntPtr hwnd)
230 if (PrinterSettings == null)
231 throw new ArgumentException ("PrintDialog needs a PrinterSettings object to display.");
234 if (allow_some_pages && PrinterSettings.FromPage > PrinterSettings.ToPage)
235 throw new ArgumentException ("FromPage out of range");
237 if (allow_some_pages) {
238 txtFrom.Text = PrinterSettings.FromPage.ToString ();
239 txtTo.Text = PrinterSettings.ToPage.ToString ();
242 if (PrinterSettings.PrintRange == PrintRange.SomePages && allow_some_pages)
243 radio_pages.Checked = true;
244 else if (PrinterSettings.PrintRange == PrintRange.Selection && allow_selection)
245 radio_sel.Checked = true;
247 radio_all.Checked = true;
249 updown_copies.Value = PrinterSettings.Copies == 0 ? 1 : (int) PrinterSettings.Copies;
250 chkbox_collate.Checked = PrinterSettings.Collate;
251 chkbox_collate.Enabled = (updown_copies.Value > 1) ? true : false;
257 SetPrinterDetails ();
262 private void OnClickCancelButton (object sender, EventArgs e)
264 form.DialogResult = DialogResult.Cancel;
267 void ShowErrorMessage (string message, Control control_to_focus)
269 MessageBox.Show (message, "Print", MessageBoxButtons.OK, MessageBoxIcon.Warning);
270 if (control_to_focus != null)
271 control_to_focus.Focus ();
274 private void OnClickOkButton (object sender, EventArgs e)
276 if (updown_copies.Text.Length < 1) {
277 ShowErrorMessage ("The 'Copies' value cannot be empty and must be a positive value.",
282 int from = -1, to = -1;
284 if (allow_some_pages && radio_pages.Checked) {
285 if (txtFrom.Text.Length < 1) {
286 ShowErrorMessage ("The 'From' value cannot be empty and must be a positive value.",
292 from = Int32.Parse (txtFrom.Text);
293 to = Int32.Parse (txtTo.Text);
296 ShowErrorMessage ("From/To values should be numeric", txtFrom);
301 ShowErrorMessage ("'From' value cannot be greater than 'To' value.", txtFrom);
305 if (to < PrinterSettings.MinimumPage || to > PrinterSettings.MaximumPage) {
306 ShowErrorMessage ("'To' value is not within the page range\n" +
307 "Enter a number between " + PrinterSettings.MinimumPage +
308 " and " + PrinterSettings.MaximumPage + ".", txtTo);
312 if (from < PrinterSettings.MinimumPage || from > PrinterSettings.MaximumPage) {
313 ShowErrorMessage ("'From' value is not within the page range\n" +
314 "Enter a number between " + PrinterSettings.MinimumPage +
315 " and " + PrinterSettings.MaximumPage + ".", txtFrom);
320 if (radio_all.Checked == true)
321 PrinterSettings.PrintRange = PrintRange.AllPages;
322 else if (radio_pages.Checked == true)
323 PrinterSettings.PrintRange = PrintRange.SomePages;
325 PrinterSettings.PrintRange = PrintRange.Selection;
327 PrinterSettings.Copies = (short) updown_copies.Value;
328 if (PrinterSettings.PrintRange == PrintRange.SomePages) {
329 PrinterSettings.FromPage = from;
330 PrinterSettings.ToPage = to;
332 PrinterSettings.Collate = chkbox_collate.Checked;
334 if (allow_print_to_file) {
335 PrinterSettings.PrintToFile = chkbox_print.Checked;
338 form.DialogResult = DialogResult.OK;
340 if (printer_combo.SelectedItem != null)
341 PrinterSettings.PrinterName = (string) printer_combo.SelectedItem;
343 if (document != null) {
344 document.PrintController = new PrintControllerWithStatusDialog (document.PrintController);
345 document.PrinterSettings = PrinterSettings;
349 private void ShowHelpButton ()
351 if (help_button == null) {
352 help_button = new Button ();
353 help_button.TabIndex = 60;
355 help_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
356 help_button.FlatStyle = FlatStyle.System;
357 help_button.Location = new Point (20, 270);
358 help_button.Text = "&Help";
359 help_button.FlatStyle = FlatStyle.System;
360 form.Controls.Add (help_button);
363 help_button.Visible = show_help;
366 private void OnUpDownValueChanged (object sender, System.EventArgs e)
368 chkbox_collate.Enabled = (updown_copies.Value > 1) ? true : false;
371 void OnPagesCheckedChanged (object obj, EventArgs args)
373 if (radio_pages.Checked && !txtTo.Focused)
377 private void CreateFormControls ()
379 form.SuspendLayout ();
381 GroupBox group_box_prn = new GroupBox ();
382 group_box_prn.Location = new Point (10, 8);
383 group_box_prn.Text = "Printer";
384 group_box_prn.Size = new Size (420, 145);
386 GroupBox group_box_range = new GroupBox ();
387 group_box_range.Location = new Point (10, 155);
388 group_box_range.Text = "Print range";
389 group_box_range.Size = new Size (240, 100);
391 GroupBox group_box_copies = new GroupBox ();
392 group_box_copies.Location = new Point (265, 155);
393 group_box_copies.Text = "Copies";
394 group_box_copies.Size = new Size (165, 100);
397 accept_button = new Button ();
398 form.AcceptButton = accept_button;
399 accept_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
400 accept_button.FlatStyle = FlatStyle.System;
401 accept_button.Location = new Point (265, 270);
402 accept_button.Text = "OK";
403 accept_button.FlatStyle = FlatStyle.System;
404 accept_button.Click += new EventHandler (OnClickOkButton);
407 cancel_button = new Button ();
408 form.CancelButton = cancel_button;
409 cancel_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
410 cancel_button.FlatStyle = FlatStyle.System;
411 cancel_button.Location = new Point (350, 270);
412 cancel_button.Text = "Cancel";
413 cancel_button.FlatStyle = FlatStyle.System;
414 cancel_button.Click += new EventHandler (OnClickCancelButton);
417 Label label = new Label ();
418 label.AutoSize = true;
419 label.Text = "&Name:";
420 label.Location = new Point (20, 33);
421 group_box_prn.Controls.Add (label);
423 label = new Label ();
424 label.Text = "Status:";
425 label.AutoSize = true;
426 label.Location = new Point (20, 60);
427 group_box_prn.Controls.Add (label);
429 label_status = new Label ();
430 label_status.AutoSize = true;
431 label_status.Location = new Point (80, 60);
432 group_box_prn.Controls.Add (label_status);
434 label = new Label ();
435 label.Text = "Type:";
436 label.AutoSize = true;
437 label.Location = new Point (20, 80);
438 group_box_prn.Controls.Add (label);
440 label_type = new Label ();
441 label_type.AutoSize = true;
442 label_type.Location = new Point (80, 80);
443 group_box_prn.Controls.Add (label_type);
445 label = new Label ();
446 label.Text = "Where:";
447 label.AutoSize = true;
448 label.Location = new Point (20, 100);
449 group_box_prn.Controls.Add (label);
451 label_where = new Label ();
452 label_where.AutoSize = true;
453 label_where.Location = new Point (80, 100);
454 group_box_prn.Controls.Add (label_where);
456 label = new Label ();
457 label.Text = "Comment:";
458 label.AutoSize = true;
459 label.Location = new Point (20, 120);
460 group_box_prn.Controls.Add (label);
462 label_comment = new Label ();
463 label_comment.AutoSize = true;
464 label_comment.Location = new Point (80, 120);
465 group_box_prn.Controls.Add (label_comment);
467 radio_all = new RadioButton ();
468 radio_all.TabIndex = 21;
469 radio_all.Location = new Point (20, 20);
470 radio_all.Text = "&All";
471 radio_all.Checked = true;
472 group_box_range.Controls.Add (radio_all);
474 radio_pages = new RadioButton ();
475 radio_pages.TabIndex = 22;
476 radio_pages.Location = new Point (20, 46);
477 radio_pages.Text = "Pa&ges";
478 radio_pages.Width = 60;
479 radio_pages.CheckedChanged += new EventHandler (OnPagesCheckedChanged);
480 group_box_range.Controls.Add (radio_pages);
482 radio_sel = new RadioButton ();
483 radio_sel.TabIndex = 23;
484 radio_sel.Location = new Point (20, 72);
485 radio_sel.Text = "&Selection";
486 group_box_range.Controls.Add (radio_sel);
488 labelFrom = new Label ();
489 labelFrom.Text = "&from:";
490 labelFrom.TabIndex = 24;
491 labelFrom.AutoSize = true;
492 labelFrom.Location = new Point (80, 50);
493 group_box_range.Controls.Add (labelFrom);
495 txtFrom = new TextBox ();
496 txtFrom.TabIndex = 25;
497 txtFrom.Location = new Point (120, 50);
499 txtFrom.TextChanged += new EventHandler (OnPagesTextChanged);
500 group_box_range.Controls.Add (txtFrom);
502 labelTo = new Label ();
503 labelTo.Text = "&to:";
504 labelTo.TabIndex = 26;
505 labelTo.AutoSize = true;
506 labelTo.Location = new Point (170, 50);
507 group_box_range.Controls.Add (labelTo);
509 txtTo = new TextBox ();
511 txtTo.Location = new Point (190, 50);
513 txtTo.TextChanged += new EventHandler (OnPagesTextChanged);
514 group_box_range.Controls.Add (txtTo);
516 chkbox_print = new CheckBox ();
517 chkbox_print.Location = new Point (305, 115);
518 chkbox_print.Text = "Print to fil&e";
521 updown_copies = new NumericUpDown ();
522 updown_copies.TabIndex = 31;
523 updown_copies.Location = new Point (105, 18);
524 updown_copies.Minimum = 1;
525 group_box_copies.Controls.Add (updown_copies);
526 updown_copies.ValueChanged += new System.EventHandler (OnUpDownValueChanged);
527 updown_copies.Size = new System.Drawing.Size (40, 20);
529 label = new Label ();
530 label.Text = "Number of &copies:";
531 label.AutoSize = true;
532 label.Location = new Point (10, 20);
533 group_box_copies.Controls.Add (label);
535 chkbox_collate = new CheckBox ();
536 chkbox_collate.TabIndex = 32;
537 chkbox_collate.Location = new Point (105, 55);
538 chkbox_collate.Text = "C&ollate";
539 chkbox_collate.Width = 58;
540 chkbox_collate.CheckedChanged += new EventHandler(chkbox_collate_CheckedChanged);
542 group_box_copies.Controls.Add (chkbox_collate);
544 collate = new CollatePreview ();
545 collate.Location = new Point (6, 50);
546 collate.Size = new Size (100, 45);
547 group_box_copies.Controls.Add (collate);
552 printer_combo = new ComboBox ();
553 printer_combo.DropDownStyle = ComboBoxStyle.DropDownList;
554 printer_combo.Location = new Point (80, 32);
555 printer_combo.Width = 220;
556 printer_combo.SelectedIndexChanged += new EventHandler (OnPrinterSelectedIndexChanged);
558 default_printer_settings = new PrinterSettings ();
559 for (int i = 0; i < installed_printers.Count; i++) {
560 printer_combo.Items.Add (installed_printers[i]);
561 if (installed_printers[i] == default_printer_settings.PrinterName)
562 printer_combo.SelectedItem = installed_printers[i];
564 printer_combo.TabIndex = 11;
565 chkbox_print.TabIndex = 12;
566 group_box_prn.Controls.Add (printer_combo);
567 group_box_prn.Controls.Add (chkbox_print);
569 form.Size = new Size (450, 327); // 384
570 form.FormBorderStyle = FormBorderStyle.FixedDialog;
571 form.MaximizeBox = false;
572 group_box_prn.TabIndex = 10;
573 group_box_range.TabIndex = 20;
574 group_box_copies.TabIndex = 30;
575 accept_button.TabIndex = 40;
576 cancel_button.TabIndex = 50;
577 form.Controls.Add (group_box_prn);
578 form.Controls.Add (group_box_range);
579 form.Controls.Add (group_box_copies);
580 form.Controls.Add (accept_button);
581 form.Controls.Add (cancel_button);
582 form.ResumeLayout (false);
585 void OnPagesTextChanged (object sender, EventArgs args)
587 radio_pages.Checked = true;
590 private void OnPrinterSelectedIndexChanged (object sender, System.EventArgs e)
592 SetPrinterDetails ();
595 private void SetPrinterDetails ()
599 string printer, port = string.Empty, type = string.Empty;
600 string status = string.Empty, comment = string.Empty;
601 Type sysprn = Type.GetType ("System.Drawing.Printing.SysPrn, System.Drawing");
602 MethodInfo dlg_info = sysprn.GetMethod ("GetPrintDialogInfo", BindingFlags.Static | BindingFlags.NonPublic);
604 printer = (string) printer_combo.SelectedItem;
606 if (printer != null) {
607 object[] args = new object [5];
613 dlg_info.Invoke (null, args);
614 port = (string) args[1];
615 type = (string) args[2];
616 status = (string) args[3];
617 comment = (string) args[4];
620 label_status.Text = status;
621 label_type.Text = type;
622 label_where.Text = port;
623 label_comment.Text = comment;
625 accept_button.Enabled = true;
628 accept_button.Enabled = false;
632 private void chkbox_collate_CheckedChanged(object sender, EventArgs e) {
633 collate.Collate = chkbox_collate.Checked;
636 class CollatePreview : UserControl
638 private bool collate;
639 public bool Collate {
640 get { return collate;}
641 set { if (collate != value) {
650 public CollatePreview ()
652 font = new Font(FontFamily.GenericSansSerif, 10);
655 protected override void OnPaint(PaintEventArgs e)
658 DrawCollate (e.Graphics);
660 DrawNoCollate (e.Graphics);
665 void DrawCollate (Graphics g)
676 for (int i = 0; i < 2; i++) {
678 g.FillRectangle (Brushes.White, x3 + (i*18), y3, 18, 24);
679 ControlPaint.DrawBorder (g, new Rectangle (x3 + (i*18), y3, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
680 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x3 + (i*18) + 5, y3 + 5, StringFormat.GenericTypographic);
682 g.FillRectangle (Brushes.White, x2 + (i*18), y2, 18, 24);
683 ControlPaint.DrawBorder (g, new Rectangle (x2 + (i*18), y2, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
684 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x2 + (i*18) + 5, y2 + 5, StringFormat.GenericTypographic);
686 g.FillRectangle (Brushes.White, x1 + (i*18), y1, 18, 24);
687 ControlPaint.DrawBorder (g, new Rectangle (x1 + (i*18), y1, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
688 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x1 + (i*18) + 5, y1 + 5, StringFormat.GenericTypographic);
697 void DrawNoCollate (Graphics g)
705 for (int i = 0; i < 3; i++) {
707 g.FillRectangle (Brushes.White, x2 + (i*18), y2, 18, 24);
708 ControlPaint.DrawBorder (g, new Rectangle (x2 + (i*18), y2, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
709 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x2 + (i*18) + 5, y2 + 5, StringFormat.GenericTypographic);
711 g.FillRectangle (Brushes.White, x1 + (i*18), y1, 18, 24);
712 ControlPaint.DrawBorder (g, new Rectangle (x1 + (i*18), y1, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
713 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x1 + (i*18) + 5, y1 + 5, StringFormat.GenericTypographic);