X11: improve handling of WS_EX_TOPMOST
[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.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing.Imaging;
37
38 namespace System.Drawing.Printing
39 {
40         [Serializable]
41 #if ! NET_2_0
42         [ComVisible(false)]
43 #endif  
44         public class PrinterSettings : ICloneable
45         {
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;
67                 
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 ())
70                 {                       
71                 }
72                 
73                 internal PrinterSettings (PrintingServices printing_services)
74                 {
75                         this.printing_services = printing_services;
76                         printer_name = printing_services.DefaultPrinter;
77                         ResetToDefaults ();
78                         printing_services.LoadPrinterSettings (printer_name, this);
79                 }
80                 
81                 private void ResetToDefaults ()
82                 {                       
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;
90                 }
91         
92                 //properties
93                 
94                 public bool CanDuplex
95                 {
96                         get { return can_duplex; }
97                 }
98                 
99                 public bool Collate
100                 {
101                         get { return collate; }
102                         set { collate = value; }
103                 }
104
105                 public short Copies
106                 {
107                         get { return copies; }
108                         set { 
109                                 if (value < 0)
110                                         throw new ArgumentException ("The value of the Copies property is less than zero.");
111                                 
112                                 copies = value;
113                         }
114                 }
115                 
116                 public PageSettings DefaultPageSettings
117                 {
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));
127                                 }
128                                 
129                                 return default_pagesettings;
130                         }
131                 }
132
133                 public Duplex Duplex
134                 {
135                         get { return this.duplex; }
136                         set { this.duplex = value; }
137                 }
138                 
139                 public int FromPage
140                 {
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");
145                                 
146                                 from_page = value;
147                         }
148                 }
149                 
150                 public static PrinterSettings.StringCollection InstalledPrinters
151                 {
152                         get { return SysPrn.GlobalService.InstalledPrinters; }
153                 }
154         
155                 public bool IsDefaultPrinter
156                 {
157                         get { return (printer_name == printing_services.DefaultPrinter); }
158                 }
159
160                 public bool IsPlotter
161                 {
162                         get { return is_plotter; }
163                 }
164
165                 public bool IsValid
166                 {
167                         get { return printing_services.IsPrinterValid (this.printer_name); }
168                 }
169                 
170                 public int LandscapeAngle
171                 {
172                         get { return landscape_angle; }
173                 }
174                 
175                 public int MaximumCopies
176                 {
177                         get { return maximum_copies; }
178                 }
179                 
180                 public int MaximumPage
181                 {
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");
187                                 
188                                 maximum_page = value;
189                         }
190                 }
191                 
192                 public int MinimumPage
193                 {
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");
198                                 
199                                 minimum_page = value;
200                         }
201                 }
202                 
203                 public PrinterSettings.PaperSizeCollection PaperSizes
204                 {
205                         get {
206                                 if (!this.IsValid)
207                                         throw new InvalidPrinterException(this);
208
209                                 return paper_sizes;                             
210                         }
211                 }
212
213                 public PrinterSettings.PaperSourceCollection PaperSources
214                 {
215                         get {
216                                 if (!this.IsValid)
217                                         throw new InvalidPrinterException(this);
218
219                                 return paper_sources;
220                         }
221                 }
222 #if NET_2_0
223                 public
224 #else
225                 internal
226 #endif
227                 string PrintFileName
228                 {
229                         get { return print_filename; }
230                         set { print_filename = value; }
231                 }
232                 public string PrinterName
233                 {
234                         get { return printer_name; }
235                         set { 
236                                 if (printer_name == value)
237                                         return;
238                                         
239                                 printer_name = value;
240                                 printing_services.LoadPrinterSettings (printer_name, this);
241                         }
242                 }
243                 
244                 public PrinterSettings.PrinterResolutionCollection PrinterResolutions
245                 {
246                         get {
247                                 if (!this.IsValid)
248                                         throw new InvalidPrinterException(this);
249                                         
250                                 if (printer_resolutions == null) {
251                                         printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
252                                         printing_services.LoadPrinterResolutions (printer_name, this);
253                                 }
254                                 
255                                 return printer_resolutions;
256                         }
257                 }
258                 
259                 public PrintRange PrintRange
260                 {
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");
266                                 
267                                 print_range = value;
268                         }
269                 }
270                 
271                 public bool PrintToFile
272                 {
273                         get { return print_tofile; }
274                         set { print_tofile = value; }
275                 }
276                 
277                 public bool SupportsColor
278                 {
279                         get { return supports_color; }
280                 }
281                 
282                 public int ToPage
283                 {
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");
288                                 
289                                 to_page = value;
290                         }               
291                 }
292                 
293                 internal NameValueCollection PrinterCapabilities {
294                         get { 
295                                 if (this.printer_capabilities == null)
296                                         this.printer_capabilities = new NameValueCollection();
297                                 return this.printer_capabilities;
298                         }
299                 }
300
301                 //methods               
302                 public object Clone ()
303                 {
304                         PrinterSettings ps = new PrinterSettings (printing_services);
305                         return ps;
306                 }
307
308                 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
309                 public Graphics CreateMeasurementGraphics()
310                 {
311                         throw new NotImplementedException();
312                 }
313 #if NET_2_0
314                 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
315                 public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)            
316                 {
317                         throw new NotImplementedException();
318                 }
319                 
320                 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
321                 public Graphics CreateMeasurementGraphics(PageSettings pageSettings)            
322                 {
323                         throw new NotImplementedException();
324                 }
325                 
326                 [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
327                 public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)                
328                 {
329                         throw new NotImplementedException();
330                 } 
331 #endif          
332
333                 [MonoTODO("PrinterSettings.GetHdevmode")]
334                 public IntPtr GetHdevmode()
335                 {
336                         throw new NotImplementedException();
337                 }
338
339                 [MonoTODO("PrinterSettings.GetHdevmode")]
340                 public IntPtr GetHdevmode(PageSettings pageSettings)
341                 {
342                         throw new NotImplementedException();
343                 }
344
345                 [MonoTODO("PrinterSettings.GetHdevname")]
346                 public IntPtr GetHdevnames()
347                 {
348                         throw new NotImplementedException();
349                 }
350                 
351 #if NET_2_0
352
353                 [MonoTODO("IsDirectPrintingSupported")]
354                 public bool IsDirectPrintingSupported (Image image)
355                 {
356                         throw new NotImplementedException();
357                 }
358                 
359                 [MonoTODO("IsDirectPrintingSupported")]
360                 public bool IsDirectPrintingSupported (ImageFormat imageFormat)
361                 {
362                         throw new NotImplementedException();
363                 }
364 #endif
365
366                 [MonoTODO("PrinterSettings.SetHdevmode")]
367                 public void SetHdevmode(IntPtr hdevmode)
368                 {
369                         throw new NotImplementedException();
370                 }
371
372                 [MonoTODO("PrinterSettings.SetHdevnames")]
373                 public void SetHdevnames(IntPtr hdevnames)
374                 {
375                         throw new NotImplementedException();
376                 }
377                 
378                 public override string ToString()
379                 {
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 + "]";
383
384                 }
385                 
386                 // Public subclasses
387                 #region Public Subclasses
388                 
389
390                 public class PaperSourceCollection : ICollection, IEnumerable
391                 {
392                         ArrayList _PaperSources = new ArrayList();
393                         
394                         public PaperSourceCollection(PaperSource[] array) {
395                                 foreach (PaperSource ps in array)
396                                         _PaperSources.Add(ps);
397                         }
398                         
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
410                         
411                         public virtual PaperSource this[int index] {
412                                 get { return _PaperSources[index] as PaperSource; }
413                         }
414                         
415                         IEnumerator IEnumerable.GetEnumerator()
416                         {
417                                 return _PaperSources.GetEnumerator();
418                         }
419                         
420                         public IEnumerator GetEnumerator()
421                         {
422                                 return _PaperSources.GetEnumerator();
423                         }
424                         
425                         void ICollection.CopyTo(Array array, int index)
426                         {
427                                 _PaperSources.CopyTo(array, index);
428                         }
429                         
430                         internal void Clear ()
431                         { 
432                                 _PaperSources.Clear (); 
433                         }
434                         
435                 }
436
437                 public class PaperSizeCollection : ICollection, IEnumerable
438                 {
439                         ArrayList _PaperSizes = new ArrayList();
440                         
441                         public PaperSizeCollection(PaperSize[] array) {
442                                 foreach (PaperSize ps in array)
443                                         _PaperSizes.Add(ps);
444                         }
445                         
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
457                         
458                         public virtual PaperSize this[int index] {
459                                 get { return _PaperSizes[index] as PaperSize; }
460                         }
461                         
462                         IEnumerator IEnumerable.GetEnumerator()
463                         {
464                                 return _PaperSizes.GetEnumerator();
465                         }
466                         
467                         public IEnumerator GetEnumerator()
468                         {
469                                 return _PaperSizes.GetEnumerator();
470                         }
471                         
472                         void ICollection.CopyTo(Array array, int index)
473                         {
474                                 _PaperSizes.CopyTo(array, index);
475                         }
476                         
477                         internal void Clear ()
478                         { 
479                                 _PaperSizes.Clear (); 
480                         }
481                 }
482
483                 public class PrinterResolutionCollection : ICollection, IEnumerable
484                 {
485                         ArrayList _PrinterResolutions = new ArrayList();
486                         
487                         public PrinterResolutionCollection(PrinterResolution[] array) {
488                                 foreach (PrinterResolution pr in array)
489                                         _PrinterResolutions.Add(pr);
490                         }
491                         
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
503                                                 
504                         public virtual PrinterResolution this[int index] {
505                                 get { return _PrinterResolutions[index] as PrinterResolution; }
506                         }
507                         
508                         IEnumerator IEnumerable.GetEnumerator()
509                         {
510                                 return _PrinterResolutions.GetEnumerator();
511                         }
512                         
513                         public IEnumerator GetEnumerator()
514                         {
515                                 return _PrinterResolutions.GetEnumerator();
516                         }
517                         
518                         void ICollection.CopyTo(Array array, int index)
519                         {
520                                 _PrinterResolutions.CopyTo(array, index);
521                         }
522                         
523                         internal void Clear ()
524                         { 
525                                 _PrinterResolutions.Clear (); 
526                         }
527                 }
528
529                 public class StringCollection : ICollection, IEnumerable
530                 {
531                         ArrayList _Strings = new ArrayList();
532                         
533                         public StringCollection(string[] array) {
534                                 foreach (string s in array)
535                                         _Strings.Add(s);
536                         }
537                         
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; } }
542                                                 
543                         public virtual string this[int index] {
544                                 get { return _Strings[index] as string; }
545                         }
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
553
554                         IEnumerator IEnumerable.GetEnumerator()
555                         {
556                                 return _Strings.GetEnumerator();
557                         }
558                         
559                         public IEnumerator GetEnumerator()
560                         {
561                                 return _Strings.GetEnumerator();
562                         }
563                         
564                         void ICollection.CopyTo(Array array, int index)
565                         {
566                                 _Strings.CopyTo(array, index);
567                         }                       
568                 }
569                 
570                 #endregion
571 /*
572                 void GetPrintDialogInfo (string printer_name, ref string port, ref string type, ref string status, ref string comment)
573                 {
574                         printing_services.GetPrintDialogInfo (printer_name, ref port, ref type, ref status, ref comment);
575                 }
576 */
577         }
578 }