PrinterSettings.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
  57. internal PrinterSettings.PaperSizeCollection paper_sizes;
  58. public PrinterSettings() : this (SysPrn.Service.DefaultPrinter)
  59. {
  60. }
  61. internal PrinterSettings (string printer)
  62. {
  63. printer_name = printer;
  64. ResetToDefaults ();
  65. SysPrn.Service.LoadPrinterSettings (printer_name, this);
  66. }
  67. private void ResetToDefaults ()
  68. {
  69. printer_resolutions = null;
  70. paper_sizes = null;
  71. maximum_page = 9999;
  72. }
  73. // Public subclasses
  74. public class PaperSourceCollection : ICollection, IEnumerable
  75. {
  76. ArrayList _PaperSources = new ArrayList();
  77. public PaperSourceCollection(PaperSource[] array) {
  78. foreach (PaperSource ps in array)
  79. _PaperSources.Add(ps);
  80. }
  81. public int Count { get { return _PaperSources.Count; } }
  82. int ICollection.Count { get { return _PaperSources.Count; } }
  83. bool ICollection.IsSynchronized { get { return false; } }
  84. object ICollection.SyncRoot { get { return this; } }
  85. #if NET_2_0
  86. [EditorBrowsable(EditorBrowsableState.Never)]
  87. public int Add (PaperSource paperSource) {throw new NotImplementedException (); }
  88. public void CopyTo (PaperSource[] paperSources, int index) {throw new NotImplementedException (); }
  89. #endif
  90. public virtual PaperSource this[int index] {
  91. get { return _PaperSources[index] as PaperSource; }
  92. }
  93. IEnumerator IEnumerable.GetEnumerator()
  94. {
  95. return _PaperSources.GetEnumerator();
  96. }
  97. public IEnumerator GetEnumerator()
  98. {
  99. return _PaperSources.GetEnumerator();
  100. }
  101. void ICollection.CopyTo(Array array, int index)
  102. {
  103. _PaperSources.CopyTo(array, index);
  104. }
  105. }
  106. public class PaperSizeCollection : ICollection, IEnumerable
  107. {
  108. ArrayList _PaperSizes = new ArrayList();
  109. public PaperSizeCollection(PaperSize[] array) {
  110. foreach (PaperSize ps in array)
  111. _PaperSizes.Add(ps);
  112. }
  113. public int Count { get { return _PaperSizes.Count; } }
  114. int ICollection.Count { get { return _PaperSizes.Count; } }
  115. bool ICollection.IsSynchronized { get { return false; } }
  116. object ICollection.SyncRoot { get { return this; } }
  117. #if NET_2_0
  118. [EditorBrowsable(EditorBrowsableState.Never)]
  119. public int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  120. public void CopyTo (PaperSize[] paperSizes, int index) {throw new NotImplementedException (); }
  121. #else
  122. internal int Add (PaperSize paperSize) {return _PaperSizes.Add (paperSize); }
  123. #endif
  124. public virtual PaperSize this[int index] {
  125. get { return _PaperSizes[index] as PaperSize; }
  126. }
  127. IEnumerator IEnumerable.GetEnumerator()
  128. {
  129. return _PaperSizes.GetEnumerator();
  130. }
  131. public IEnumerator GetEnumerator()
  132. {
  133. return _PaperSizes.GetEnumerator();
  134. }
  135. void ICollection.CopyTo(Array array, int index)
  136. {
  137. _PaperSizes.CopyTo(array, index);
  138. }
  139. internal void Clear ()
  140. {
  141. _PaperSizes.Clear ();
  142. }
  143. }
  144. public class PrinterResolutionCollection : ICollection, IEnumerable
  145. {
  146. ArrayList _PrinterResolutions = new ArrayList();
  147. public PrinterResolutionCollection(PrinterResolution[] array) {
  148. foreach (PrinterResolution pr in array)
  149. _PrinterResolutions.Add(pr);
  150. }
  151. public int Count { get { return _PrinterResolutions.Count; } }
  152. int ICollection.Count { get { return _PrinterResolutions.Count; } }
  153. bool ICollection.IsSynchronized { get { return false; } }
  154. object ICollection.SyncRoot { get { return this; } }
  155. #if NET_2_0
  156. [EditorBrowsable(EditorBrowsableState.Never)]
  157. public int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  158. public void CopyTo (PrinterResolution[] printerResolutions, int index) {throw new NotImplementedException (); }
  159. #else
  160. internal int Add (PrinterResolution printerResolution) { return _PrinterResolutions.Add (printerResolution); }
  161. #endif
  162. public virtual PrinterResolution this[int index] {
  163. get { return _PrinterResolutions[index] as PrinterResolution; }
  164. }
  165. IEnumerator IEnumerable.GetEnumerator()
  166. {
  167. return _PrinterResolutions.GetEnumerator();
  168. }
  169. public IEnumerator GetEnumerator()
  170. {
  171. return _PrinterResolutions.GetEnumerator();
  172. }
  173. void ICollection.CopyTo(Array array, int index)
  174. {
  175. _PrinterResolutions.CopyTo(array, index);
  176. }
  177. internal void Clear ()
  178. {
  179. _PrinterResolutions.Clear ();
  180. }
  181. }
  182. public class StringCollection : ICollection, IEnumerable
  183. {
  184. ArrayList _Strings = new ArrayList();
  185. public StringCollection(string[] array) {
  186. foreach (string s in array)
  187. _Strings.Add(s);
  188. }
  189. public int Count { get { return _Strings.Count; } }
  190. int ICollection.Count { get { return _Strings.Count; } }
  191. bool ICollection.IsSynchronized { get { return false; } }
  192. object ICollection.SyncRoot { get { return this; } }
  193. public virtual string this[int index] {
  194. get { return _Strings[index] as string; }
  195. }
  196. #if NET_2_0
  197. [EditorBrowsable(EditorBrowsableState.Never)]
  198. public int Add (string value) { return _Strings.Add (value); }
  199. public void CopyTo (string[] strings, int index) {throw new NotImplementedException (); }
  200. #else
  201. internal int Add (string value) { return _Strings.Add (value); }
  202. #endif
  203. IEnumerator IEnumerable.GetEnumerator()
  204. {
  205. return _Strings.GetEnumerator();
  206. }
  207. public IEnumerator GetEnumerator()
  208. {
  209. return _Strings.GetEnumerator();
  210. }
  211. void ICollection.CopyTo(Array array, int index)
  212. {
  213. _Strings.CopyTo(array, index);
  214. }
  215. }
  216. //properties
  217. public bool CanDuplex
  218. {
  219. get { return can_duplex; }
  220. }
  221. public bool Collate
  222. {
  223. get { return collate; }
  224. set { collate = value; }
  225. }
  226. public short Copies
  227. {
  228. get { return copies; }
  229. set {
  230. if (value < 0)
  231. throw new ArgumentException ("The value of the Copies property is less than zero.");
  232. copies = value;
  233. }
  234. }
  235. [MonoTODO("PrinterSettings.DefaultPageSettings")]
  236. public PageSettings DefaultPageSettings
  237. {
  238. get
  239. {
  240. return new PageSettings(
  241. this,
  242. // TODO: get default color mode for this printer
  243. false,
  244. // TODO: get default orientation for this printer
  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. [MonoTODO("PrinterSettings.PrintToFile")]
  368. public bool PrintToFile
  369. {
  370. get { throw new NotImplementedException(); }
  371. set { throw new NotImplementedException(); }
  372. }
  373. public bool SupportsColor
  374. {
  375. get { return supports_color; }
  376. }
  377. public int ToPage
  378. {
  379. get { return to_page; }
  380. set {
  381. if (value < 0)
  382. throw new ArgumentException ("The value of the ToPage property is less than zero");
  383. to_page = value;
  384. }
  385. }
  386. //methods
  387. public object Clone ()
  388. {
  389. PrinterSettings ps = new PrinterSettings (printer_name);
  390. return ps;
  391. }
  392. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  393. public Graphics CreateMeasurementGraphics()
  394. {
  395. throw new NotImplementedException();
  396. }
  397. #if NET_2_0
  398. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  399. public Graphics CreateMeasurementGraphics(bool honorOriginAtMargins)
  400. {
  401. throw new NotImplementedException();
  402. }
  403. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  404. public Graphics CreateMeasurementGraphics(PageSettings pageSettings)
  405. {
  406. throw new NotImplementedException();
  407. }
  408. [MonoTODO("PrinterSettings.CreateMeasurementGraphics")]
  409. public Graphics CreateMeasurementGraphics (PageSettings pageSettings, bool honorOriginAtMargins)
  410. {
  411. throw new NotImplementedException();
  412. }
  413. #endif
  414. [MonoTODO("PrinterSettings.GetHdevmode")]
  415. public IntPtr GetHdevmode()
  416. {
  417. throw new NotImplementedException();
  418. }
  419. [MonoTODO("PrinterSettings.GetHdevmode")]
  420. public IntPtr GetHdevmode(PageSettings pageSettings)
  421. {
  422. throw new NotImplementedException();
  423. }
  424. [MonoTODO("PrinterSettings.GetHdevname")]
  425. public IntPtr GetHdevnames()
  426. {
  427. throw new NotImplementedException();
  428. }
  429. #if NET_2_0
  430. [MonoTODO("IsDirectPrintingSupported")]
  431. public bool IsDirectPrintingSupported (Image image)
  432. {
  433. throw new NotImplementedException();
  434. }
  435. [MonoTODO("IsDirectPrintingSupported")]
  436. public bool IsDirectPrintingSupported (ImageFormat imageFormat)
  437. {
  438. throw new NotImplementedException();
  439. }
  440. #endif
  441. [MonoTODO("PrinterSettings.SetHdevmode")]
  442. public void SetHdevmode(IntPtr hdevmode)
  443. {
  444. throw new NotImplementedException();
  445. }
  446. [MonoTODO("PrinterSettings.SetHdevnames")]
  447. public void SetHdevnames(IntPtr hdevnames)
  448. {
  449. throw new NotImplementedException();
  450. }
  451. [MonoTODO("PrinterSettings.ToString")]
  452. public override string ToString()
  453. {
  454. throw new NotImplementedException();
  455. }
  456. }
  457. }