flush
[mcs.git] / class / System.Drawing / System.Drawing.Printing / PrinterSettings.cs
blobd1c0bad721100af073b16eebc3dc372d3684589f
1 //
2 // System.Drawing.PrinterSettings.cs
3 //
4 // Authors:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Herve Poussineau (hpoussineau@fr.st)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing.Imaging;
38 namespace System.Drawing.Printing
40 [Serializable]
41 #if ! NET_2_0
42 [ComVisible(false)]
43 #endif
44 public class PrinterSettings : ICloneable
46 private string printer_name;
47 private string print_filename;
48 private short copies;
49 private int maximum_page;
50 private int minimum_page;
51 private int from_page;
52 private int to_page;
53 private bool collate;
54 private PrintRange print_range;
55 internal int maximum_copies;
56 internal bool can_duplex;
57 internal bool supports_color;
58 internal int landscape_angle;
59 private bool print_tofile;
60 internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
61 internal PrinterSettings.PaperSizeCollection paper_sizes;
62 internal PrinterSettings.PaperSourceCollection paper_sources;
63 private PageSettings default_pagesettings;
64 private Duplex duplex;
65 internal bool is_plotter;
66 private PrintingServices printing_services;
68 internal NameValueCollection printer_capabilities; // this stores a list of all the printer options. Used only in cups, but might come in handy on win too.
69 public PrinterSettings() : this (SysPrn.CreatePrintingService ())
73 internal PrinterSettings (PrintingServices printing_services)
75 this.printing_services = printing_services;
76 printer_name = printing_services.DefaultPrinter;
77 ResetToDefaults ();
78 printing_services.LoadPrinterSettings (printer_name, this);
81 private void ResetToDefaults ()
83 printer_resolutions = null;
84 paper_sizes = null;
85 paper_sources = null;
86 default_pagesettings = null;
87 maximum_page = 9999;
88 copies = 1;
89 collate = true;
92 //properties
94 public bool CanDuplex
96 get { return can_duplex; }
99 public bool Collate
101 get { return collate; }
102 set { collate = value; }
105 public short Copies
107 get { return copies; }
108 set {
109 if (value < 0)
110 throw new ArgumentException ("The value of the Copies property is less than zero.");
112 copies = value;
116 public PageSettings DefaultPageSettings
118 get {
119 if (default_pagesettings == null) {
120 default_pagesettings = new PageSettings (this,
121 SupportsColor,
122 false,
123 // Real defaults are set by LoadPrinterSettings
124 new PaperSize("A4", 827, 1169),
125 new PaperSource("Tray", PaperSourceKind.FormSource),
126 new PrinterResolution(200, 200, PrinterResolutionKind.Medium));
129 return default_pagesettings;
133 public Duplex Duplex
135 get { return this.duplex; }
136 set { this.duplex = value; }
139 public int FromPage
141 get { return from_page; }
142 set {
143 if (value < 0)
144 throw new ArgumentException ("The value of the FromPage property is less than zero");
146 from_page = value;
150 public static PrinterSettings.StringCollection InstalledPrinters
152 get { return SysPrn.GlobalService.InstalledPrinters; }
155 public bool IsDefaultPrinter
157 get { return (printer_name == printing_services.DefaultPrinter); }
160 public bool IsPlotter
162 get { return is_plotter; }
165 public bool IsValid
167 get { return printing_services.IsPrinterValid (this.printer_name); }
170 public int LandscapeAngle
172 get { return landscape_angle; }
175 public int MaximumCopies
177 get { return maximum_copies; }
180 public int MaximumPage
182 get { return maximum_page; }
183 set {
184 // This not documented but behaves like MinimumPage
185 if (value < 0)
186 throw new ArgumentException ("The value of the MaximumPage property is less than zero");
188 maximum_page = value;
192 public int MinimumPage
194 get { return minimum_page; }
195 set {
196 if (value < 0)
197 throw new ArgumentException ("The value of the MaximumPage property is less than zero");
199 minimum_page = value;
203 public PrinterSettings.PaperSizeCollection PaperSizes
205 get {
206 if (!this.IsValid)
207 throw new InvalidPrinterException(this);
209 return paper_sizes;
213 public PrinterSettings.PaperSourceCollection PaperSources
215 get {
216 if (!this.IsValid)
217 throw new InvalidPrinterException(this);
219 return paper_sources;
222 #if NET_2_0
223 public
224 #else
225 internal
226 #endif
227 string PrintFileName
229 get { return print_filename; }
230 set { print_filename = value; }
232 public string PrinterName
234 get { return printer_name; }
235 set {
236 if (printer_name == value)
237 return;
239 printer_name = value;
240 printing_services.LoadPrinterSettings (printer_name, this);
244 public PrinterSettings.PrinterResolutionCollection PrinterResolutions
246 get {
247 if (!this.IsValid)
248 throw new InvalidPrinterException(this);
250 if (printer_resolutions == null) {
251 printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
252 printing_services.LoadPrinterResolutions (printer_name, this);
255 return printer_resolutions;
259 public PrintRange PrintRange
261 get { return print_range; }
262 set {
263 if (value != PrintRange.AllPages && value != PrintRange.Selection &&
264 value != PrintRange.SomePages)
265 throw new InvalidEnumArgumentException ("The value of the PrintRange property is not one of the PrintRange values");
267 print_range = value;
271 public bool PrintToFile
273 get { return print_tofile; }
274 set { print_tofile = value; }
277 public bool SupportsColor
279 get { return supports_color; }
282 public int ToPage
284 get { return to_page; }
285 set {
286 if (value < 0)
287 throw new ArgumentException ("The value of the ToPage property is less than zero");
289 to_page = value;
293 internal NameValueCollection PrinterCapabilities {
294 get {
295 if (this.printer_capabilities == null)
296 this.printer_capabilities = new NameValueCollection();
297 return this.printer_capabilities;
301 //methods
302 public object Clone ()
304 PrinterSettings ps = new PrinterSettings (printing_services);
305 return ps;
308 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
309 public Graphics CreateMeasurementGraphics()
311 throw new NotImplementedException();
313 #if NET_2_0
314 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
315 public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)
317 throw new NotImplementedException();
320 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
321 public Graphics CreateMeasurementGraphics(PageSettings pageSettings)
323 throw new NotImplementedException();
326 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
327 public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)
329 throw new NotImplementedException();
331 #endif
333 [MonoTODO("PrinterSettings.GetHdevmode")]
334 public IntPtr GetHdevmode()
336 throw new NotImplementedException();
339 [MonoTODO("PrinterSettings.GetHdevmode")]
340 public IntPtr GetHdevmode(PageSettings pageSettings)
342 throw new NotImplementedException();
345 [MonoTODO("PrinterSettings.GetHdevname")]
346 public IntPtr GetHdevnames()
348 throw new NotImplementedException();
351 #if NET_2_0
353 [MonoTODO("IsDirectPrintingSupported")]
354 public bool IsDirectPrintingSupported (Image image)
356 throw new NotImplementedException();
359 [MonoTODO("IsDirectPrintingSupported")]
360 public bool IsDirectPrintingSupported (ImageFormat imageFormat)
362 throw new NotImplementedException();
364 #endif
366 [MonoTODO("PrinterSettings.SetHdevmode")]
367 public void SetHdevmode(IntPtr hdevmode)
369 throw new NotImplementedException();
372 [MonoTODO("PrinterSettings.SetHdevnames")]
373 public void SetHdevnames(IntPtr hdevnames)
375 throw new NotImplementedException();
378 public override string ToString()
380 return "Printer [PrinterSettings " + printer_name + " Copies=" + copies + " Collate=" + collate
381 + " Duplex=" + can_duplex + " FromPage=" + from_page + " LandscapeAngle=" + landscape_angle
382 + " MaximumCopies=" + maximum_copies + " OutputPort=" + " ToPage=" + to_page + "]";
386 // Public subclasses
387 #region Public Subclasses
390 public class PaperSourceCollection : ICollection, IEnumerable
392 ArrayList _PaperSources = new ArrayList();
394 public PaperSourceCollection(PaperSource[] array) {
395 foreach (PaperSource ps in array)
396 _PaperSources.Add(ps);
399 public int Count { get { return _PaperSources.Count; } }
400 int ICollection.Count { get { return _PaperSources.Count; } }
401 bool ICollection.IsSynchronized { get { return false; } }
402 object ICollection.SyncRoot { get { return this; } }
403 #if NET_2_0
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 public int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
406 public void CopyTo (PaperSource[] paperSources, int index) {throw new NotImplementedException (); }
407 #else
408 internal int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
409 #endif
411 public virtual PaperSource this[int index] {
412 get { return _PaperSources[index] as PaperSource; }
415 IEnumerator IEnumerable.GetEnumerator()
417 return _PaperSources.GetEnumerator();
420 public IEnumerator GetEnumerator()
422 return _PaperSources.GetEnumerator();
425 void ICollection.CopyTo(Array array, int index)
427 _PaperSources.CopyTo(array, index);
430 internal void Clear ()
432 _PaperSources.Clear ();
437 public class PaperSizeCollection : ICollection, IEnumerable
439 ArrayList _PaperSizes = new ArrayList();
441 public PaperSizeCollection(PaperSize[] array) {
442 foreach (PaperSize ps in array)
443 _PaperSizes.Add(ps);
446 public int Count { get { return _PaperSizes.Count; } }
447 int ICollection.Count { get { return _PaperSizes.Count; } }
448 bool ICollection.IsSynchronized { get { return false; } }
449 object ICollection.SyncRoot { get { return this; } }
450 #if NET_2_0
451 [EditorBrowsable(EditorBrowsableState.Never)]
452 public int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
453 public void CopyTo (PaperSize[] paperSizes, int index) {throw new NotImplementedException (); }
454 #else
455 internal int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
456 #endif
458 public virtual PaperSize this[int index] {
459 get { return _PaperSizes[index] as PaperSize; }
462 IEnumerator IEnumerable.GetEnumerator()
464 return _PaperSizes.GetEnumerator();
467 public IEnumerator GetEnumerator()
469 return _PaperSizes.GetEnumerator();
472 void ICollection.CopyTo(Array array, int index)
474 _PaperSizes.CopyTo(array, index);
477 internal void Clear ()
479 _PaperSizes.Clear ();
483 public class PrinterResolutionCollection : ICollection, IEnumerable
485 ArrayList _PrinterResolutions = new ArrayList();
487 public PrinterResolutionCollection(PrinterResolution[] array) {
488 foreach (PrinterResolution pr in array)
489 _PrinterResolutions.Add(pr);
492 public int Count { get { return _PrinterResolutions.Count; } }
493 int ICollection.Count { get { return _PrinterResolutions.Count; } }
494 bool ICollection.IsSynchronized { get { return false; } }
495 object ICollection.SyncRoot { get { return this; } }
496 #if NET_2_0
497 [EditorBrowsable(EditorBrowsableState.Never)]
498 public int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
499 public void CopyTo (PrinterResolution[] printerResolutions, int index) {throw new NotImplementedException (); }
500 #else
501 internal int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
502 #endif
504 public virtual PrinterResolution this[int index] {
505 get { return _PrinterResolutions[index] as PrinterResolution; }
508 IEnumerator IEnumerable.GetEnumerator()
510 return _PrinterResolutions.GetEnumerator();
513 public IEnumerator GetEnumerator()
515 return _PrinterResolutions.GetEnumerator();
518 void ICollection.CopyTo(Array array, int index)
520 _PrinterResolutions.CopyTo(array, index);
523 internal void Clear ()
525 _PrinterResolutions.Clear ();
529 public class StringCollection : ICollection, IEnumerable
531 ArrayList _Strings = new ArrayList();
533 public StringCollection(string[] array) {
534 foreach (string s in array)
535 _Strings.Add(s);
538 public int Count { get { return _Strings.Count; } }
539 int ICollection.Count { get { return _Strings.Count; } }
540 bool ICollection.IsSynchronized { get { return false; } }
541 object ICollection.SyncRoot { get { return this; } }
543 public virtual string this[int index] {
544 get { return _Strings[index] as string; }
546 #if NET_2_0
547 [EditorBrowsable(EditorBrowsableState.Never)]
548 public int Add (string value) { return _Strings.Add (value); }
549 public void CopyTo (string[] strings, int index) {throw new NotImplementedException (); }
550 #else
551 internal int Add (string value) { return _Strings.Add (value); }
552 #endif
554 IEnumerator IEnumerable.GetEnumerator()
556 return _Strings.GetEnumerator();
559 public IEnumerator GetEnumerator()
561 return _Strings.GetEnumerator();
564 void ICollection.CopyTo(Array array, int index)
566 _Strings.CopyTo(array, index);
570 #endregion
572 void GetPrintDialogInfo (string printer_name, ref string port, ref string type, ref string status, ref string comment)
574 printing_services.GetPrintDialogInfo (printer_name, ref port, ref type, ref status, ref comment);