Merge pull request #347 from JamesB7/master
[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
56 #if NET_2_0
57                 float hardMarginX;
58                 float hardMarginY;
59                 RectangleF printableArea;               
60 #endif          
61                 PrinterSettings printerSettings;
62                 
63                 public PageSettings() : this(new PrinterSettings())
64                 {
65                 }
66                 
67                 public PageSettings(PrinterSettings printerSettings)
68                 {
69                         PrinterSettings = printerSettings;
70                         
71                         this.color = printerSettings.DefaultPageSettings.color;
72                         this.landscape = printerSettings.DefaultPageSettings.landscape;
73                         this.paperSize = printerSettings.DefaultPageSettings.paperSize;
74                         this.paperSource = printerSettings.DefaultPageSettings.paperSource;
75                         this.printerResolution = printerSettings.DefaultPageSettings.printerResolution;
76                 }
77                 
78                 // used by PrinterSettings.DefaultPageSettings
79                 internal PageSettings(PrinterSettings printerSettings, bool color, bool landscape, PaperSize paperSize, PaperSource paperSource, PrinterResolution printerResolution)
80                 {
81                         PrinterSettings = printerSettings;
82                         this.color = color;
83                         this.landscape = landscape;
84                         this.paperSize = paperSize;
85                         this.paperSource = paperSource;
86                         this.printerResolution = printerResolution;
87                 }
88
89                 //props
90                 public Rectangle Bounds{
91                         get{
92                                 int width = this.paperSize.Width;
93                                 int height = this.paperSize.Height;
94                                 
95                                 width -= this.margins.Left + this.margins.Right;
96                                 height -= this.margins.Top + this.margins.Bottom;
97                                 
98                                 if (this.landscape) {
99                                         // swap width and height
100                                         int tmp = width;
101                                         width = height;
102                                         height = tmp;
103                                 }
104                                 return new Rectangle (this.margins.Left, this.margins.Top, width, height);
105                         }
106                 }
107                 
108                 public bool Color{
109                         get{
110                                 if (!this.printerSettings.IsValid)
111                                         throw new InvalidPrinterException(this.printerSettings);
112                                 return color;
113                         }
114                         set{
115                                 color = value;
116                         }
117                 }
118                 
119                 public bool Landscape {
120                         get{
121                                 if (!this.printerSettings.IsValid)
122                                         throw new InvalidPrinterException(this.printerSettings);
123                                 return landscape;
124                         }
125                         set{
126                                 landscape = value;
127                         }
128                 }
129                 
130                 public Margins Margins{
131                         get{
132                                 if (!this.printerSettings.IsValid)
133                                         throw new InvalidPrinterException(this.printerSettings);
134                                 return margins;
135                         }
136                         set{
137                                 margins = value;
138                         }
139                 }
140                 
141                 public PaperSize PaperSize{
142                         get{
143                                 if (!this.printerSettings.IsValid)
144                                         throw new InvalidPrinterException(this.printerSettings);
145                                 return paperSize;
146                         }
147                         set{
148                                 if (value != null)
149                                         paperSize = value;
150                         }
151                 }
152                 
153                 public PaperSource PaperSource{
154                         get{
155                                 if (!this.printerSettings.IsValid)
156                                         throw new InvalidPrinterException(this.printerSettings);
157                                 return paperSource;
158                         }
159                         set{
160                                 if (value != null)
161                                         paperSource = value;
162                         }
163                 }
164                 
165                 public PrinterResolution PrinterResolution{
166                         get{
167                                 if (!this.printerSettings.IsValid)
168                                         throw new InvalidPrinterException(this.printerSettings);
169                                 return printerResolution;
170                         }
171                         set{
172                                 if (value != null)
173                                         printerResolution = value;
174                         }
175                 }
176                 
177                 public PrinterSettings PrinterSettings{
178                         get{
179                                 return printerSettings;
180                         }
181                         set{
182                                 printerSettings = value;
183                         }
184                 }               
185 #if NET_2_0
186                 public float HardMarginX {
187                         get {
188                                 return hardMarginX;
189                         }
190                 }
191                 
192                 public float HardMarginY {
193                         get {
194                                 return hardMarginY;
195                         }
196                 }
197                 
198                 public RectangleF PrintableArea {
199                         get {
200                                 return printableArea;
201                         }
202                 }
203 #endif
204
205
206                 public object Clone ()
207                 {
208                         // We do a deep copy
209                         PrinterResolution pres = new PrinterResolution (this.printerResolution.X, this.printerResolution.Y, this.printerResolution.Kind);
210                         PaperSource psource = new PaperSource (this.paperSource.SourceName, this.paperSource.Kind);
211                         PaperSize psize = new PaperSize (this.paperSize.PaperName, this.paperSize.Width, this.paperSize.Height);
212                         psize.SetKind (this.paperSize.Kind);
213
214                         PageSettings ps = new PageSettings (this.printerSettings, this.color, this.landscape,
215                                         psize, psource, pres);
216                         ps.Margins = (Margins) this.margins.Clone ();
217                         return ps;
218                 }
219
220
221                 [MonoTODO("PageSettings.CopyToHdevmode")]
222                 public void CopyToHdevmode (IntPtr hdevmode){
223                         throw new NotImplementedException ();
224                 }
225
226
227                 [MonoTODO("PageSettings.SetHdevmode")]
228                 public void SetHdevmode (IntPtr hdevmode){
229                         throw new NotImplementedException ();
230                 }       
231
232                 public override string ToString(){
233                         string ret = "[PageSettings: Color={0}";
234                         ret += ", Landscape={1}";
235                         ret += ", Margins={2}";
236                         ret += ", PaperSize={3}";
237                         ret += ", PaperSource={4}";
238                         ret += ", PrinterResolution={5}";
239                         ret += "]";
240                         
241                         return String.Format(ret, this.color, this.landscape, this.margins, this.paperSize, this.paperSource, this.printerResolution);
242                 }
243         }
244 }