PrinterSettings.cs 14 KB

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