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)
11 //
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.
30 //
31 
32 using System.Runtime.InteropServices;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing.Imaging;
37 
38 namespace System.Drawing.Printing
39 {
40 	[Serializable]
41 	public class PrinterSettings : ICloneable
42 	{
43 		private string printer_name;
44 		private string print_filename;
45 		private short copies;
46 		private int maximum_page;
47 		private int minimum_page;
48 		private int from_page;
49 		private int to_page;
50 		private bool collate;
51 		private PrintRange print_range;
52 		internal int maximum_copies;
53 		internal bool can_duplex;
54 		internal bool supports_color;
55 		internal int landscape_angle;
56 		private bool print_tofile;
57 		internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
58 		internal PrinterSettings.PaperSizeCollection paper_sizes;
59 		internal PrinterSettings.PaperSourceCollection paper_sources;
60 		private PageSettings default_pagesettings;
61 		private Duplex duplex;
62 		internal bool is_plotter;
63 		private PrintingServices printing_services;
64 
65 		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.
PrinterSettings()66 		public PrinterSettings() : this (SysPrn.CreatePrintingService ())
67 		{
68 		}
69 
PrinterSettings(PrintingServices printing_services)70 		internal PrinterSettings (PrintingServices printing_services)
71 		{
72 			this.printing_services = printing_services;
73 			printer_name = printing_services.DefaultPrinter;
74 			ResetToDefaults ();
75 			printing_services.LoadPrinterSettings (printer_name, this);
76 		}
77 
ResetToDefaults()78 		private void ResetToDefaults ()
79 		{
80 			printer_resolutions = null;
81 			paper_sizes = null;
82 			paper_sources = null;
83 			default_pagesettings = null;
84 			maximum_page = 9999;
85 			copies = 1;
86 			collate = true;
87 		}
88 
89 		//properties
90 
91 		public bool CanDuplex
92 		{
93 			get { return can_duplex; }
94 		}
95 
96 		public bool Collate
97 		{
98 			get { return collate; }
99 			set { collate = value; }
100 		}
101 
102 		public short Copies
103 		{
104 			get { return copies; }
105 			set {
106 				if (value < 0)
107 					throw new ArgumentException ("The value of the Copies property is less than zero.");
108 
109 				copies = value;
110 			}
111 		}
112 
113 		public PageSettings DefaultPageSettings
114 		{
115 			get {
116 				if (default_pagesettings == null) {
117 					default_pagesettings = new PageSettings (this,
118 						SupportsColor,
119 						false,
120 						// Real defaults are set by LoadPrinterSettings
121 						new PaperSize("A4", 827, 1169),
122 						new PaperSource(PaperSourceKind.FormSource, "Tray"),
123 						new PrinterResolution(PrinterResolutionKind.Medium, 200, 200));
124 				}
125 
126 				return default_pagesettings;
127 			}
128 		}
129 
130 		public Duplex Duplex
131 		{
132 			get { return this.duplex; }
133 			set { this.duplex = value; }
134 		}
135 
136 		public int FromPage
137 		{
138 			get { return from_page; }
139 			set {
140 				if (value < 0)
141 					throw new ArgumentException ("The value of the FromPage property is less than zero");
142 
143 				from_page = value;
144 			}
145 		}
146 
147 		public static PrinterSettings.StringCollection InstalledPrinters
148 		{
149 			get { return SysPrn.GlobalService.InstalledPrinters; }
150 		}
151 
152 		public bool IsDefaultPrinter
153 		{
154 			get { return (printer_name == printing_services.DefaultPrinter); }
155 		}
156 
157 		public bool IsPlotter
158 		{
159 			get { return is_plotter; }
160 		}
161 
162 		public bool IsValid
163 		{
164 			get { return printing_services.IsPrinterValid (this.printer_name); }
165 		}
166 
167 		public int LandscapeAngle
168 		{
169 			get { return landscape_angle; }
170 		}
171 
172 		public int MaximumCopies
173 		{
174 			get { return maximum_copies; }
175 		}
176 
177 		public int MaximumPage
178 		{
179 			get { return maximum_page; }
180 			set {
181 				// This not documented but behaves like MinimumPage
182 				if (value < 0)
183 					throw new ArgumentException ("The value of the MaximumPage property is less than zero");
184 
185 				maximum_page = value;
186 			}
187 		}
188 
189 		public int MinimumPage
190 		{
191 			get { return minimum_page; }
192 			set {
193 				if (value < 0)
194 					throw new ArgumentException ("The value of the MaximumPage property is less than zero");
195 
196 				minimum_page = value;
197 			}
198 		}
199 
200 		public PrinterSettings.PaperSizeCollection PaperSizes
201 		{
202 			get {
203 				if (!this.IsValid)
204 					throw new InvalidPrinterException(this);
205 
206 				return paper_sizes;
207 			}
208 		}
209 
210 		public PrinterSettings.PaperSourceCollection PaperSources
211 		{
212 			get {
213 				if (!this.IsValid)
214 					throw new InvalidPrinterException(this);
215 
216 				return paper_sources;
217 			}
218 		}
219 		public
220 		string PrintFileName
221 		{
222 			get { return print_filename; }
223 			set { print_filename = value; }
224 		}
225 		public string PrinterName
226 		{
227 			get { return printer_name; }
228 			set {
229 				if (printer_name == value)
230 					return;
231 
232 				printer_name = value;
233 				printing_services.LoadPrinterSettings (printer_name, this);
234 			}
235 		}
236 
237 		public PrinterSettings.PrinterResolutionCollection PrinterResolutions
238 		{
239 			get {
240 				if (!this.IsValid)
241 					throw new InvalidPrinterException(this);
242 
243 				if (printer_resolutions == null) {
244 					printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
245 					printing_services.LoadPrinterResolutions (printer_name, this);
246 				}
247 
248 				return printer_resolutions;
249 			}
250 		}
251 
252 		public PrintRange PrintRange
253 		{
254 			get { return print_range; }
255 			set {
256 				if (value != PrintRange.AllPages && value != PrintRange.Selection &&
257 					value != PrintRange.SomePages)
258 					throw new InvalidEnumArgumentException ("The value of the PrintRange property is not one of the PrintRange values");
259 
260 				print_range = value;
261 			}
262 		}
263 
264 		public bool PrintToFile
265 		{
266 			get { return print_tofile; }
267 			set { print_tofile = value; }
268 		}
269 
270 		public bool SupportsColor
271 		{
272 			get { return supports_color; }
273 		}
274 
275 		public int ToPage
276 		{
277 			get { return to_page; }
278 			set {
279 				if (value < 0)
280 					throw new ArgumentException ("The value of the ToPage property is less than zero");
281 
282 				to_page = value;
283 			}
284 		}
285 
286 		internal NameValueCollection PrinterCapabilities {
287 			get {
288 				if (this.printer_capabilities == null)
289 					this.printer_capabilities = new NameValueCollection();
290 				return this.printer_capabilities;
291 			}
292 		}
293 
294 		//methods
Clone()295 		public object Clone ()
296 		{
297 			PrinterSettings ps = new PrinterSettings (printing_services);
298 			return ps;
299 		}
300 
301 		[MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
CreateMeasurementGraphics()302 		public Graphics CreateMeasurementGraphics()
303 		{
304 			throw new NotImplementedException();
305 		}
306 		[MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
CreateMeasurementGraphics(bool honorOriginAtMargins)307 		public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)
308 		{
309 			throw new NotImplementedException();
310 		}
311 
312 		[MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
CreateMeasurementGraphics(PageSettings pageSettings)313 		public Graphics CreateMeasurementGraphics(PageSettings pageSettings)
314 		{
315 			throw new NotImplementedException();
316 		}
317 
318 		[MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
CreateMeasurementGraphics(PageSettings pageSettings, bool honorOriginAtMargins)319 		public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)
320 		{
321 			throw new NotImplementedException();
322 		}
323 
324 		[MonoTODO("PrinterSettings.GetHdevmode")]
GetHdevmode()325 		public IntPtr GetHdevmode()
326 		{
327 			throw new NotImplementedException();
328 		}
329 
330 		[MonoTODO("PrinterSettings.GetHdevmode")]
GetHdevmode(PageSettings pageSettings)331 		public IntPtr GetHdevmode(PageSettings pageSettings)
332 		{
333 			throw new NotImplementedException();
334 		}
335 
336 		[MonoTODO("PrinterSettings.GetHdevname")]
GetHdevnames()337 		public IntPtr GetHdevnames()
338 		{
339 			throw new NotImplementedException();
340 		}
341 
342 
343 		[MonoTODO("IsDirectPrintingSupported")]
IsDirectPrintingSupported(Image image)344 		public bool IsDirectPrintingSupported (Image image)
345 		{
346 			throw new NotImplementedException();
347 		}
348 
349 		[MonoTODO("IsDirectPrintingSupported")]
IsDirectPrintingSupported(ImageFormat imageFormat)350 		public bool IsDirectPrintingSupported (ImageFormat imageFormat)
351 		{
352 			throw new NotImplementedException();
353 		}
354 
355 		[MonoTODO("PrinterSettings.SetHdevmode")]
SetHdevmode(IntPtr hdevmode)356 		public void SetHdevmode(IntPtr hdevmode)
357 		{
358 			throw new NotImplementedException();
359 		}
360 
361 		[MonoTODO("PrinterSettings.SetHdevnames")]
SetHdevnames(IntPtr hdevnames)362 		public void SetHdevnames(IntPtr hdevnames)
363 		{
364 			throw new NotImplementedException();
365 		}
366 
ToString()367 		public override string ToString()
368 		{
369 			return "Printer [PrinterSettings " + printer_name + " Copies=" + copies +  " Collate=" + collate
370 			+ " Duplex=" + can_duplex + " FromPage=" + from_page + " LandscapeAngle=" + landscape_angle
371 			+ " MaximumCopies=" + maximum_copies + " OutputPort=" + " ToPage=" + to_page + "]";
372 
373 		}
374 
375 		// Public subclasses
376 		#region Public Subclasses
377 
378 
379 		public class PaperSourceCollection : ICollection, IEnumerable
380 		{
381 			ArrayList _PaperSources = new ArrayList();
382 
PaperSourceCollection(PaperSource[] array)383 			public PaperSourceCollection(PaperSource[] array) {
384 				foreach (PaperSource ps in array)
385 					_PaperSources.Add(ps);
386 			}
387 
388 			public int Count { get { return _PaperSources.Count; } }
389 			int ICollection.Count { get { return _PaperSources.Count; } }
390 			bool ICollection.IsSynchronized { get { return false; } }
391 			object ICollection.SyncRoot { get { return this; } }
392 			[EditorBrowsable(EditorBrowsableState.Never)]
Add(PaperSource paperSource)393       			public int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
CopyTo(PaperSource[] paperSources, int index)394 			public void CopyTo (PaperSource[] paperSources, int index)  {throw new NotImplementedException (); }
395 
396 			public virtual PaperSource this[int index] {
397 				get { return _PaperSources[index] as PaperSource; }
398 			}
399 
IEnumerable.GetEnumerator()400 			IEnumerator IEnumerable.GetEnumerator()
401 			{
402 				return _PaperSources.GetEnumerator();
403 			}
404 
GetEnumerator()405 			public IEnumerator GetEnumerator()
406 			{
407 				return _PaperSources.GetEnumerator();
408 			}
409 
ICollection.CopyTo(Array array, int index)410 			void ICollection.CopyTo(Array array, int index)
411 			{
412 				_PaperSources.CopyTo(array, index);
413 			}
414 
Clear()415 			internal void Clear ()
416 			{
417 				_PaperSources.Clear ();
418 			}
419 
420 		}
421 
422 		public class PaperSizeCollection : ICollection, IEnumerable
423 		{
424 			ArrayList _PaperSizes = new ArrayList();
425 
PaperSizeCollection(PaperSize[] array)426 			public PaperSizeCollection(PaperSize[] array) {
427 				foreach (PaperSize ps in array)
428 					_PaperSizes.Add(ps);
429 			}
430 
431 			public int Count { get { return _PaperSizes.Count; } }
432 			int ICollection.Count { get { return _PaperSizes.Count; } }
433 			bool ICollection.IsSynchronized { get { return false; } }
434 			object ICollection.SyncRoot { get { return this; } }
435 			[EditorBrowsable(EditorBrowsableState.Never)]
Add(PaperSize paperSize)436 			public int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
CopyTo(PaperSize[] paperSizes, int index)437 			public void CopyTo (PaperSize[] paperSizes, int index) {throw new NotImplementedException (); }
438 
439 			public virtual PaperSize this[int index] {
440 				get { return _PaperSizes[index] as PaperSize; }
441 			}
442 
IEnumerable.GetEnumerator()443 			IEnumerator IEnumerable.GetEnumerator()
444 			{
445 				return _PaperSizes.GetEnumerator();
446 			}
447 
GetEnumerator()448 			public IEnumerator GetEnumerator()
449 			{
450 				return _PaperSizes.GetEnumerator();
451 			}
452 
ICollection.CopyTo(Array array, int index)453 			void ICollection.CopyTo(Array array, int index)
454 			{
455 				_PaperSizes.CopyTo(array, index);
456 			}
457 
Clear()458 			internal void Clear ()
459 			{
460 				_PaperSizes.Clear ();
461 			}
462 		}
463 
464 		public class PrinterResolutionCollection : ICollection, IEnumerable
465 		{
466 			ArrayList _PrinterResolutions = new ArrayList();
467 
PrinterResolutionCollection(PrinterResolution[] array)468 			public PrinterResolutionCollection(PrinterResolution[] array) {
469 				foreach (PrinterResolution pr in array)
470 					_PrinterResolutions.Add(pr);
471 			}
472 
473 			public int Count { get { return _PrinterResolutions.Count; } }
474 			int ICollection.Count { get { return _PrinterResolutions.Count; } }
475 			bool ICollection.IsSynchronized { get { return false; } }
476 			object ICollection.SyncRoot { get { return this; } }
477 			[EditorBrowsable(EditorBrowsableState.Never)]
Add(PrinterResolution printerResolution)478 			public int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
CopyTo(PrinterResolution[] printerResolutions, int index)479 			public void CopyTo (PrinterResolution[] printerResolutions, int index) {throw new NotImplementedException (); }
480 
481 			public virtual PrinterResolution this[int index] {
482 				get { return _PrinterResolutions[index] as PrinterResolution; }
483 			}
484 
IEnumerable.GetEnumerator()485 			IEnumerator IEnumerable.GetEnumerator()
486 			{
487 				return _PrinterResolutions.GetEnumerator();
488 			}
489 
GetEnumerator()490 			public IEnumerator GetEnumerator()
491 			{
492 				return _PrinterResolutions.GetEnumerator();
493 			}
494 
ICollection.CopyTo(Array array, int index)495 			void ICollection.CopyTo(Array array, int index)
496 			{
497 				_PrinterResolutions.CopyTo(array, index);
498 			}
499 
Clear()500 			internal void Clear ()
501 			{
502 				_PrinterResolutions.Clear ();
503 			}
504 		}
505 
506 		public class StringCollection : ICollection, IEnumerable
507 		{
508 			ArrayList _Strings = new ArrayList();
509 
StringCollection(string[] array)510 			public StringCollection(string[] array) {
511 				foreach (string s in array)
512 					_Strings.Add(s);
513 			}
514 
515 			public int Count { get { return _Strings.Count; } }
516 			int ICollection.Count { get { return _Strings.Count; } }
517 			bool ICollection.IsSynchronized { get { return false; } }
518 			object ICollection.SyncRoot { get { return this; } }
519 
520 			public virtual string this[int index] {
521 				get { return _Strings[index] as string; }
522 			}
523 			[EditorBrowsable(EditorBrowsableState.Never)]
Add(string value)524       			public int Add (string value) { return _Strings.Add (value); }
CopyTo(string[] strings, int index)525       			public void CopyTo (string[] strings, int index) {throw new NotImplementedException (); }
526 
IEnumerable.GetEnumerator()527 			IEnumerator IEnumerable.GetEnumerator()
528 			{
529 				return _Strings.GetEnumerator();
530 			}
531 
GetEnumerator()532 			public IEnumerator GetEnumerator()
533 			{
534 				return _Strings.GetEnumerator();
535 			}
536 
ICollection.CopyTo(Array array, int index)537 			void ICollection.CopyTo(Array array, int index)
538 			{
539 				_Strings.CopyTo(array, index);
540 			}
541 		}
542 
543 		#endregion
544 /*
545 		void GetPrintDialogInfo (string printer_name, ref string port, ref string type, ref string status, ref string comment)
546 		{
547 			printing_services.GetPrintDialogInfo (printer_name, ref port, ref type, ref status, ref comment);
548 		}
549 */
550 	}
551 }
552