PrinterSettings.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. //
  2. // System.Drawing.PrinterSettings.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. // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Runtime.InteropServices;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Drawing.Imaging;
  35. namespace System.Drawing.Printing
  36. {
  37. [Serializable]
  38. #if ! NET_2_0
  39. [ComVisible(false)]
  40. #endif
  41. public class PrinterSettings : ICloneable
  42. {
  43. private string printer_name;
  44. private string print_filename;
  45. private short copies;
  46. private int maximum_page;
  47. private int minimum_page;
  48. private int from_page;
  49. private int to_page;
  50. private bool collate;
  51. private PrintRange print_range;
  52. internal int maximum_copies;
  53. internal bool can_duplex;
  54. internal bool supports_color;
  55. internal int landscape_angle;
  56. private bool print_tofile;
  57. internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
  58. internal PrinterSettings.PaperSizeCollection paper_sizes;
  59. public PrinterSettings() : this (SysPrn.Service.DefaultPrinter)
  60. {
  61. print_tofile = false;
  62. }
  63. internal PrinterSettings (string printer)
  64. {
  65. printer_name = printer;
  66. ResetToDefaults ();
  67. SysPrn.Service.LoadPrinterSettings (printer_name, this);
  68. }
  69. private void ResetToDefaults ()
  70. {
  71. printer_resolutions = null;
  72. paper_sizes = null;
  73. maximum_page = 9999;
  74. }
  75. // Public subclasses
  76. public class PaperSourceCollection : ICollection, IEnumerable
  77. {
  78. ArrayList _PaperSources = new ArrayList();
  79. public PaperSourceCollection(PaperSource[] array) {
  80. foreach (PaperSource ps in array)
  81. _PaperSources.Add(ps);
  82. }
  83. public int Count { get { return _PaperSources.Count; } }
  84. int ICollection.Count { get { return _PaperSources.Count; } }
  85. bool ICollection.IsSynchronized { get { return false; } }
  86. object ICollection.SyncRoot { get { return this; } }
  87. #if NET_2_0
  88. [EditorBrowsable(EditorBrowsableState.Never)]
  89. public int Add (PaperSource paperSource) {throw new NotImplementedException (); }
  90. public void CopyTo (PaperSource[] paperSources, int index) {throw new NotImplementedException (); }
  91. #endif
  92. public virtual PaperSource this[int index] {
  93. get { return _PaperSources[index] as PaperSource; }
  94. }
  95. IEnumerator IEnumerable.GetEnumerator()
  96. {
  97. return _PaperSources.GetEnumerator();
  98. }
  99. public IEnumerator GetEnumerator()
  100. {
  101. return _PaperSources.GetEnumerator();
  102. }
  103. void ICollection.CopyTo(Array array, int index)
  104. {
  105. _PaperSources.CopyTo(array, index);
  106. }
  107. }
  108. public class PaperSizeCollection : ICollection, IEnumerable
  109. {
  110. ArrayList _PaperSizes = new ArrayList();
  111. public PaperSizeCollection(PaperSize[] array) {
  112. foreach (PaperSize ps in array)
  113. _PaperSizes.Add(ps);
  114. }
  115. public int Count { get { return _PaperSizes.Count; } }
  116. int ICollection.Count { get { return _PaperSizes.Count; } }
  117. bool ICollection.IsSynchronized { get { return false; } }
  118. object ICollection.SyncRoot { get { return this; } }
  119. #if NET_2_0
  120. [EditorBrowsable(EditorBrowsableState.Never)]
  121. public int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  122. public void CopyTo (PaperSize[] paperSizes, int index) {throw new NotImplementedException (); }
  123. #else
  124. internal int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  125. #endif
  126. public virtual PaperSize this[int index] {
  127. get { return _PaperSizes[index] as PaperSize; }
  128. }
  129. IEnumerator IEnumerable.GetEnumerator()
  130. {
  131. return _PaperSizes.GetEnumerator();
  132. }
  133. public IEnumerator GetEnumerator()
  134. {
  135. return _PaperSizes.GetEnumerator();
  136. }
  137. void ICollection.CopyTo(Array array, int index)
  138. {
  139. _PaperSizes.CopyTo(array, index);
  140. }
  141. internal void Clear ()
  142. {
  143. _PaperSizes.Clear ();
  144. }
  145. }
  146. public class PrinterResolutionCollection : ICollection, IEnumerable
  147. {
  148. ArrayList _PrinterResolutions = new ArrayList();
  149. public PrinterResolutionCollection(PrinterResolution[] array) {
  150. foreach (PrinterResolution pr in array)
  151. _PrinterResolutions.Add(pr);
  152. }
  153. public int Count { get { return _PrinterResolutions.Count; } }
  154. int ICollection.Count { get { return _PrinterResolutions.Count; } }
  155. bool ICollection.IsSynchronized { get { return false; } }
  156. object ICollection.SyncRoot { get { return this; } }
  157. #if NET_2_0
  158. [EditorBrowsable(EditorBrowsableState.Never)]
  159. public int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  160. public void CopyTo (PrinterResolution[] printerResolutions, int index) {throw new NotImplementedException (); }
  161. #else
  162. internal int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  163. #endif
  164. public virtual PrinterResolution this[int index] {
  165. get { return _PrinterResolutions[index] as PrinterResolution; }
  166. }
  167. IEnumerator IEnumerable.GetEnumerator()
  168. {
  169. return _PrinterResolutions.GetEnumerator();
  170. }
  171. public IEnumerator GetEnumerator()
  172. {
  173. return _PrinterResolutions.GetEnumerator();
  174. }
  175. void ICollection.CopyTo(Array array, int index)
  176. {
  177. _PrinterResolutions.CopyTo(array, index);
  178. }
  179. internal void Clear ()
  180. {
  181. _PrinterResolutions.Clear ();
  182. }
  183. }
  184. public class StringCollection : ICollection, IEnumerable
  185. {
  186. ArrayList _Strings = new ArrayList();
  187. public StringCollection(string[] array) {
  188. foreach (string s in array)
  189. _Strings.Add(s);
  190. }
  191. public int Count { get { return _Strings.Count; } }
  192. int ICollection.Count { get { return _Strings.Count; } }
  193. bool ICollection.IsSynchronized { get { return false; } }
  194. object ICollection.SyncRoot { get { return this; } }
  195. public virtual string this[int index] {
  196. get { return _Strings[index] as string; }
  197. }
  198. #if NET_2_0
  199. [EditorBrowsable(EditorBrowsableState.Never)]
  200. public int Add (string value) { return _Strings.Add (value); }
  201. public void CopyTo (string[] strings, int index) {throw new NotImplementedException (); }
  202. #else
  203. internal int Add (string value) { return _Strings.Add (value); }
  204. #endif
  205. IEnumerator IEnumerable.GetEnumerator()
  206. {
  207. return _Strings.GetEnumerator();
  208. }
  209. public IEnumerator GetEnumerator()
  210. {
  211. return _Strings.GetEnumerator();
  212. }
  213. void ICollection.CopyTo(Array array, int index)
  214. {
  215. _Strings.CopyTo(array, index);
  216. }
  217. }
  218. //properties
  219. public bool CanDuplex
  220. {
  221. get { return can_duplex; }
  222. }
  223. public bool Collate
  224. {
  225. get { return collate; }
  226. set { collate = value; }
  227. }
  228. public short Copies
  229. {
  230. get { return copies; }
  231. set {
  232. if (value < 0)
  233. throw new ArgumentException ("The value of the Copies property is less than zero.");
  234. copies = value;
  235. }
  236. }
  237. [MonoTODO("PrinterSettings.DefaultPageSettings")]
  238. public PageSettings DefaultPageSettings
  239. {
  240. get
  241. {
  242. return new PageSettings(
  243. this,
  244. SupportsColor,
  245. false,
  246. // TODO: get default paper size for this printer
  247. new PaperSize("A4", 827, 1169),
  248. // TODO: get default paper source for this printer
  249. new PaperSource("default", PaperSourceKind.FormSource),
  250. // TODO: get default resolution for this printer
  251. new PrinterResolution(300, 300, PrinterResolutionKind.Medium)
  252. );
  253. }
  254. }
  255. [MonoTODO("PrinterSettings.Duplex")]
  256. public Duplex Duplex
  257. {
  258. get { throw new NotImplementedException(); }
  259. set { throw new NotImplementedException(); }
  260. }
  261. public int FromPage
  262. {
  263. get { return from_page; }
  264. set {
  265. if (value < 0)
  266. throw new ArgumentException ("The value of the FromPage property is less than zero");
  267. from_page = value;
  268. }
  269. }
  270. public static PrinterSettings.StringCollection InstalledPrinters
  271. {
  272. get { return SysPrn.Service.InstalledPrinters; }
  273. }
  274. public bool IsDefaultPrinter
  275. {
  276. get { return (printer_name == SysPrn.Service.DefaultPrinter); }
  277. }
  278. [MonoTODO("PrinterSettings.IsPlotter")]
  279. public bool IsPlotter
  280. {
  281. get { return false; }
  282. }
  283. [MonoTODO("PrinterSettings.IsValid")]
  284. public bool IsValid
  285. {
  286. get { return true; }
  287. }
  288. public int LandscapeAngle
  289. {
  290. get { return landscape_angle; }
  291. }
  292. public int MaximumCopies
  293. {
  294. get { return maximum_copies; }
  295. }
  296. public int MaximumPage
  297. {
  298. get { return maximum_page; }
  299. set {
  300. // This not documented but behaves like MinimumPage
  301. if (value < 0)
  302. throw new ArgumentException ("The value of the MaximumPage property is less than zero");
  303. maximum_page = value;
  304. }
  305. }
  306. public int MinimumPage
  307. {
  308. get { return minimum_page; }
  309. set {
  310. if (value < 0)
  311. throw new ArgumentException ("The value of the MaximumPage property is less than zero");
  312. minimum_page = value;
  313. }
  314. }
  315. public PrinterSettings.PaperSizeCollection PaperSizes
  316. {
  317. get {
  318. if (paper_sizes == null) {
  319. paper_sizes = new PrinterSettings.PaperSizeCollection (new PaperSize [] {});
  320. SysPrn.Service.LoadPrinterPaperSizes (printer_name, this);
  321. }
  322. return paper_sizes;
  323. }
  324. }
  325. [MonoTODO("PrinterSettings.PaperSources")]
  326. public PrinterSettings.PaperSourceCollection PaperSources
  327. {
  328. get { throw new NotImplementedException(); }
  329. }
  330. #if NET_2_0
  331. public string PrintFileName
  332. {
  333. get { return print_filename; }
  334. set { print_filename = value; }
  335. }
  336. #endif
  337. public string PrinterName
  338. {
  339. get { return printer_name; }
  340. set {
  341. if (printer_name == value)
  342. return;
  343. printer_name = value;
  344. SysPrn.Service.LoadPrinterSettings (printer_name, this);
  345. }
  346. }
  347. public PrinterSettings.PrinterResolutionCollection PrinterResolutions
  348. {
  349. get {
  350. if (printer_resolutions == null) {
  351. printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
  352. SysPrn.Service.LoadPrinterResolutions (printer_name, this);
  353. }
  354. return printer_resolutions;
  355. }
  356. }
  357. public PrintRange PrintRange
  358. {
  359. get { return print_range; }
  360. set {
  361. if (value != PrintRange.AllPages && value != PrintRange.Selection &&
  362. value != PrintRange.SomePages)
  363. throw new InvalidEnumArgumentException ("The value of the PrintRange property is not one of the PrintRange values");
  364. print_range = value;
  365. }
  366. }
  367. public bool PrintToFile
  368. {
  369. get { return print_tofile; }
  370. set { print_tofile = value; }
  371. }
  372. public bool SupportsColor
  373. {
  374. get { return supports_color; }
  375. }
  376. public int ToPage
  377. {
  378. get { return to_page; }
  379. set {
  380. if (value < 0)
  381. throw new ArgumentException ("The value of the ToPage property is less than zero");
  382. to_page = value;
  383. }
  384. }
  385. //methods
  386. public object Clone ()
  387. {
  388. PrinterSettings ps = new PrinterSettings (printer_name);
  389. return ps;
  390. }
  391. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  392. public Graphics CreateMeasurementGraphics()
  393. {
  394. throw new NotImplementedException();
  395. }
  396. #if NET_2_0
  397. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  398. public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)
  399. {
  400. throw new NotImplementedException();
  401. }
  402. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  403. public Graphics CreateMeasurementGraphics(PageSettings pageSettings)
  404. {
  405. throw new NotImplementedException();
  406. }
  407. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  408. public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)
  409. {
  410. throw new NotImplementedException();
  411. }
  412. #endif
  413. [MonoTODO("PrinterSettings.GetHdevmode")]
  414. public IntPtr GetHdevmode()
  415. {
  416. throw new NotImplementedException();
  417. }
  418. [MonoTODO("PrinterSettings.GetHdevmode")]
  419. public IntPtr GetHdevmode(PageSettings pageSettings)
  420. {
  421. throw new NotImplementedException();
  422. }
  423. [MonoTODO("PrinterSettings.GetHdevname")]
  424. public IntPtr GetHdevnames()
  425. {
  426. throw new NotImplementedException();
  427. }
  428. #if NET_2_0
  429. [MonoTODO("IsDirectPrintingSupported")]
  430. public bool IsDirectPrintingSupported (Image image)
  431. {
  432. throw new NotImplementedException();
  433. }
  434. [MonoTODO("IsDirectPrintingSupported")]
  435. public bool IsDirectPrintingSupported (ImageFormat imageFormat)
  436. {
  437. throw new NotImplementedException();
  438. }
  439. #endif
  440. [MonoTODO("PrinterSettings.SetHdevmode")]
  441. public void SetHdevmode(IntPtr hdevmode)
  442. {
  443. throw new NotImplementedException();
  444. }
  445. [MonoTODO("PrinterSettings.SetHdevnames")]
  446. public void SetHdevnames(IntPtr hdevnames)
  447. {
  448. throw new NotImplementedException();
  449. }
  450. public override string ToString()
  451. {
  452. return "Printer [PrinterSettings" + printer_name + " Copies=" + copies + " Collate=" + collate
  453. + " Duplex=" + can_duplex + " FromPage=" + from_page + " LandscapeAngle=" + landscape_angle
  454. + " MaximumCopies=" + maximum_copies + " OutputPort=" + " ToPage=" + to_page + "]";
  455. }
  456. }
  457. }