PrintingServices.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, [email protected]
  26. //
  27. using System.Runtime.InteropServices;
  28. using System.Collections;
  29. using System.Drawing.Printing;
  30. using System.ComponentModel;
  31. using System.Drawing.Imaging;
  32. namespace System.Drawing.Printing
  33. {
  34. /// <summary>
  35. /// This class is designed to cache the values retrieved by the
  36. /// native printing services, as opposed to GlobalPrintingServices, which
  37. /// doesn't cache any values.
  38. /// </summary>
  39. internal abstract class PrintingServices
  40. {
  41. #region Properties
  42. internal abstract string DefaultPrinter { get; }
  43. #endregion
  44. #region Methods
  45. internal abstract bool IsPrinterValid(string printer);
  46. internal abstract void LoadPrinterSettings (string printer, PrinterSettings settings);
  47. internal abstract void LoadPrinterResolutions (string printer, PrinterSettings settings);
  48. // Used from SWF
  49. internal abstract void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment);
  50. internal void LoadDefaultResolutions (PrinterSettings.PrinterResolutionCollection col)
  51. {
  52. col.Add (new PrinterResolution ((int) PrinterResolutionKind.High, -1, PrinterResolutionKind.High));
  53. col.Add (new PrinterResolution ((int) PrinterResolutionKind.Medium, -1, PrinterResolutionKind.Medium));
  54. col.Add (new PrinterResolution ((int) PrinterResolutionKind.Low, -1, PrinterResolutionKind.Low));
  55. col.Add (new PrinterResolution ((int) PrinterResolutionKind.Draft, -1, PrinterResolutionKind.Draft));
  56. }
  57. #endregion
  58. }
  59. internal abstract class GlobalPrintingServices
  60. {
  61. #region Properties
  62. internal abstract PrinterSettings.StringCollection InstalledPrinters { get; }
  63. #endregion
  64. #region Methods
  65. internal abstract IntPtr CreateGraphicsContext (PrinterSettings settings, PageSettings page_settings);
  66. internal abstract bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file);
  67. internal abstract bool StartPage (GraphicsPrinter gr);
  68. internal abstract bool EndPage (GraphicsPrinter gr);
  69. internal abstract bool EndDoc (GraphicsPrinter gr);
  70. #endregion
  71. }
  72. internal class SysPrn
  73. {
  74. static GlobalPrintingServices global_printing_services;
  75. static bool is_unix;
  76. static SysPrn ()
  77. {
  78. is_unix = GDIPlus.RunningOnUnix ();
  79. }
  80. internal static PrintingServices CreatePrintingService () {
  81. if (is_unix)
  82. return new PrintingServicesUnix ();
  83. return new PrintingServicesWin32 ();
  84. }
  85. internal static GlobalPrintingServices GlobalService {
  86. get {
  87. if (global_printing_services == null) {
  88. if (is_unix)
  89. global_printing_services = new GlobalPrintingServicesUnix ();
  90. else
  91. global_printing_services = new GlobalPrintingServicesWin32 ();
  92. }
  93. return global_printing_services;
  94. }
  95. }
  96. internal static void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment)
  97. {
  98. CreatePrintingService().GetPrintDialogInfo (printer, ref port, ref type, ref status, ref comment);
  99. }
  100. internal class Printer {
  101. public readonly string Name;
  102. public readonly string Comment;
  103. public readonly string Port;
  104. public readonly string Type;
  105. public readonly string Status;
  106. public PrinterSettings Settings;
  107. public bool IsDefault;
  108. public Printer (string port, string type, string status, string comment) {
  109. Port = port;
  110. Type = type;
  111. Status = status;
  112. Comment = comment;
  113. }
  114. }
  115. }
  116. internal class GraphicsPrinter
  117. {
  118. private Graphics graphics;
  119. private IntPtr hDC;
  120. internal GraphicsPrinter (Graphics gr, IntPtr dc)
  121. {
  122. graphics = gr;
  123. hDC = dc;
  124. }
  125. internal Graphics Graphics {
  126. get { return graphics; }
  127. set { graphics = value; }
  128. }
  129. internal IntPtr Hdc { get { return hDC; }}
  130. }
  131. }