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