PrinterSettings.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.Collections.Specialized;
  34. using System.ComponentModel;
  35. using System.Drawing.Imaging;
  36. namespace System.Drawing.Printing
  37. {
  38. [Serializable]
  39. #if ! NET_2_0
  40. [ComVisible(false)]
  41. #endif
  42. public class PrinterSettings : ICloneable
  43. {
  44. private string printer_name;
  45. private string print_filename;
  46. private short copies;
  47. private int maximum_page;
  48. private int minimum_page;
  49. private int from_page;
  50. private int to_page;
  51. private bool collate;
  52. private PrintRange print_range;
  53. internal int maximum_copies;
  54. internal bool can_duplex;
  55. internal bool supports_color;
  56. internal int landscape_angle;
  57. private bool print_tofile;
  58. internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
  59. internal PrinterSettings.PaperSizeCollection paper_sizes;
  60. internal PrinterSettings.PaperSourceCollection paper_sources;
  61. private PageSettings default_pagesettings;
  62. private Duplex duplex;
  63. internal bool is_plotter;
  64. private PrintingServices printing_services;
  65. internal NameValueCollection printer_capabilities; // this stores a list of all the printer options. Used only in cups, but might come in handy on win too.
  66. public PrinterSettings() : this (SysPrn.CreatePrintingService ())
  67. {
  68. print_tofile = false;
  69. }
  70. internal PrinterSettings (PrintingServices printing_services)
  71. {
  72. this.printing_services = printing_services;
  73. printer_name = printing_services.DefaultPrinter;
  74. ResetToDefaults ();
  75. printing_services.LoadPrinterSettings (printer_name, this);
  76. }
  77. private void ResetToDefaults ()
  78. {
  79. printer_resolutions = null;
  80. paper_sizes = null;
  81. paper_sources = null;
  82. default_pagesettings = null;
  83. maximum_page = 9999;
  84. copies = 1;
  85. collate = true;
  86. }
  87. //properties
  88. public bool CanDuplex
  89. {
  90. get { return can_duplex; }
  91. }
  92. public bool Collate
  93. {
  94. get { return collate; }
  95. set { collate = value; }
  96. }
  97. public short Copies
  98. {
  99. get { return copies; }
  100. set {
  101. if (value < 0)
  102. throw new ArgumentException ("The value of the Copies property is less than zero.");
  103. copies = value;
  104. }
  105. }
  106. public PageSettings DefaultPageSettings
  107. {
  108. get {
  109. if (default_pagesettings == null) {
  110. default_pagesettings = new PageSettings (this,
  111. SupportsColor,
  112. false,
  113. // Real defaults are set by LoadPrinterSettings
  114. new PaperSize("A4", 827, 1169),
  115. new PaperSource("Tray", PaperSourceKind.FormSource),
  116. new PrinterResolution(200, 200, PrinterResolutionKind.Medium));
  117. }
  118. return default_pagesettings;
  119. }
  120. }
  121. public Duplex Duplex
  122. {
  123. get { return this.duplex; }
  124. set { this.duplex = value; }
  125. }
  126. public int FromPage
  127. {
  128. get { return from_page; }
  129. set {
  130. if (value < 0)
  131. throw new ArgumentException ("The value of the FromPage property is less than zero");
  132. from_page = value;
  133. }
  134. }
  135. public static PrinterSettings.StringCollection InstalledPrinters
  136. {
  137. get { return SysPrn.GlobalService.InstalledPrinters; }
  138. }
  139. public bool IsDefaultPrinter
  140. {
  141. get { return (printer_name == printing_services.DefaultPrinter); }
  142. }
  143. public bool IsPlotter
  144. {
  145. get { return is_plotter; }
  146. }
  147. public bool IsValid
  148. {
  149. get { return printing_services.IsPrinterValid (this.printer_name); }
  150. }
  151. public int LandscapeAngle
  152. {
  153. get { return landscape_angle; }
  154. }
  155. public int MaximumCopies
  156. {
  157. get { return maximum_copies; }
  158. }
  159. public int MaximumPage
  160. {
  161. get { return maximum_page; }
  162. set {
  163. // This not documented but behaves like MinimumPage
  164. if (value < 0)
  165. throw new ArgumentException ("The value of the MaximumPage property is less than zero");
  166. maximum_page = value;
  167. }
  168. }
  169. public int MinimumPage
  170. {
  171. get { return minimum_page; }
  172. set {
  173. if (value < 0)
  174. throw new ArgumentException ("The value of the MaximumPage property is less than zero");
  175. minimum_page = value;
  176. }
  177. }
  178. public PrinterSettings.PaperSizeCollection PaperSizes
  179. {
  180. get {
  181. if (!this.IsValid)
  182. throw new InvalidPrinterException(this);
  183. return paper_sizes;
  184. }
  185. }
  186. public PrinterSettings.PaperSourceCollection PaperSources
  187. {
  188. get {
  189. if (!this.IsValid)
  190. throw new InvalidPrinterException(this);
  191. return paper_sources;
  192. }
  193. }
  194. #if NET_2_0
  195. public
  196. #else
  197. internal
  198. #endif
  199. string PrintFileName
  200. {
  201. get { return print_filename; }
  202. set { print_filename = value; }
  203. }
  204. public string PrinterName
  205. {
  206. get { return printer_name; }
  207. set {
  208. if (printer_name == value)
  209. return;
  210. printer_name = value;
  211. printing_services.LoadPrinterSettings (printer_name, this);
  212. }
  213. }
  214. public PrinterSettings.PrinterResolutionCollection PrinterResolutions
  215. {
  216. get {
  217. if (!this.IsValid)
  218. throw new InvalidPrinterException(this);
  219. if (printer_resolutions == null) {
  220. printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
  221. printing_services.LoadPrinterResolutions (printer_name, this);
  222. }
  223. return printer_resolutions;
  224. }
  225. }
  226. public PrintRange PrintRange
  227. {
  228. get { return print_range; }
  229. set {
  230. if (value != PrintRange.AllPages && value != PrintRange.Selection &&
  231. value != PrintRange.SomePages)
  232. throw new InvalidEnumArgumentException ("The value of the PrintRange property is not one of the PrintRange values");
  233. print_range = value;
  234. }
  235. }
  236. public bool PrintToFile
  237. {
  238. get { return print_tofile; }
  239. set { print_tofile = value; }
  240. }
  241. public bool SupportsColor
  242. {
  243. get { return supports_color; }
  244. }
  245. public int ToPage
  246. {
  247. get { return to_page; }
  248. set {
  249. if (value < 0)
  250. throw new ArgumentException ("The value of the ToPage property is less than zero");
  251. to_page = value;
  252. }
  253. }
  254. internal NameValueCollection PrinterCapabilities {
  255. get {
  256. if (this.printer_capabilities == null)
  257. this.printer_capabilities = new NameValueCollection();
  258. return this.printer_capabilities;
  259. }
  260. }
  261. //methods
  262. public object Clone ()
  263. {
  264. PrinterSettings ps = new PrinterSettings (printing_services);
  265. return ps;
  266. }
  267. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  268. public Graphics CreateMeasurementGraphics()
  269. {
  270. throw new NotImplementedException();
  271. }
  272. #if NET_2_0
  273. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  274. public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)
  275. {
  276. throw new NotImplementedException();
  277. }
  278. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  279. public Graphics CreateMeasurementGraphics(PageSettings pageSettings)
  280. {
  281. throw new NotImplementedException();
  282. }
  283. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  284. public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)
  285. {
  286. throw new NotImplementedException();
  287. }
  288. #endif
  289. [MonoTODO("PrinterSettings.GetHdevmode")]
  290. public IntPtr GetHdevmode()
  291. {
  292. throw new NotImplementedException();
  293. }
  294. [MonoTODO("PrinterSettings.GetHdevmode")]
  295. public IntPtr GetHdevmode(PageSettings pageSettings)
  296. {
  297. throw new NotImplementedException();
  298. }
  299. [MonoTODO("PrinterSettings.GetHdevname")]
  300. public IntPtr GetHdevnames()
  301. {
  302. throw new NotImplementedException();
  303. }
  304. #if NET_2_0
  305. [MonoTODO("IsDirectPrintingSupported")]
  306. public bool IsDirectPrintingSupported (Image image)
  307. {
  308. throw new NotImplementedException();
  309. }
  310. [MonoTODO("IsDirectPrintingSupported")]
  311. public bool IsDirectPrintingSupported (ImageFormat imageFormat)
  312. {
  313. throw new NotImplementedException();
  314. }
  315. #endif
  316. [MonoTODO("PrinterSettings.SetHdevmode")]
  317. public void SetHdevmode(IntPtr hdevmode)
  318. {
  319. throw new NotImplementedException();
  320. }
  321. [MonoTODO("PrinterSettings.SetHdevnames")]
  322. public void SetHdevnames(IntPtr hdevnames)
  323. {
  324. throw new NotImplementedException();
  325. }
  326. public override string ToString()
  327. {
  328. return "Printer [PrinterSettings " + printer_name + " Copies=" + copies + " Collate=" + collate
  329. + " Duplex=" + can_duplex + " FromPage=" + from_page + " LandscapeAngle=" + landscape_angle
  330. + " MaximumCopies=" + maximum_copies + " OutputPort=" + " ToPage=" + to_page + "]";
  331. }
  332. // Public subclasses
  333. #region Public Subclasses
  334. public class PaperSourceCollection : ICollection, IEnumerable
  335. {
  336. ArrayList _PaperSources = new ArrayList();
  337. public PaperSourceCollection(PaperSource[] array) {
  338. foreach (PaperSource ps in array)
  339. _PaperSources.Add(ps);
  340. }
  341. public int Count { get { return _PaperSources.Count; } }
  342. int ICollection.Count { get { return _PaperSources.Count; } }
  343. bool ICollection.IsSynchronized { get { return false; } }
  344. object ICollection.SyncRoot { get { return this; } }
  345. #if NET_2_0
  346. [EditorBrowsable(EditorBrowsableState.Never)]
  347. public int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
  348. public void CopyTo (PaperSource[] paperSources, int index) {throw new NotImplementedException (); }
  349. #else
  350. internal int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
  351. #endif
  352. public virtual PaperSource this[int index] {
  353. get { return _PaperSources[index] as PaperSource; }
  354. }
  355. IEnumerator IEnumerable.GetEnumerator()
  356. {
  357. return _PaperSources.GetEnumerator();
  358. }
  359. public IEnumerator GetEnumerator()
  360. {
  361. return _PaperSources.GetEnumerator();
  362. }
  363. void ICollection.CopyTo(Array array, int index)
  364. {
  365. _PaperSources.CopyTo(array, index);
  366. }
  367. internal void Clear ()
  368. {
  369. _PaperSources.Clear ();
  370. }
  371. }
  372. public class PaperSizeCollection : ICollection, IEnumerable
  373. {
  374. ArrayList _PaperSizes = new ArrayList();
  375. public PaperSizeCollection(PaperSize[] array) {
  376. foreach (PaperSize ps in array)
  377. _PaperSizes.Add(ps);
  378. }
  379. public int Count { get { return _PaperSizes.Count; } }
  380. int ICollection.Count { get { return _PaperSizes.Count; } }
  381. bool ICollection.IsSynchronized { get { return false; } }
  382. object ICollection.SyncRoot { get { return this; } }
  383. #if NET_2_0
  384. [EditorBrowsable(EditorBrowsableState.Never)]
  385. public int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  386. public void CopyTo (PaperSize[] paperSizes, int index) {throw new NotImplementedException (); }
  387. #else
  388. internal int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  389. #endif
  390. public virtual PaperSize this[int index] {
  391. get { return _PaperSizes[index] as PaperSize; }
  392. }
  393. IEnumerator IEnumerable.GetEnumerator()
  394. {
  395. return _PaperSizes.GetEnumerator();
  396. }
  397. public IEnumerator GetEnumerator()
  398. {
  399. return _PaperSizes.GetEnumerator();
  400. }
  401. void ICollection.CopyTo(Array array, int index)
  402. {
  403. _PaperSizes.CopyTo(array, index);
  404. }
  405. internal void Clear ()
  406. {
  407. _PaperSizes.Clear ();
  408. }
  409. }
  410. public class PrinterResolutionCollection : ICollection, IEnumerable
  411. {
  412. ArrayList _PrinterResolutions = new ArrayList();
  413. public PrinterResolutionCollection(PrinterResolution[] array) {
  414. foreach (PrinterResolution pr in array)
  415. _PrinterResolutions.Add(pr);
  416. }
  417. public int Count { get { return _PrinterResolutions.Count; } }
  418. int ICollection.Count { get { return _PrinterResolutions.Count; } }
  419. bool ICollection.IsSynchronized { get { return false; } }
  420. object ICollection.SyncRoot { get { return this; } }
  421. #if NET_2_0
  422. [EditorBrowsable(EditorBrowsableState.Never)]
  423. public int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  424. public void CopyTo (PrinterResolution[] printerResolutions, int index) {throw new NotImplementedException (); }
  425. #else
  426. internal int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  427. #endif
  428. public virtual PrinterResolution this[int index] {
  429. get { return _PrinterResolutions[index] as PrinterResolution; }
  430. }
  431. IEnumerator IEnumerable.GetEnumerator()
  432. {
  433. return _PrinterResolutions.GetEnumerator();
  434. }
  435. public IEnumerator GetEnumerator()
  436. {
  437. return _PrinterResolutions.GetEnumerator();
  438. }
  439. void ICollection.CopyTo(Array array, int index)
  440. {
  441. _PrinterResolutions.CopyTo(array, index);
  442. }
  443. internal void Clear ()
  444. {
  445. _PrinterResolutions.Clear ();
  446. }
  447. }
  448. public class StringCollection : ICollection, IEnumerable
  449. {
  450. ArrayList _Strings = new ArrayList();
  451. public StringCollection(string[] array) {
  452. foreach (string s in array)
  453. _Strings.Add(s);
  454. }
  455. public int Count { get { return _Strings.Count; } }
  456. int ICollection.Count { get { return _Strings.Count; } }
  457. bool ICollection.IsSynchronized { get { return false; } }
  458. object ICollection.SyncRoot { get { return this; } }
  459. public virtual string this[int index] {
  460. get { return _Strings[index] as string; }
  461. }
  462. #if NET_2_0
  463. [EditorBrowsable(EditorBrowsableState.Never)]
  464. public int Add (string value) { return _Strings.Add (value); }
  465. public void CopyTo (string[] strings, int index) {throw new NotImplementedException (); }
  466. #else
  467. internal int Add (string value) { return _Strings.Add (value); }
  468. #endif
  469. IEnumerator IEnumerable.GetEnumerator()
  470. {
  471. return _Strings.GetEnumerator();
  472. }
  473. public IEnumerator GetEnumerator()
  474. {
  475. return _Strings.GetEnumerator();
  476. }
  477. void ICollection.CopyTo(Array array, int index)
  478. {
  479. _Strings.CopyTo(array, index);
  480. }
  481. }
  482. #endregion
  483. void GetPrintDialogInfo (string printer_name, ref string port, ref string type, ref string status, ref string comment)
  484. {
  485. printing_services.GetPrintDialogInfo (printer_name, ref port, ref type, ref status, ref comment);
  486. }
  487. }
  488. }