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 [Designer ("System.Windows.Forms.Design.PrintDialogDesigner, " + Consts.AssemblySystem_Design,
41 "System.ComponentModel.Design.IDesigner")]
42 [DefaultProperty("Document")]
43 public sealed class PrintDialog : CommonDialog {
44 PrintDocument document;
45 bool allow_current_page;
46 bool allow_print_to_file;
48 bool allow_some_pages;
52 PrinterSettings current_settings;
54 private Button cancel_button;
55 private Button accept_button;
56 private Button help_button;
57 private ComboBox printer_combo;
58 private RadioButton radio_all;
59 private RadioButton radio_pages;
60 private RadioButton radio_sel;
61 private PrinterSettings.StringCollection installed_printers;
62 private PrinterSettings default_printer_settings;
63 private TextBox txtFrom;
64 private TextBox txtTo;
65 private Label labelTo;
66 private Label labelFrom;
67 private CheckBox chkbox_print;
68 private NumericUpDown updown_copies;
69 private CheckBox chkbox_collate;
70 private Label label_status;
71 private Label label_type;
72 private Label label_where;
73 private Label label_comment;
74 private CollatePreview collate;
75 private bool use_ex_dialog;
79 form = new DialogForm (this);
81 installed_printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
84 CreateFormControls ();
88 public override void Reset ()
90 current_settings = null;
91 AllowPrintToFile = true;
92 AllowSelection = false;
93 AllowSomePages = false;
99 [DefaultValue (false)]
100 public bool AllowCurrentPage {
102 return allow_current_page;
106 allow_current_page = value;
107 radio_pages.Enabled = value;
112 public bool AllowPrintToFile {
114 return allow_print_to_file;
118 allow_print_to_file = value;
119 chkbox_print.Enabled = value;
123 [DefaultValue(false)]
124 public bool AllowSelection {
126 return allow_selection;
130 allow_selection = value;
131 radio_sel.Enabled = value;
135 [DefaultValue(false)]
136 public bool AllowSomePages {
138 return allow_some_pages;
142 allow_some_pages = value;
143 radio_pages.Enabled = value;
144 txtFrom.Enabled = value;
145 txtTo.Enabled = value;
146 labelTo.Enabled = value;
147 labelFrom.Enabled = value;
149 if (PrinterSettings != null) {
150 txtFrom.Text = PrinterSettings.FromPage.ToString ();
151 txtTo.Text = PrinterSettings.ToPage.ToString ();
157 public PrintDocument Document {
164 current_settings = (value == null) ? new PrinterSettings () : value.PrinterSettings;
170 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
171 public PrinterSettings PrinterSettings {
173 if (current_settings == null)
174 current_settings = new PrinterSettings ();
175 return current_settings;
179 if (value != null && value == current_settings)
182 current_settings = (value == null) ? new PrinterSettings () : value;
187 [DefaultValue(false)]
188 public bool PrintToFile {
190 return print_to_file;
194 print_to_file = value;
199 public bool ShowNetwork {
205 show_network = value;
209 [DefaultValue(false)]
210 public bool ShowHelp {
221 [MonoTODO ("Stub, not implemented, will always use default dialog")]
222 [DefaultValue (false)]
223 public bool UseEXDialog {
224 get { return use_ex_dialog; }
225 set { use_ex_dialog = value; }
228 protected override bool RunDialog (IntPtr hwndOwner)
230 if (allow_some_pages && PrinterSettings.FromPage > PrinterSettings.ToPage)
231 throw new ArgumentException ("FromPage out of range");
233 if (allow_some_pages) {
234 txtFrom.Text = PrinterSettings.FromPage.ToString ();
235 txtTo.Text = PrinterSettings.ToPage.ToString ();
238 if (PrinterSettings.PrintRange == PrintRange.SomePages && allow_some_pages)
239 radio_pages.Checked = true;
240 else if (PrinterSettings.PrintRange == PrintRange.Selection && allow_selection)
241 radio_sel.Checked = true;
243 radio_all.Checked = true;
245 updown_copies.Value = PrinterSettings.Copies == 0 ? 1 : (int) PrinterSettings.Copies;
246 chkbox_collate.Checked = PrinterSettings.Collate;
247 chkbox_collate.Enabled = (updown_copies.Value > 1) ? true : false;
253 SetPrinterDetails ();
258 private void OnClickCancelButton (object sender, EventArgs e)
260 form.DialogResult = DialogResult.Cancel;
263 void ShowErrorMessage (string message, Control control_to_focus)
265 MessageBox.Show (message, "Print", MessageBoxButtons.OK, MessageBoxIcon.Warning);
266 if (control_to_focus != null)
267 control_to_focus.Focus ();
270 private void OnClickOkButton (object sender, EventArgs e)
272 if (updown_copies.Text.Length < 1) {
273 ShowErrorMessage ("The 'Copies' value cannot be empty and must be a positive value.",
278 int from = -1, to = -1;
280 if (allow_some_pages && radio_pages.Checked) {
281 if (txtFrom.Text.Length < 1) {
282 ShowErrorMessage ("The 'From' value cannot be empty and must be a positive value.",
288 from = Int32.Parse (txtFrom.Text);
289 to = Int32.Parse (txtTo.Text);
292 ShowErrorMessage ("From/To values should be numeric", txtFrom);
297 ShowErrorMessage ("'From' value cannot be greater than 'To' value.", txtFrom);
301 if (to < PrinterSettings.MinimumPage || to > PrinterSettings.MaximumPage) {
302 ShowErrorMessage ("'To' value is not within the page range\n" +
303 "Enter a number between " + PrinterSettings.MinimumPage +
304 " and " + PrinterSettings.MaximumPage + ".", txtTo);
308 if (from < PrinterSettings.MinimumPage || from > PrinterSettings.MaximumPage) {
309 ShowErrorMessage ("'From' value is not within the page range\n" +
310 "Enter a number between " + PrinterSettings.MinimumPage +
311 " and " + PrinterSettings.MaximumPage + ".", txtFrom);
316 if (radio_all.Checked == true)
317 PrinterSettings.PrintRange = PrintRange.AllPages;
318 else if (radio_pages.Checked == true)
319 PrinterSettings.PrintRange = PrintRange.SomePages;
321 PrinterSettings.PrintRange = PrintRange.Selection;
323 PrinterSettings.Copies = (short) updown_copies.Value;
324 if (PrinterSettings.PrintRange == PrintRange.SomePages) {
325 PrinterSettings.FromPage = from;
326 PrinterSettings.ToPage = to;
328 PrinterSettings.Collate = chkbox_collate.Checked;
330 if (allow_print_to_file) {
331 PrinterSettings.PrintToFile = chkbox_print.Checked;
334 form.DialogResult = DialogResult.OK;
336 if (printer_combo.SelectedItem != null)
337 PrinterSettings.PrinterName = (string) printer_combo.SelectedItem;
339 if (document != null) {
340 document.PrintController = new PrintControllerWithStatusDialog (document.PrintController);
341 document.PrinterSettings = PrinterSettings;
345 private void ShowHelpButton ()
347 if (help_button == null) {
348 help_button = new Button ();
349 help_button.TabIndex = 60;
351 help_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
352 help_button.FlatStyle = FlatStyle.System;
353 help_button.Location = new Point (20, 270);
354 help_button.Text = "&Help";
355 help_button.FlatStyle = FlatStyle.System;
356 form.Controls.Add (help_button);
359 help_button.Visible = show_help;
362 private void OnUpDownValueChanged (object sender, System.EventArgs e)
364 chkbox_collate.Enabled = (updown_copies.Value > 1) ? true : false;
367 void OnPagesCheckedChanged (object obj, EventArgs args)
369 if (radio_pages.Checked && !txtTo.Focused)
373 private void CreateFormControls ()
375 form.SuspendLayout ();
377 GroupBox group_box_prn = new GroupBox ();
378 group_box_prn.Location = new Point (10, 8);
379 group_box_prn.Text = "Printer";
380 group_box_prn.Size = new Size (420, 145);
382 GroupBox group_box_range = new GroupBox ();
383 group_box_range.Location = new Point (10, 155);
384 group_box_range.Text = "Print range";
385 group_box_range.Size = new Size (240, 100);
387 GroupBox group_box_copies = new GroupBox ();
388 group_box_copies.Location = new Point (265, 155);
389 group_box_copies.Text = "Copies";
390 group_box_copies.Size = new Size (165, 100);
393 accept_button = new Button ();
394 form.AcceptButton = accept_button;
395 accept_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
396 accept_button.FlatStyle = FlatStyle.System;
397 accept_button.Location = new Point (265, 270);
398 accept_button.Text = "OK";
399 accept_button.FlatStyle = FlatStyle.System;
400 accept_button.Click += new EventHandler (OnClickOkButton);
403 cancel_button = new Button ();
404 form.CancelButton = cancel_button;
405 cancel_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
406 cancel_button.FlatStyle = FlatStyle.System;
407 cancel_button.Location = new Point (350, 270);
408 cancel_button.Text = "Cancel";
409 cancel_button.FlatStyle = FlatStyle.System;
410 cancel_button.Click += new EventHandler (OnClickCancelButton);
413 Label label = new Label ();
414 label.AutoSize = true;
415 label.Text = "&Name:";
416 label.Location = new Point (20, 33);
417 group_box_prn.Controls.Add (label);
419 label = new Label ();
420 label.Text = "Status:";
421 label.AutoSize = true;
422 label.Location = new Point (20, 60);
423 group_box_prn.Controls.Add (label);
425 label_status = new Label ();
426 label_status.AutoSize = true;
427 label_status.Location = new Point (80, 60);
428 group_box_prn.Controls.Add (label_status);
430 label = new Label ();
431 label.Text = "Type:";
432 label.AutoSize = true;
433 label.Location = new Point (20, 80);
434 group_box_prn.Controls.Add (label);
436 label_type = new Label ();
437 label_type.AutoSize = true;
438 label_type.Location = new Point (80, 80);
439 group_box_prn.Controls.Add (label_type);
441 label = new Label ();
442 label.Text = "Where:";
443 label.AutoSize = true;
444 label.Location = new Point (20, 100);
445 group_box_prn.Controls.Add (label);
447 label_where = new Label ();
448 label_where.AutoSize = true;
449 label_where.Location = new Point (80, 100);
450 group_box_prn.Controls.Add (label_where);
452 label = new Label ();
453 label.Text = "Comment:";
454 label.AutoSize = true;
455 label.Location = new Point (20, 120);
456 group_box_prn.Controls.Add (label);
458 label_comment = new Label ();
459 label_comment.AutoSize = true;
460 label_comment.Location = new Point (80, 120);
461 group_box_prn.Controls.Add (label_comment);
463 radio_all = new RadioButton ();
464 radio_all.TabIndex = 21;
465 radio_all.Location = new Point (20, 20);
466 radio_all.Text = "&All";
467 radio_all.Checked = true;
468 group_box_range.Controls.Add (radio_all);
470 radio_pages = new RadioButton ();
471 radio_pages.TabIndex = 22;
472 radio_pages.Location = new Point (20, 46);
473 radio_pages.Text = "Pa&ges";
474 radio_pages.Width = 60;
475 radio_pages.CheckedChanged += new EventHandler (OnPagesCheckedChanged);
476 group_box_range.Controls.Add (radio_pages);
478 radio_sel = new RadioButton ();
479 radio_sel.TabIndex = 23;
480 radio_sel.Location = new Point (20, 72);
481 radio_sel.Text = "&Selection";
482 group_box_range.Controls.Add (radio_sel);
484 labelFrom = new Label ();
485 labelFrom.Text = "&from:";
486 labelFrom.TabIndex = 24;
487 labelFrom.AutoSize = true;
488 labelFrom.Location = new Point (80, 50);
489 group_box_range.Controls.Add (labelFrom);
491 txtFrom = new TextBox ();
492 txtFrom.TabIndex = 25;
493 txtFrom.Location = new Point (120, 50);
495 txtFrom.TextChanged += new EventHandler (OnPagesTextChanged);
496 group_box_range.Controls.Add (txtFrom);
498 labelTo = new Label ();
499 labelTo.Text = "&to:";
500 labelTo.TabIndex = 26;
501 labelTo.AutoSize = true;
502 labelTo.Location = new Point (170, 50);
503 group_box_range.Controls.Add (labelTo);
505 txtTo = new TextBox ();
507 txtTo.Location = new Point (190, 50);
509 txtTo.TextChanged += new EventHandler (OnPagesTextChanged);
510 group_box_range.Controls.Add (txtTo);
512 chkbox_print = new CheckBox ();
513 chkbox_print.Location = new Point (305, 115);
514 chkbox_print.Text = "Print to fil&e";
517 updown_copies = new NumericUpDown ();
518 updown_copies.TabIndex = 31;
519 updown_copies.Location = new Point (105, 18);
520 updown_copies.Minimum = 1;
521 group_box_copies.Controls.Add (updown_copies);
522 updown_copies.ValueChanged += new System.EventHandler (OnUpDownValueChanged);
523 updown_copies.Size = new System.Drawing.Size (40, 20);
525 label = new Label ();
526 label.Text = "Number of &copies:";
527 label.AutoSize = true;
528 label.Location = new Point (10, 20);
529 group_box_copies.Controls.Add (label);
531 chkbox_collate = new CheckBox ();
532 chkbox_collate.TabIndex = 32;
533 chkbox_collate.Location = new Point (105, 55);
534 chkbox_collate.Text = "C&ollate";
535 chkbox_collate.Width = 58;
536 chkbox_collate.CheckedChanged += new EventHandler(chkbox_collate_CheckedChanged);
538 group_box_copies.Controls.Add (chkbox_collate);
540 collate = new CollatePreview ();
541 collate.Location = new Point (6, 50);
542 collate.Size = new Size (100, 45);
543 group_box_copies.Controls.Add (collate);
548 printer_combo = new ComboBox ();
549 printer_combo.DropDownStyle = ComboBoxStyle.DropDownList;
550 printer_combo.Location = new Point (80, 32);
551 printer_combo.Width = 220;
552 printer_combo.SelectedIndexChanged += new EventHandler (OnPrinterSelectedIndexChanged);
554 default_printer_settings = new PrinterSettings ();
555 for (int i = 0; i < installed_printers.Count; i++) {
556 printer_combo.Items.Add (installed_printers[i]);
557 if (installed_printers[i] == default_printer_settings.PrinterName)
558 printer_combo.SelectedItem = installed_printers[i];
560 printer_combo.TabIndex = 11;
561 chkbox_print.TabIndex = 12;
562 group_box_prn.Controls.Add (printer_combo);
563 group_box_prn.Controls.Add (chkbox_print);
565 form.Size = new Size (450, 327); // 384
566 form.FormBorderStyle = FormBorderStyle.FixedDialog;
567 form.MaximizeBox = false;
568 group_box_prn.TabIndex = 10;
569 group_box_range.TabIndex = 20;
570 group_box_copies.TabIndex = 30;
571 accept_button.TabIndex = 40;
572 cancel_button.TabIndex = 50;
573 form.Controls.Add (group_box_prn);
574 form.Controls.Add (group_box_range);
575 form.Controls.Add (group_box_copies);
576 form.Controls.Add (accept_button);
577 form.Controls.Add (cancel_button);
578 form.ResumeLayout (false);
581 void OnPagesTextChanged (object sender, EventArgs args)
583 radio_pages.Checked = true;
586 private void OnPrinterSelectedIndexChanged (object sender, System.EventArgs e)
588 SetPrinterDetails ();
591 private void SetPrinterDetails ()
595 string printer, port = string.Empty, type = string.Empty;
596 string status = string.Empty, comment = string.Empty;
597 Type sysprn = Type.GetType ("System.Drawing.Printing.SysPrn, System.Drawing");
598 MethodInfo dlg_info = sysprn.GetMethod ("GetPrintDialogInfo", BindingFlags.Static | BindingFlags.NonPublic);
600 printer = (string) printer_combo.SelectedItem;
602 if (printer != null) {
603 object[] args = new object [5];
609 dlg_info.Invoke (null, args);
610 port = (string) args[1];
611 type = (string) args[2];
612 status = (string) args[3];
613 comment = (string) args[4];
616 label_status.Text = status;
617 label_type.Text = type;
618 label_where.Text = port;
619 label_comment.Text = comment;
621 accept_button.Enabled = true;
624 accept_button.Enabled = false;
628 private void chkbox_collate_CheckedChanged(object sender, EventArgs e) {
629 collate.Collate = chkbox_collate.Checked;
632 class CollatePreview : UserControl
634 private bool collate;
635 public bool Collate {
636 get { return collate;}
637 set { if (collate != value) {
646 public CollatePreview ()
648 font = new Font(FontFamily.GenericSansSerif, 10);
651 protected override void OnPaint(PaintEventArgs e)
654 DrawCollate (e.Graphics);
656 DrawNoCollate (e.Graphics);
661 void DrawCollate (Graphics g)
672 for (int i = 0; i < 2; i++) {
674 g.FillRectangle (Brushes.White, x3 + (i*18), y3, 18, 24);
675 ControlPaint.DrawBorder (g, new Rectangle (x3 + (i*18), y3, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
676 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x3 + (i*18) + 5, y3 + 5, StringFormat.GenericTypographic);
678 g.FillRectangle (Brushes.White, x2 + (i*18), y2, 18, 24);
679 ControlPaint.DrawBorder (g, new Rectangle (x2 + (i*18), y2, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
680 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x2 + (i*18) + 5, y2 + 5, StringFormat.GenericTypographic);
682 g.FillRectangle (Brushes.White, x1 + (i*18), y1, 18, 24);
683 ControlPaint.DrawBorder (g, new Rectangle (x1 + (i*18), y1, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
684 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x1 + (i*18) + 5, y1 + 5, StringFormat.GenericTypographic);
693 void DrawNoCollate (Graphics g)
701 for (int i = 0; i < 3; i++) {
703 g.FillRectangle (Brushes.White, x2 + (i*18), y2, 18, 24);
704 ControlPaint.DrawBorder (g, new Rectangle (x2 + (i*18), y2, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
705 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x2 + (i*18) + 5, y2 + 5, StringFormat.GenericTypographic);
707 g.FillRectangle (Brushes.White, x1 + (i*18), y1, 18, 24);
708 ControlPaint.DrawBorder (g, new Rectangle (x1 + (i*18), y1, 18, 24), SystemColors.ControlDark, ButtonBorderStyle.Solid);
709 g.DrawString ((i+1).ToString(), font, SystemBrushes.ControlDarkDark, x1 + (i*18) + 5, y1 + 5, StringFormat.GenericTypographic);