CefPrintHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// Implement this interface to handle printing on Linux. Each browser will have
  10. /// only one print job in progress at a time. The methods of this class will be
  11. /// called on the browser process UI thread.
  12. /// </summary>
  13. public abstract unsafe partial class CefPrintHandler
  14. {
  15. private void on_print_start(cef_print_handler_t* self, cef_browser_t* browser)
  16. {
  17. CheckSelf(self);
  18. var mBrowser = CefBrowser.FromNative(browser);
  19. OnPrintStart(mBrowser);
  20. }
  21. /// <summary>
  22. /// Called when printing has started for the specified |browser|. This method
  23. /// will be called before the other OnPrint*() methods and irrespective of how
  24. /// printing was initiated (e.g. CefBrowserHost::Print(), JavaScript
  25. /// window.print() or PDF extension print button).
  26. /// </summary>
  27. protected virtual void OnPrintStart(CefBrowser browser)
  28. {
  29. }
  30. private void on_print_settings(cef_print_handler_t* self, cef_browser_t* browser, cef_print_settings_t* settings, int get_defaults)
  31. {
  32. CheckSelf(self);
  33. var mBrowser = CefBrowser.FromNative(browser);
  34. using (var m_settings = CefPrintSettings.FromNative(settings))
  35. {
  36. OnPrintSettings(mBrowser, m_settings, get_defaults != 0);
  37. }
  38. }
  39. /// <summary>
  40. /// Synchronize |settings| with client state. If |get_defaults| is true then
  41. /// populate |settings| with the default print settings. Do not keep a
  42. /// reference to |settings| outside of this callback.
  43. /// </summary>
  44. protected abstract void OnPrintSettings(CefBrowser browser, CefPrintSettings settings, bool getDefaults);
  45. private int on_print_dialog(cef_print_handler_t* self, cef_browser_t* browser, int has_selection, cef_print_dialog_callback_t* callback)
  46. {
  47. CheckSelf(self);
  48. var mBrowser = CefBrowser.FromNative(browser);
  49. var m_callback = CefPrintDialogCallback.FromNative(callback);
  50. var m_result = OnPrintDialog(mBrowser, has_selection != 0, m_callback);
  51. return m_result ? 1 : 0;
  52. }
  53. /// <summary>
  54. /// Show the print dialog. Execute |callback| once the dialog is dismissed.
  55. /// Return true if the dialog will be displayed or false to cancel the
  56. /// printing immediately.
  57. /// </summary>
  58. protected abstract bool OnPrintDialog(CefBrowser browser, bool hasSelection, CefPrintDialogCallback callback);
  59. private int on_print_job(cef_print_handler_t* self, cef_browser_t* browser, cef_string_t* document_name, cef_string_t* pdf_file_path, cef_print_job_callback_t* callback)
  60. {
  61. CheckSelf(self);
  62. var mBrowser = CefBrowser.FromNative(browser);
  63. var m_documentName = cef_string_t.ToString(document_name);
  64. var m_pdfFilePath = cef_string_t.ToString(pdf_file_path);
  65. var m_callback = CefPrintJobCallback.FromNative(callback);
  66. var m_result = OnPrintJob(mBrowser, m_documentName, m_pdfFilePath, m_callback);
  67. return m_result ? 1 : 0;
  68. }
  69. /// <summary>
  70. /// Send the print job to the printer. Execute |callback| once the job is
  71. /// completed. Return true if the job will proceed or false to cancel the job
  72. /// immediately.
  73. /// </summary>
  74. protected abstract bool OnPrintJob(CefBrowser browser, string documentName, string pdfFilePath, CefPrintJobCallback callback);
  75. private void on_print_reset(cef_print_handler_t* self, cef_browser_t* browser)
  76. {
  77. CheckSelf(self);
  78. var mBrowser = CefBrowser.FromNative(browser);
  79. OnPrintReset(mBrowser);
  80. }
  81. /// <summary>
  82. /// Reset client state related to printing.
  83. /// </summary>
  84. protected abstract void OnPrintReset(CefBrowser browser);
  85. private cef_size_t get_pdf_paper_size(cef_print_handler_t* self, cef_browser_t* browser, int device_units_per_inch)
  86. {
  87. CheckSelf(self);
  88. var mBrowser = CefBrowser.FromNativeOrNull(browser);
  89. var m_result = GetPdfPaperSize(mBrowser, device_units_per_inch);
  90. var n_result = new cef_size_t
  91. {
  92. width = m_result.Width,
  93. height = m_result.Height,
  94. };
  95. return n_result;
  96. }
  97. /// <summary>
  98. /// Return the PDF paper size in device units. Used in combination with
  99. /// CefBrowserHost::PrintToPDF().
  100. /// </summary>
  101. protected virtual CefSize GetPdfPaperSize(CefBrowser browser, int deviceUnitsPerInch)
  102. => new CefSize();
  103. }
  104. }