BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PrintDocument.cs
1 //
2 // System.Drawing.PrintDocument.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.ComponentModel;
37
38 namespace System.Drawing.Printing
39 {
40         [DefaultEvent ("PrintPage"), DefaultProperty ("DocumentName")]
41         [ToolboxItemFilter ("System.Drawing.Printing", ToolboxItemFilterType.Allow)]
42         public class PrintDocument : System.ComponentModel.Component
43         {
44                 private PageSettings defaultpagesettings;
45                 private PrinterSettings printersettings;
46                 private PrintController printcontroller;
47                 private string documentname;
48 #if !(NET_1_0)
49                 private bool originAtMargins = false; // .NET V1.1 Beta
50 #endif
51
52                 public PrintDocument() {
53                         documentname = "document"; //offical default.
54                         printersettings = new PrinterSettings(); // use default values
55                         defaultpagesettings = (PageSettings) printersettings.DefaultPageSettings.Clone ();
56                         printcontroller = new StandardPrintController();
57                 }
58
59                 // properties
60                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
61                 [Browsable (false)]
62                 [SRDescription ("The settings for the current page.")]
63                 public PageSettings DefaultPageSettings{
64                         get{
65                                 return defaultpagesettings;
66                         }
67                         set{
68                                 defaultpagesettings = value;
69                         }
70                 }
71
72                 // Name of the document, not the file!
73                 [DefaultValue ("document")]
74                 [SRDescription ("The name of the document.")]
75                 public string DocumentName{
76                         get{
77                                 return documentname;
78                         }
79                         set{
80                                 documentname = value;
81                         }
82                 }
83
84                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
85                 [Browsable (false)]
86                 [SRDescription ("The print controller object.")]
87                 public PrintController PrintController{
88                         get{
89                                 return printcontroller;
90                         }
91                         set{
92                                 printcontroller = value;
93                         }
94                 }
95
96                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
97                 [Browsable (false)]
98                 [SRDescription ("The current settings for the active printer.")]
99                 public PrinterSettings PrinterSettings{
100                         get{
101                                 return printersettings;
102                         }
103                         set{
104                                 printersettings = value == null ? new PrinterSettings () : value;
105                         }
106                 }
107
108 #if !(NET_1_0)
109                 [DefaultValue (false)]
110                 [SRDescription ("Determines if the origin is set at the specified margins.")]
111                 public bool OriginAtMargins{
112                         get{
113                                 return originAtMargins;
114                         }
115                         set{
116                                 originAtMargins = value;
117                         }
118                 }
119 #endif
120
121                 // methods
122                 public void Print(){
123                         PrintEventArgs printArgs = new PrintEventArgs();
124                         this.OnBeginPrint(printArgs);
125                         if (printArgs.Cancel)
126                                 return;
127                         PrintController.OnStartPrint(this, printArgs);
128                         if (printArgs.Cancel)
129                                 return;                 
130                                 
131                         Graphics g = null;
132                         
133                         if (printArgs.GraphicsContext != null) {
134                                 g = Graphics.FromHdc (printArgs.GraphicsContext.Hdc);
135                                 printArgs.GraphicsContext.Graphics = g;
136                         }
137
138                         // while there are more pages
139                         PrintPageEventArgs printPageArgs;
140                         do
141                         {
142                                 QueryPageSettingsEventArgs queryPageSettingsArgs = new QueryPageSettingsEventArgs (
143                                                 DefaultPageSettings.Clone () as PageSettings);
144                                 OnQueryPageSettings (queryPageSettingsArgs);
145                                 
146                                 PageSettings pageSettings = queryPageSettingsArgs.PageSettings;
147                                 printPageArgs = new PrintPageEventArgs(
148                                                 g,
149                                                 pageSettings.Bounds,
150                                                 new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height),
151                                                 pageSettings);
152                                                         
153                                 // TODO: We should create a graphics context for each page since they can have diferent paper
154                                 // size, orientation, etc. We use a single graphic for now to keep Cairo using a single PDF file.
155                                 
156                                 printPageArgs.GraphicsContext = printArgs.GraphicsContext;
157                                 Graphics pg = PrintController.OnStartPage(this, printPageArgs);
158
159                                 // assign Graphics in printPageArgs
160                                 printPageArgs.SetGraphics(pg);
161                                 
162                                 if (!printPageArgs.Cancel)
163                                         this.OnPrintPage(printPageArgs);                                
164                                 
165                                 PrintController.OnEndPage(this, printPageArgs);                         
166                                 if (printPageArgs.Cancel)
167                                         break;                          
168                         } while (printPageArgs.HasMorePages);                   
169
170                         this.OnEndPrint(printArgs);
171                         PrintController.OnEndPrint(this, printArgs);                    
172                 }
173
174                 public override string ToString(){
175                         return "[PrintDocument " + this.DocumentName + "]";
176                 }
177                 
178                 // events
179                 protected virtual void OnBeginPrint(PrintEventArgs e){
180                         //fire the event
181                         if (BeginPrint != null)
182                                 BeginPrint(this, e);
183                 }
184                 
185                 protected virtual void OnEndPrint(PrintEventArgs e){
186                         //fire the event
187                         if (EndPrint != null)
188                                 EndPrint(this, e);
189                 }
190                 
191                 protected virtual void OnPrintPage(PrintPageEventArgs e){
192                         //fire the event
193                         if (PrintPage != null)
194                                 PrintPage(this, e);
195                 }
196                 
197                 protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e){
198                         //fire the event
199                         if (QueryPageSettings != null)
200                                 QueryPageSettings(this, e);
201                 }
202
203                 [SRDescription ("Raised when printing begins")]
204                 public event PrintEventHandler BeginPrint;
205
206                 [SRDescription ("Raised when printing ends")]
207                 public event PrintEventHandler EndPrint;
208
209                 [SRDescription ("Raised when printing of a new page begins")]
210                 public event PrintPageEventHandler PrintPage;
211
212                 [SRDescription ("Raised before printing of a new page begins")]
213                 public event QueryPageSettingsEventHandler QueryPageSettings;
214         }
215 }