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