PrinterSettings.cs 14 KB

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