2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PrintingServices.cs
1 //
2 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Author:
24 //
25 //      Jordi Mas i Hernandez, jordimash@gmail.com
26 //
27
28 using System.Runtime.InteropServices;
29 using System.Collections;
30 using System.Drawing.Printing;
31 using System.ComponentModel;
32 using System.Drawing.Imaging;
33
34 namespace System.Drawing.Printing
35 {
36         /// <summary>
37         /// This class is designed to cache the values retrieved by the 
38         /// native printing services, as opposed to GlobalPrintingServices, which
39         /// doesn't cache any values.
40         /// </summary>
41         internal abstract class PrintingServices
42         {
43                 #region Properties
44                 internal abstract string DefaultPrinter { get; }
45                 #endregion
46
47                 #region Methods
48                 internal abstract bool IsPrinterValid(string printer);
49                 internal abstract void LoadPrinterSettings (string printer, PrinterSettings settings);
50                 internal abstract void LoadPrinterResolutions (string printer, PrinterSettings settings);
51
52                 // Used from SWF
53                 internal abstract void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment);
54                 
55                 internal void LoadDefaultResolutions (PrinterSettings.PrinterResolutionCollection col)
56                 {
57                         col.Add (new PrinterResolution ((int) PrinterResolutionKind.High, -1, PrinterResolutionKind.High));
58                         col.Add (new PrinterResolution ((int) PrinterResolutionKind.Medium, -1, PrinterResolutionKind.Medium));
59                         col.Add (new PrinterResolution ((int) PrinterResolutionKind.Low, -1, PrinterResolutionKind.Low));
60                         col.Add (new PrinterResolution ((int) PrinterResolutionKind.Draft, -1, PrinterResolutionKind.Draft));
61                 }
62                 #endregion
63         }
64         
65         internal abstract class GlobalPrintingServices
66         {
67                 #region Properties
68                 internal abstract PrinterSettings.StringCollection InstalledPrinters { get; }
69                 #endregion
70
71                 #region Methods
72                 internal abstract IntPtr CreateGraphicsContext (PrinterSettings settings, PageSettings page_settings);
73
74                 internal abstract bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file);
75                 internal abstract bool StartPage (GraphicsPrinter gr);
76                 internal abstract bool EndPage (GraphicsPrinter gr);
77                 internal abstract bool EndDoc (GraphicsPrinter gr);
78                 #endregion
79         
80         }
81
82         internal class SysPrn
83         {
84                 static GlobalPrintingServices global_printing_services;
85                 static bool is_unix;
86
87                 static SysPrn ()
88                 {
89                         is_unix = GDIPlus.RunningOnUnix ();
90                 }
91                 
92                 internal static PrintingServices CreatePrintingService () {
93                         if (is_unix)
94                                 return new PrintingServicesUnix ();
95                         return new PrintingServicesWin32 ();                            
96                 }                       
97
98                 internal static GlobalPrintingServices GlobalService {
99                         get {\r
100                                 if (global_printing_services == null) {\r
101                                         if (is_unix)\r
102                                                 global_printing_services = new GlobalPrintingServicesUnix ();\r
103                                         else\r
104                                                 global_printing_services = new GlobalPrintingServicesWin32 ();\r
105                                 }\r
106 \r
107                                 return global_printing_services;\r
108                         }
109                 }
110
111                 internal static void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment) 
112                 {
113                         CreatePrintingService().GetPrintDialogInfo (printer, ref port, ref type, ref status, ref comment);
114                 }
115
116                 internal class Printer {
117                         public readonly string Name;
118                         public readonly string Comment;
119                         public readonly string Port;
120                         public readonly string Type;
121                         public readonly string Status;
122                         public PrinterSettings Settings;
123                         public bool IsDefault;
124                         
125                         public Printer (string port, string type, string status, string comment) {
126                                 Port = port;
127                                 Type = type;
128                                 Status = status;
129                                 Comment = comment;
130                         }
131                 }
132         }
133         
134         internal class GraphicsPrinter
135         {
136                 private Graphics graphics;
137                 private IntPtr  hDC;
138                  
139                 internal GraphicsPrinter (Graphics gr, IntPtr dc)
140                 {
141                         graphics = gr;
142                         hDC = dc;
143                 }
144                                                 
145                 internal Graphics Graphics { 
146                         get { return graphics; }
147                         set { graphics = value; }
148                 }
149                 internal IntPtr Hdc { get { return hDC; }}
150         }
151 }
152
153