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 PrinterSettings printer_settings;
44 bool allow_current_page;
45 bool allow_print_to_file;
47 bool allow_some_pages;
51 PrinterSettings current_settings;
53 private Button cancel_button;
54 private Button accept_button;
55 private Button help_button;
56 private ComboBox printer_combo;
57 private RadioButton radio_all;
58 private RadioButton radio_pages;
59 private RadioButton radio_sel;
60 private PrinterSettings.StringCollection installed_printers;
61 private PrinterSettings default_printer_settings;
62 private TextBox txtFrom;
63 private TextBox txtTo;
64 private Label labelTo;
65 private Label labelFrom;
66 private CheckBox chkbox_print;
67 private NumericUpDown updown_copies;
68 private CheckBox chkbox_collate;
69 private Label label_status;
70 private Label label_type;
71 private Label label_where;
72 private Label label_comment;
77 installed_printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
80 CreateFormControls ();
84 public override void Reset ()
86 current_settings = null;
87 AllowPrintToFile = true;
88 AllowSelection = false;
89 AllowSomePages = false;
96 public bool AllowCurrentPage {
98 return allow_current_page;
102 allow_current_page = value;
103 radio_pages.Enabled = value;
109 public bool AllowPrintToFile {
111 return allow_print_to_file;
115 allow_print_to_file = value;
116 chkbox_print.Enabled = value;
120 [DefaultValue(false)]
121 public bool AllowSelection {
123 return allow_selection;
127 allow_selection = value;
128 radio_sel.Enabled = value;
132 [DefaultValue(false)]
133 public bool AllowSomePages {
135 return allow_some_pages;
139 allow_some_pages = value;
140 radio_pages.Enabled = value;
141 txtFrom.Enabled = value;
142 txtTo.Enabled = value;
143 labelTo.Enabled = value;
144 labelFrom.Enabled = value;
146 if (current_settings != null) {
147 txtFrom.Text = current_settings.FromPage.ToString ();
148 txtTo.Text = current_settings.ToPage.ToString ();
154 public PrintDocument Document {
164 current_settings = value.PrinterSettings;
165 printer_settings = null;
171 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
172 public PrinterSettings PrinterSettings {
174 return printer_settings;
178 current_settings = printer_settings = value;
183 [DefaultValue(false)]
184 public bool PrintToFile {
186 return print_to_file;
190 print_to_file = value;
195 public bool ShowNetwork {
201 show_network = value;
205 [DefaultValue(false)]
206 public bool ShowHelp {
217 protected override bool RunDialog (IntPtr hwnd)
219 if (Document == null && PrinterSettings == null)
220 throw new ArgumentException ("PrintDialog needs a Docouement or PrinterSettings object to display");
222 if (allow_some_pages && current_settings.FromPage > current_settings.ToPage)
223 throw new ArgumentException ("FromPage out of range");
225 if (allow_some_pages && current_settings != null) {
226 txtFrom.Text = current_settings.FromPage.ToString ();
227 txtTo.Text = current_settings.ToPage.ToString ();
230 updown_copies.Value = current_settings.Copies;
231 chkbox_collate.Enabled = (updown_copies.Value > 0) ? true : false;
237 SetPrinterDetails ();
242 private void OnClickCancelButton (object sender, EventArgs e)
244 form.DialogResult = DialogResult.Cancel;
247 private void OnClickOkButton (object sender, EventArgs e)
252 from = Int32.Parse (txtFrom.Text);
253 to = Int32.Parse (txtTo.Text);
257 MessageBox.Show ("From/To values should be numeric", "Print",
258 MessageBoxButtons.OK, MessageBoxIcon.Warning);
262 if (allow_some_pages) {
264 MessageBox.Show ("From value cannot be greater than To value", "Print",
265 MessageBoxButtons.OK, MessageBoxIcon.Warning);
269 if (to < current_settings.MinimumPage || to > current_settings.MaximumPage) {
270 MessageBox.Show ("To value is not within the page range\n" +
271 "Enter a number between " + current_settings.MinimumPage +
272 " and " + current_settings.MaximumPage, "Print",
273 MessageBoxButtons.OK, MessageBoxIcon.Warning);
277 if (from < current_settings.MinimumPage || from > current_settings.MaximumPage) {
279 MessageBox.Show ("From value is not within the page range\n" +
280 "Enter a number between " + current_settings.MinimumPage +
281 " and " + current_settings.MaximumPage, "Print",
282 MessageBoxButtons.OK, MessageBoxIcon.Warning);
287 if (radio_all.Checked == true) {
288 current_settings.PrintRange = PrintRange.AllPages;
290 if (radio_pages.Checked == true) {
291 current_settings.PrintRange = PrintRange.SomePages;
293 if (radio_sel.Checked == true) {
294 current_settings.PrintRange = PrintRange.Selection;
299 current_settings.Copies = (short) updown_copies.Value;
300 current_settings.FromPage = from;
301 current_settings.ToPage = to;
302 current_settings.Collate = chkbox_collate.Checked;
304 if (allow_print_to_file) {
305 current_settings.PrintToFile = chkbox_print.Checked;
308 form.DialogResult = DialogResult.OK;
310 if (printer_combo.SelectedItem != null)
311 current_settings.PrinterName = (string) printer_combo.SelectedItem;
314 private void ShowHelpButton ()
316 if (help_button == null) {
317 help_button = new Button ();
318 help_button.TabIndex = 60;
320 help_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
321 help_button.FlatStyle = FlatStyle.System;
322 help_button.Location = new Point (20, 270);
323 help_button.Text = "&Help";
324 help_button.FlatStyle = FlatStyle.System;
325 form.Controls.Add (help_button);
328 help_button.Visible = show_help;
331 private void OnUpDownValueChanged (object sender, System.EventArgs e)
333 chkbox_collate.Enabled = (updown_copies.Value > 0) ? true : false;
336 private void CreateFormControls ()
338 form.SuspendLayout ();
340 GroupBox group_box_prn = new GroupBox ();
341 group_box_prn.Location = new Point (10, 8);
342 group_box_prn.Text = "Printer";
343 group_box_prn.Size = new Size (420, 145);
345 GroupBox group_box_range = new GroupBox ();
346 group_box_range.Location = new Point (10, 155);
347 group_box_range.Text = "Print range";
348 group_box_range.Size = new Size (240, 100);
350 GroupBox group_box_copies = new GroupBox ();
351 group_box_copies.Location = new Point (265, 155);
352 group_box_copies.Text = "Copies";
353 group_box_copies.Size = new Size (165, 100);
356 accept_button = new Button ();
357 form.AcceptButton = accept_button;
358 accept_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
359 accept_button.FlatStyle = FlatStyle.System;
360 accept_button.Location = new Point (265, 270);
361 accept_button.Text = "OK";
362 accept_button.FlatStyle = FlatStyle.System;
363 accept_button.Click += new EventHandler (OnClickOkButton);
366 cancel_button = new Button ();
367 form.CancelButton = cancel_button;
368 cancel_button.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
369 cancel_button.FlatStyle = FlatStyle.System;
370 cancel_button.Location = new Point (350, 270);
371 cancel_button.Text = "Cancel";
372 cancel_button.FlatStyle = FlatStyle.System;
373 cancel_button.Click += new EventHandler (OnClickCancelButton);
376 Label label = new Label ();
377 label.AutoSize = true;
378 label.Text = "&Name:";
379 label.Location = new Point (20, 33);
380 group_box_prn.Controls.Add (label);
382 label = new Label ();
383 label.Text = "Status:";
384 label.AutoSize = true;
385 label.Location = new Point (20, 60);
386 group_box_prn.Controls.Add (label);
388 label_status = new Label ();
389 label_status.AutoSize = true;
390 label_status.Location = new Point (80, 60);
391 group_box_prn.Controls.Add (label_status);
393 label = new Label ();
394 label.Text = "Type:";
395 label.AutoSize = true;
396 label.Location = new Point (20, 80);
397 group_box_prn.Controls.Add (label);
399 label_type = new Label ();
400 label_type.AutoSize = true;
401 label_type.Location = new Point (80, 80);
402 group_box_prn.Controls.Add (label_type);
404 label = new Label ();
405 label.Text = "Where:";
406 label.AutoSize = true;
407 label.Location = new Point (20, 100);
408 group_box_prn.Controls.Add (label);
410 label = new Label ();
411 label.Text = "Comment:";
412 label.AutoSize = true;
413 label.Location = new Point (20, 120);
414 group_box_prn.Controls.Add (label);
416 label_comment = new Label ();
417 label_comment.AutoSize = true;
418 label_comment.Location = new Point (80, 120);
420 radio_all = new RadioButton ();
421 radio_all.TabIndex = 21;
422 radio_all.Location = new Point (20, 20);
423 radio_all.Text = "&All";
424 radio_all.Checked = true;
425 group_box_range.Controls.Add (radio_all);
427 radio_pages = new RadioButton ();
428 radio_pages.TabIndex = 22;
429 radio_pages.Location = new Point (20, 46);
430 radio_pages.Text = "Pa&ges";
431 radio_pages.Width = 60;
432 group_box_range.Controls.Add (radio_pages);
434 radio_sel = new RadioButton ();
435 radio_sel.TabIndex = 23;
436 radio_sel.Location = new Point (20, 72);
437 radio_sel.Text = "&Selection";
438 group_box_range.Controls.Add (radio_sel);
440 labelFrom = new Label ();
441 labelFrom.Text = "&from:";
442 labelFrom.AutoSize = true;
443 labelFrom.Location = new Point (80, 50);
444 group_box_range.Controls.Add (labelFrom);
446 txtFrom = new TextBox ();
447 txtFrom.TabIndex = 24;
448 txtFrom.Location = new Point (120, 50);
450 group_box_range.Controls.Add (txtFrom);
452 labelTo = new Label ();
453 labelTo.Text = "&to:";
454 labelTo.AutoSize = true;
455 labelTo.Location = new Point (170, 50);
456 group_box_range.Controls.Add (labelTo);
458 txtTo = new TextBox ();
460 txtTo.Location = new Point (190, 50);
462 group_box_range.Controls.Add (txtTo);
464 chkbox_print = new CheckBox ();
465 chkbox_print.Location = new Point (305, 115);
466 chkbox_print.Text = "Print to fil&e";
468 label = new Label ();
469 label.Text = "Number of &copies:";
470 label.AutoSize = true;
471 label.Location = new Point (20, 20);
472 group_box_copies.Controls.Add (label);
474 updown_copies = new NumericUpDown ();
475 updown_copies.TabIndex = 31;
476 updown_copies.Location = new Point (120, 20);
477 group_box_copies.Controls.Add (updown_copies);
478 updown_copies.ValueChanged += new System.EventHandler (OnUpDownValueChanged);
479 updown_copies.Size = new System.Drawing.Size (40, 20);
481 chkbox_collate = new CheckBox ();
482 chkbox_collate.TabIndex = 32;
483 chkbox_collate.Location = new Point (20, 40);
484 chkbox_collate.Text = "C&ollate";
485 chkbox_collate.Width = 80;
486 group_box_copies.Controls.Add (chkbox_collate);
491 printer_combo = new ComboBox ();
492 printer_combo.DropDownStyle = ComboBoxStyle.DropDownList;
493 printer_combo.Location = new Point (80, 32);
494 printer_combo.Width = 220;
495 printer_combo.SelectedIndexChanged += new EventHandler (OnPrinterSelectedIndexChanged);
497 default_printer_settings = new PrinterSettings ();
498 for (int i = 0; i < installed_printers.Count; i++) {
499 printer_combo.Items.Add (installed_printers[i]);
500 if (installed_printers[i] == default_printer_settings.PrinterName)
501 printer_combo.SelectedItem = installed_printers[i];
503 printer_combo.TabIndex = 11;
504 chkbox_print.TabIndex = 12;
505 group_box_prn.Controls.Add (printer_combo);
506 group_box_prn.Controls.Add (chkbox_print);
508 form.Size = new Size (450, 327); // 384
509 form.FormBorderStyle = FormBorderStyle.FixedDialog;
510 form.MaximizeBox = false;
511 group_box_prn.TabIndex = 10;
512 group_box_range.TabIndex = 20;
513 group_box_copies.TabIndex = 30;
514 accept_button.TabIndex = 40;
515 cancel_button.TabIndex = 50;
516 form.Controls.Add (group_box_prn);
517 form.Controls.Add (group_box_range);
518 form.Controls.Add (group_box_copies);
519 form.Controls.Add (accept_button);
520 form.Controls.Add (cancel_button);
521 form.ResumeLayout (false);
524 private void OnPrinterSelectedIndexChanged (object sender, System.EventArgs e)
526 SetPrinterDetails ();
529 private void SetPrinterDetails ()
533 string printer, port = string.Empty, type = string.Empty;
534 string status = string.Empty, comment = string.Empty;
535 Type sysprn = Type.GetType ("System.Drawing.Printing.SysPrn, System.Drawing");
536 MethodInfo dlg_info = sysprn.GetMethod ("GetPrintDialogInfo", BindingFlags.Static | BindingFlags.NonPublic);
538 printer = (string) printer_combo.SelectedItem;
540 if (printer != null) {
541 object[] args = new object [5];
547 dlg_info.Invoke (null, args);
548 port = (string) args[1];
549 type = (string) args[2];
550 status = (string) args[3];
551 comment = (string) args[4];
554 label_status.Text = status;
555 label_type.Text = type;
556 label_where.Text = port;
557 label_comment.Text = comment;