2 // System.Drawing.PrintDocument.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Herve Poussineau (hpoussineau@fr.st)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2002 Ximian, Inc
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System
.ComponentModel
;
38 namespace System
.Drawing
.Printing
40 [DefaultEvent ("PrintPage"), DefaultProperty ("DocumentName")]
41 [ToolboxItemFilter ("System.Drawing.Printing", ToolboxItemFilterType
.Allow
)]
42 public class PrintDocument
: System
.ComponentModel
.Component
44 private PageSettings defaultpagesettings
;
45 private PrinterSettings printersettings
;
46 private PrintController printcontroller
;
47 private string documentname
;
48 private bool originAtMargins
= false; // .NET V1.1 Beta
50 public PrintDocument() {
51 documentname
= "document"; //offical default.
52 printersettings
= new PrinterSettings(); // use default values
53 defaultpagesettings
= (PageSettings
) printersettings
.DefaultPageSettings
.Clone ();
54 printcontroller
= new StandardPrintController();
58 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
60 [SRDescription ("The settings for the current page.")]
61 public PageSettings DefaultPageSettings
{
63 return defaultpagesettings
;
66 defaultpagesettings
= value;
70 // Name of the document, not the file!
71 [DefaultValue ("document")]
72 [SRDescription ("The name of the document.")]
73 public string DocumentName
{
82 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
84 [SRDescription ("The print controller object.")]
85 public PrintController PrintController
{
87 return printcontroller
;
90 printcontroller
= value;
94 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
96 [SRDescription ("The current settings for the active printer.")]
97 public PrinterSettings PrinterSettings
{
99 return printersettings
;
102 printersettings
= value == null ? new PrinterSettings () : value;
106 [DefaultValue (false)]
107 [SRDescription ("Determines if the origin is set at the specified margins.")]
108 public bool OriginAtMargins
{
110 return originAtMargins
;
113 originAtMargins
= value;
119 PrintEventArgs printArgs
= new PrintEventArgs();
120 this.OnBeginPrint(printArgs
);
121 if (printArgs
.Cancel
)
123 PrintController
.OnStartPrint(this, printArgs
);
124 if (printArgs
.Cancel
)
129 if (printArgs
.GraphicsContext
!= null) {
130 g
= Graphics
.FromHdc (printArgs
.GraphicsContext
.Hdc
);
131 printArgs
.GraphicsContext
.Graphics
= g
;
134 // while there are more pages
135 PrintPageEventArgs printPageArgs
;
138 QueryPageSettingsEventArgs queryPageSettingsArgs
= new QueryPageSettingsEventArgs (
139 DefaultPageSettings
.Clone () as PageSettings
);
140 OnQueryPageSettings (queryPageSettingsArgs
);
142 PageSettings pageSettings
= queryPageSettingsArgs
.PageSettings
;
143 printPageArgs
= new PrintPageEventArgs(
146 new Rectangle(0, 0, pageSettings
.PaperSize
.Width
, pageSettings
.PaperSize
.Height
),
149 // TODO: We should create a graphics context for each page since they can have diferent paper
150 // size, orientation, etc. We use a single graphic for now to keep Cairo using a single PDF file.
152 printPageArgs
.GraphicsContext
= printArgs
.GraphicsContext
;
153 Graphics pg
= PrintController
.OnStartPage(this, printPageArgs
);
155 // assign Graphics in printPageArgs
156 printPageArgs
.SetGraphics(pg
);
158 if (!printPageArgs
.Cancel
)
159 this.OnPrintPage(printPageArgs
);
161 PrintController
.OnEndPage(this, printPageArgs
);
162 if (printPageArgs
.Cancel
)
164 } while (printPageArgs
.HasMorePages
);
166 this.OnEndPrint(printArgs
);
167 PrintController
.OnEndPrint(this, printArgs
);
170 public override string ToString(){
171 return "[PrintDocument " + this.DocumentName
+ "]";
175 protected virtual void OnBeginPrint(PrintEventArgs e
){
177 if (BeginPrint
!= null)
181 protected virtual void OnEndPrint(PrintEventArgs e
){
183 if (EndPrint
!= null)
187 protected virtual void OnPrintPage(PrintPageEventArgs e
){
189 if (PrintPage
!= null)
193 protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e
){
195 if (QueryPageSettings
!= null)
196 QueryPageSettings(this, e
);
199 [SRDescription ("Raised when printing begins")]
200 public event PrintEventHandler BeginPrint
;
202 [SRDescription ("Raised when printing ends")]
203 public event PrintEventHandler EndPrint
;
205 [SRDescription ("Raised when printing of a new page begins")]
206 public event PrintPageEventHandler PrintPage
;
208 [SRDescription ("Raised before printing of a new page begins")]
209 public event QueryPageSettingsEventHandler QueryPageSettings
;