PageSettings.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // System.Drawing.PageSettings.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002 Ximian, Inc
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Runtime.InteropServices;
  35. namespace System.Drawing.Printing
  36. {
  37. #if NET_2_0
  38. [Serializable]
  39. #else
  40. [ComVisible (false)]
  41. #endif
  42. public class PageSettings : ICloneable
  43. {
  44. internal bool color;
  45. internal bool landscape;
  46. internal PaperSize paperSize;
  47. internal PaperSource paperSource;
  48. internal PrinterResolution printerResolution;
  49. // create a new default Margins object (is 1 inch for all margins)
  50. Margins margins = new Margins();
  51. float hardMarginX;
  52. float hardMarginY;
  53. RectangleF printableArea;
  54. PrinterSettings printerSettings;
  55. public PageSettings() : this(new PrinterSettings())
  56. {
  57. }
  58. public PageSettings(PrinterSettings printerSettings)
  59. {
  60. PrinterSettings = printerSettings;
  61. this.color = printerSettings.DefaultPageSettings.color;
  62. this.landscape = printerSettings.DefaultPageSettings.landscape;
  63. this.paperSize = printerSettings.DefaultPageSettings.paperSize;
  64. this.paperSource = printerSettings.DefaultPageSettings.paperSource;
  65. this.printerResolution = printerSettings.DefaultPageSettings.printerResolution;
  66. }
  67. // used by PrinterSettings.DefaultPageSettings
  68. internal PageSettings(PrinterSettings printerSettings, bool color, bool landscape, PaperSize paperSize, PaperSource paperSource, PrinterResolution printerResolution)
  69. {
  70. PrinterSettings = printerSettings;
  71. this.color = color;
  72. this.landscape = landscape;
  73. this.paperSize = paperSize;
  74. this.paperSource = paperSource;
  75. this.printerResolution = printerResolution;
  76. }
  77. //props
  78. public Rectangle Bounds{
  79. get{
  80. int width = this.paperSize.Width;
  81. int height = this.paperSize.Height;
  82. width -= this.margins.Left + this.margins.Right;
  83. height -= this.margins.Top + this.margins.Bottom;
  84. if (this.landscape) {
  85. // swap width and height
  86. int tmp = width;
  87. width = height;
  88. height = tmp;
  89. }
  90. return new Rectangle (this.margins.Left, this.margins.Top, width, height);
  91. }
  92. }
  93. public bool Color{
  94. get{
  95. if (!this.printerSettings.IsValid)
  96. throw new InvalidPrinterException(this.printerSettings);
  97. return color;
  98. }
  99. set{
  100. color = value;
  101. }
  102. }
  103. public bool Landscape {
  104. get{
  105. if (!this.printerSettings.IsValid)
  106. throw new InvalidPrinterException(this.printerSettings);
  107. return landscape;
  108. }
  109. set{
  110. landscape = value;
  111. }
  112. }
  113. public Margins Margins{
  114. get{
  115. if (!this.printerSettings.IsValid)
  116. throw new InvalidPrinterException(this.printerSettings);
  117. return margins;
  118. }
  119. set{
  120. margins = value;
  121. }
  122. }
  123. public PaperSize PaperSize{
  124. get{
  125. if (!this.printerSettings.IsValid)
  126. throw new InvalidPrinterException(this.printerSettings);
  127. return paperSize;
  128. }
  129. set{
  130. if (value != null)
  131. paperSize = value;
  132. }
  133. }
  134. public PaperSource PaperSource{
  135. get{
  136. if (!this.printerSettings.IsValid)
  137. throw new InvalidPrinterException(this.printerSettings);
  138. return paperSource;
  139. }
  140. set{
  141. if (value != null)
  142. paperSource = value;
  143. }
  144. }
  145. public PrinterResolution PrinterResolution{
  146. get{
  147. if (!this.printerSettings.IsValid)
  148. throw new InvalidPrinterException(this.printerSettings);
  149. return printerResolution;
  150. }
  151. set{
  152. if (value != null)
  153. printerResolution = value;
  154. }
  155. }
  156. public PrinterSettings PrinterSettings{
  157. get{
  158. return printerSettings;
  159. }
  160. set{
  161. printerSettings = value;
  162. }
  163. }
  164. #if NET_2_0
  165. public float HardMarginX {
  166. get {
  167. return hardMarginX;
  168. }
  169. }
  170. public float HardMarginY {
  171. get {
  172. return hardMarginY;
  173. }
  174. }
  175. public RectangleF PrintableArea {
  176. get {
  177. return printableArea;
  178. }
  179. }
  180. #endif
  181. public object Clone ()
  182. {
  183. // We do a deep copy
  184. PrinterResolution pres = new PrinterResolution (this.printerResolution.X, this.printerResolution.Y, this.printerResolution.Kind);
  185. PaperSource psource = new PaperSource (this.paperSource.SourceName, this.paperSource.Kind);
  186. PaperSize psize = new PaperSize (this.paperSize.PaperName, this.paperSize.Width, this.paperSize.Height);
  187. psize.SetKind (this.paperSize.Kind);
  188. PageSettings ps = new PageSettings (this.printerSettings, this.color, this.landscape,
  189. psize, psource, pres);
  190. ps.Margins = (Margins) this.margins.Clone ();
  191. return ps;
  192. }
  193. [MonoTODO("PageSettings.CopyToHdevmode")]
  194. public void CopyToHdevmode (IntPtr hdevmode){
  195. throw new NotImplementedException ();
  196. }
  197. [MonoTODO("PageSettings.SetHdevmode")]
  198. public void SetHdevmode (IntPtr hdevmode){
  199. throw new NotImplementedException ();
  200. }
  201. public override string ToString(){
  202. string ret = "[PageSettings: Color={0}";
  203. ret += ", Landscape={1}";
  204. ret += ", Margins={2}";
  205. ret += ", PaperSize={3}";
  206. ret += ", PaperSource={4}";
  207. ret += ", PrinterResolution={5}";
  208. ret += "]";
  209. return String.Format(ret, this.color, this.landscape, this.margins, this.paperSize, this.paperSource, this.printerResolution);
  210. }
  211. }
  212. }