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 = null;
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_where = new Label ();
435 label_where.AutoSize = true;
436 label_where.Location = new Point (80, 100);
437 group_box_prn.Controls.Add (label_where);
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);
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);
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);
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);
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);
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);
478 txtFrom = new TextBox ();
479 txtFrom.TabIndex = 25;
480 txtFrom.Location = new Point (120, 50);
482 txtFrom.TextChanged += new EventHandler (OnPagesTextChanged);
483 group_box_range.Controls.Add (txtFrom);
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);
492 txtTo = new TextBox ();
494 txtTo.Location = new Point (190, 50);
496 txtTo.TextChanged += new EventHandler (OnPagesTextChanged);
497 group_box_range.Controls.Add (txtTo);
499 chkbox_print = new CheckBox ();
500 chkbox_print.Location = new Point (305, 115);
501 chkbox_print.Text = "Print to fil&e";
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);
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);
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);
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);
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];
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);
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);
560 void OnPagesTextChanged (object sender, EventArgs args)
562 radio_pages.Checked = true;
565 private void OnPrinterSelectedIndexChanged (object sender, System.EventArgs e)
567 SetPrinterDetails ();
570 private void SetPrinterDetails ()
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);
579 printer = (string) printer_combo.SelectedItem;
581 if (printer != null) {
582 object[] args = new object [5];
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];
595 label_status.Text = status;
596 label_type.Text = type;
597 label_where.Text = port;
598 label_comment.Text = comment;
600 accept_button.Enabled = true;
603 accept_button.Enabled = false;