PrinterSettings.cs 14 KB

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