2007-06-21 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PageSettings.cs
1 //
2 // System.Drawing.PageSettings.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 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Runtime.InteropServices;
37
38 namespace System.Drawing.Printing
39 {
40 #if NET_2_0
41         [Serializable]
42 #else
43         [ComVisible (false)]
44 #endif
45         public class PageSettings : ICloneable
46         {
47                 internal bool color;
48                 internal bool landscape;
49                 internal PaperSize paperSize;
50                 internal PaperSource paperSource;
51                 internal PrinterResolution printerResolution;
52
53                 // create a new default Margins object (is 1 inch for all margins)
54                 Margins margins = new Margins();
55 \r
56                 float hardMarginX;\r
57                 float hardMarginY;
58                 RectangleF printableArea;
59                 PrinterSettings printerSettings;
60                 
61                 public PageSettings() : this(new PrinterSettings())
62                 {
63                 }
64                 
65                 public PageSettings(PrinterSettings printerSettings)
66                 {
67                         PrinterSettings = printerSettings;
68                         
69                         this.color = printerSettings.DefaultPageSettings.color;
70                         this.landscape = printerSettings.DefaultPageSettings.landscape;
71                         this.paperSize = printerSettings.DefaultPageSettings.paperSize;
72                         this.paperSource = printerSettings.DefaultPageSettings.paperSource;
73                         this.printerResolution = printerSettings.DefaultPageSettings.printerResolution;
74                 }
75                 
76                 // used by PrinterSettings.DefaultPageSettings
77                 internal PageSettings(PrinterSettings printerSettings, bool color, bool landscape, PaperSize paperSize, PaperSource paperSource, PrinterResolution printerResolution)
78                 {
79                         PrinterSettings = printerSettings;
80                         this.color = color;
81                         this.landscape = landscape;
82                         this.paperSize = paperSize;
83                         this.paperSource = paperSource;
84                         this.printerResolution = printerResolution;
85                 }
86
87                 //props
88                 public Rectangle Bounds{
89                         get{
90                                 int width = this.paperSize.Width;
91                                 int height = this.paperSize.Height;
92                                 
93                                 width -= this.margins.Left + this.margins.Right;
94                                 height -= this.margins.Top + this.margins.Bottom;
95                                 
96                                 if (this.landscape) {
97                                         // swap width and height
98                                         int tmp = width;
99                                         width = height;
100                                         height = tmp;
101                                 }
102                                 return new Rectangle (this.margins.Left, this.margins.Top, width, height);
103                         }
104                 }
105                 
106                 public bool Color{
107                         get{
108                                 if (!this.printerSettings.IsValid)
109                                         throw new InvalidPrinterException(this.printerSettings);
110                                 return color;
111                         }
112                         set{
113                                 color = value;
114                         }
115                 }
116                 
117                 public bool Landscape {
118                         get{
119                                 if (!this.printerSettings.IsValid)
120                                         throw new InvalidPrinterException(this.printerSettings);
121                                 return landscape;
122                         }
123                         set{
124                                 landscape = value;
125                         }
126                 }
127                 
128                 public Margins Margins{
129                         get{
130                                 if (!this.printerSettings.IsValid)
131                                         throw new InvalidPrinterException(this.printerSettings);
132                                 return margins;
133                         }
134                         set{
135                                 margins = value;
136                         }
137                 }
138                 
139                 public PaperSize PaperSize{
140                         get{
141                                 if (!this.printerSettings.IsValid)
142                                         throw new InvalidPrinterException(this.printerSettings);
143                                 return paperSize;
144                         }
145                         set{
146                                 if (value != null)
147                                         paperSize = value;
148                         }
149                 }
150                 
151                 public PaperSource PaperSource{
152                         get{
153                                 if (!this.printerSettings.IsValid)
154                                         throw new InvalidPrinterException(this.printerSettings);
155                                 return paperSource;
156                         }
157                         set{
158                                 if (value != null)
159                                         paperSource = value;
160                         }
161                 }
162                 
163                 public PrinterResolution PrinterResolution{
164                         get{
165                                 if (!this.printerSettings.IsValid)
166                                         throw new InvalidPrinterException(this.printerSettings);
167                                 return printerResolution;
168                         }
169                         set{
170                                 if (value != null)
171                                         printerResolution = value;
172                         }
173                 }
174                 
175                 public PrinterSettings PrinterSettings{
176                         get{
177                                 return printerSettings;
178                         }
179                         set{
180                                 printerSettings = value;
181                         }
182                 }               
183 #if NET_2_0
184                 public float HardMarginX {
185                         get {
186                                 return hardMarginX;
187                         }
188                 }
189                 \r
190                 public float HardMarginY {
191                         get {
192                                 return hardMarginY;
193                         }
194                 }
195                 
196                 public RectangleF PrintableArea {
197                         get {
198                                 return printableArea;
199                         }
200                 }
201 #endif\r
202
203
204                 public object Clone ()
205                 {
206                         // We do a deep copy
207                         PrinterResolution pres = new PrinterResolution (this.printerResolution.X, this.printerResolution.Y, this.printerResolution.Kind);
208                         PaperSource psource = new PaperSource (this.paperSource.SourceName, this.paperSource.Kind);
209                         PaperSize psize = new PaperSize (this.paperSize.PaperName, this.paperSize.Width, this.paperSize.Height);
210                         psize.SetKind (this.paperSize.Kind);
211
212                         PageSettings ps = new PageSettings (this.printerSettings, this.color, this.landscape,
213                                         psize, psource, pres);
214                         ps.Margins = (Margins) this.margins.Clone ();
215                         return ps;
216                 }
217
218
219                 [MonoTODO("PageSettings.CopyToHdevmode")]
220                 public void CopyToHdevmode (IntPtr hdevmode){
221                         throw new NotImplementedException ();
222                 }
223
224
225                 [MonoTODO("PageSettings.SetHdevmode")]
226                 public void SetHdevmode (IntPtr hdevmode){
227                         throw new NotImplementedException ();
228                 }       
229
230                 public override string ToString(){
231                         string ret = "[PageSettings: Color={0}";
232                         ret += ", Landscape={1}";
233                         ret += ", Margins={2}";
234                         ret += ", PaperSize={3}";
235                         ret += ", PaperSource={4}";
236                         ret += ", PrinterResolution={5}";
237                         ret += "]";
238                         
239                         return String.Format(ret, this.color, this.landscape, this.margins, this.paperSize, this.paperSource, this.printerResolution);
240                 }
241         }
242 }