CefPrintSettings.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class representing print settings.
  10. /// </summary>
  11. public sealed unsafe partial class CefPrintSettings
  12. {
  13. /// <summary>
  14. /// Create a new CefPrintSettings object.
  15. /// </summary>
  16. public static CefPrintSettings Create()
  17. {
  18. return CefPrintSettings.FromNative(
  19. cef_print_settings_t.create()
  20. );
  21. }
  22. /// <summary>
  23. /// Returns true if this object is valid. Do not call any other methods if this
  24. /// function returns false.
  25. /// </summary>
  26. public bool IsValid
  27. {
  28. get
  29. {
  30. return cef_print_settings_t.is_valid(_self) != 0;
  31. }
  32. }
  33. /// <summary>
  34. /// Returns true if the values of this object are read-only. Some APIs may
  35. /// expose read-only objects.
  36. /// </summary>
  37. public bool IsReadOnly
  38. {
  39. get
  40. {
  41. return cef_print_settings_t.is_read_only(_self) != 0;
  42. }
  43. }
  44. /// <summary>
  45. /// Set the page orientation.
  46. /// </summary>
  47. public void SetOrientation(bool landscape)
  48. {
  49. cef_print_settings_t.set_orientation(_self, landscape ? 1 : 0);
  50. }
  51. /// <summary>
  52. /// Returns true if the orientation is landscape.
  53. /// </summary>
  54. public bool IsLandscape()
  55. {
  56. return cef_print_settings_t.is_landscape(_self) != 0;
  57. }
  58. /// <summary>
  59. /// Set the printer printable area in device units.
  60. /// Some platforms already provide flipped area. Set |landscape_needs_flip|
  61. /// to false on those platforms to avoid double flipping.
  62. /// </summary>
  63. public void SetPrinterPrintableArea(CefSize physicalSizeDeviceUnits, CefRectangle printableAreaDeviceUnits, bool landscapeNeedsFlip)
  64. {
  65. var n_physicalSizeDeviceUnits = new cef_size_t(
  66. physicalSizeDeviceUnits.Width,
  67. physicalSizeDeviceUnits.Height
  68. );
  69. var n_printableAreaDeviceUnits = new cef_rect_t(
  70. printableAreaDeviceUnits.X,
  71. printableAreaDeviceUnits.Y,
  72. printableAreaDeviceUnits.Width,
  73. printableAreaDeviceUnits.Height
  74. );
  75. cef_print_settings_t.set_printer_printable_area(
  76. _self,
  77. &n_physicalSizeDeviceUnits,
  78. &n_printableAreaDeviceUnits,
  79. landscapeNeedsFlip ? 1 : 0
  80. );
  81. }
  82. /// <summary>
  83. /// Set the device name.
  84. /// </summary>
  85. public void SetDeviceName(string name)
  86. {
  87. fixed (char* name_str = name)
  88. {
  89. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  90. cef_print_settings_t.set_device_name(_self, &n_name);
  91. }
  92. }
  93. /// <summary>
  94. /// Get the device name.
  95. /// </summary>
  96. public string DeviceName
  97. {
  98. get
  99. {
  100. var n_result = cef_print_settings_t.get_device_name(_self);
  101. if (n_result == null) return null;
  102. return cef_string_userfree.ToString(n_result);
  103. }
  104. }
  105. /// <summary>
  106. /// Set the DPI (dots per inch).
  107. /// </summary>
  108. public void SetDpi(int dpi)
  109. {
  110. cef_print_settings_t.set_dpi(_self, dpi);
  111. }
  112. /// <summary>
  113. /// Get the DPI (dots per inch).
  114. /// </summary>
  115. public int GetDpi()
  116. {
  117. return cef_print_settings_t.get_dpi(_self);
  118. }
  119. /// <summary>
  120. /// Set the page ranges.
  121. /// </summary>
  122. public void SetPageRanges(CefRange[] ranges)
  123. {
  124. var count = ranges != null ? ranges.Length : 0;
  125. var n_ranges = new cef_range_t[count];
  126. for (var i = 0; i < count; i++)
  127. {
  128. n_ranges[i].from = ranges[i].From;
  129. n_ranges[i].to = ranges[i].To;
  130. }
  131. fixed (cef_range_t* n_ranges_ptr = n_ranges)
  132. {
  133. cef_print_settings_t.set_page_ranges(_self, (UIntPtr)count, n_ranges_ptr);
  134. }
  135. }
  136. /// <summary>
  137. /// Returns the number of page ranges that currently exist.
  138. /// </summary>
  139. public int GetPageRangesCount()
  140. {
  141. return (int)cef_print_settings_t.get_page_ranges_count(_self);
  142. }
  143. /// <summary>
  144. /// Retrieve the page ranges.
  145. /// </summary>
  146. public CefRange[] GetPageRanges()
  147. {
  148. var count = GetPageRangesCount();
  149. if (count == 0) return new CefRange[0];
  150. var n_ranges = new cef_range_t[count];
  151. UIntPtr n_count = (UIntPtr)count;
  152. fixed (cef_range_t* n_ranges_ptr = n_ranges)
  153. {
  154. cef_print_settings_t.get_page_ranges(_self, &n_count, n_ranges_ptr);
  155. }
  156. count = (int)n_count;
  157. if (count == 0) return new CefRange[0];
  158. var ranges = new CefRange[count];
  159. for (var i = 0; i < count; i++)
  160. {
  161. ranges[i].From = n_ranges[i].from;
  162. ranges[i].To = n_ranges[i].to;
  163. }
  164. return ranges;
  165. }
  166. /// <summary>
  167. /// Set whether only the selection will be printed.
  168. /// </summary>
  169. public void SetSelectionOnly(bool selectionOnly)
  170. {
  171. cef_print_settings_t.set_selection_only(_self, selectionOnly ? 1 : 0);
  172. }
  173. /// <summary>
  174. /// Returns true if only the selection will be printed.
  175. /// </summary>
  176. public bool IsSelectionOnly
  177. {
  178. get
  179. {
  180. return cef_print_settings_t.is_selection_only(_self) != 0;
  181. }
  182. }
  183. /// <summary>
  184. /// Set whether pages will be collated.
  185. /// </summary>
  186. public void SetCollate(bool collate)
  187. {
  188. cef_print_settings_t.set_collate(_self, collate ? 1 : 0);
  189. }
  190. /// <summary>
  191. /// Returns true if pages will be collated.
  192. /// </summary>
  193. public bool WillCollate
  194. {
  195. get
  196. {
  197. return cef_print_settings_t.will_collate(_self) != 0;
  198. }
  199. }
  200. /// <summary>
  201. /// Set the color model.
  202. /// </summary>
  203. public void SetColorModel(CefColorModel colorModel)
  204. {
  205. cef_print_settings_t.set_color_model(_self, colorModel);
  206. }
  207. /// <summary>
  208. /// Get the color model.
  209. /// </summary>
  210. public CefColorModel GetColorModel()
  211. {
  212. return cef_print_settings_t.get_color_model(_self);
  213. }
  214. /// <summary>
  215. /// Set the number of copies.
  216. /// </summary>
  217. public void SetCopies(int copies)
  218. {
  219. cef_print_settings_t.set_copies(_self, copies);
  220. }
  221. /// <summary>
  222. /// Get the number of copies.
  223. /// </summary>
  224. public int GetCopies()
  225. {
  226. return cef_print_settings_t.get_copies(_self);
  227. }
  228. /// <summary>
  229. /// Set the duplex mode.
  230. /// </summary>
  231. public void SetDuplexMode(CefDuplexMode mode)
  232. {
  233. cef_print_settings_t.set_duplex_mode(_self, mode);
  234. }
  235. /// <summary>
  236. /// Get the duplex mode.
  237. /// </summary>
  238. public CefDuplexMode GetDuplexMode()
  239. {
  240. return cef_print_settings_t.get_duplex_mode(_self);
  241. }
  242. }
  243. }