Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed various invoke issues in printing code

Signed-off-by: Peter Nelson <peter@peterdn.com>
  • Loading branch information...
commit 482e88f9e7db3d172ffffc0d14047167af8ac130 1 parent 993204a
Peter Nelson peterdn authored
Showing with 39 additions and 24 deletions.
  1. +36 −21 WebKitCore/PrintManager.cs
  2. +3 −3 WebKitCore/WebKitBrowserCore.cs
57 WebKitCore/PrintManager.cs
View
@@ -13,7 +13,8 @@ internal class PrintManager
{
private PrintDocument _document;
private IWebFramePrivate _webFramePrivate;
- private IWebKitBrowser _owner;
+ private IWebKitBrowserHost _owner;
+ private IWebKitBrowser _browser;
private Graphics _printGfx;
private uint _nPages;
private uint _page;
@@ -21,12 +22,13 @@ internal class PrintManager
private bool _preview;
private bool _printing = false;
- public PrintManager(PrintDocument Document, IWebKitBrowser Owner, bool Preview)
+ public PrintManager(PrintDocument Document, IWebKitBrowserHost Owner, IWebKitBrowser Browser, bool Preview)
{
this._document = Document;
this._owner = Owner;
- this._webFramePrivate =
- (IWebFramePrivate)((IWebView)_owner.GetWebView()).mainFrame();
+ this._browser = Browser;
+ this._webFramePrivate =
+ (IWebFramePrivate)((IWebView)_browser.GetWebView()).mainFrame();
this._preview = Preview;
}
@@ -57,28 +59,24 @@ private void _document_PrintPage(object sender, PrintPageEventArgs e)
{
// running on a seperate thread, so we invoke _webFramePrivate
// methods on the owners ui thread
-
+
if (_printGfx == null)
{
// initialise printing
_printGfx = e.Graphics;
_hDC = _printGfx.GetHdc().ToInt32();
- _owner.Host.Invoke(new MethodInvoker(delegate() {
- _webFramePrivate.setInPrintingMode(1, _hDC);
- }));
-
- _nPages = (uint)_owner.Host.Invoke(
- new GetPrintedPageCountDelegate(delegate() {
+ _ownerInvoke(() => _webFramePrivate.setInPrintingMode(1, _hDC));
+
+ _nPages = _ownerInvoke(() =>
+ {
return _webFramePrivate.getPrintedPageCount(_hDC);
- }));
+ });
_page = 1;
- }
-
- _owner.Host.Invoke(new MethodInvoker(delegate() {
- _webFramePrivate.spoolPages(_hDC, _page, _page, IntPtr.Zero);
- }));
+ }
+
+ _ownerInvoke(() => _webFramePrivate.spoolPages(_hDC, _page, _page, IntPtr.Zero));
++_page;
if (_page <= _nPages)
@@ -86,14 +84,31 @@ private void _document_PrintPage(object sender, PrintPageEventArgs e)
e.HasMorePages = true;
}
else
- {
- _owner.Host.Invoke(new MethodInvoker(delegate() {
- _webFramePrivate.setInPrintingMode(0, _hDC);
- }));
+ {
+ _ownerInvoke(() => _webFramePrivate.setInPrintingMode(0, _hDC));
e.HasMorePages = false;
_printGfx = null;
_nPages = 0;
}
}
+
+ private delegate TResult Fn<TResult>();
+ private delegate void Fn();
+
+ private TResult _ownerInvoke<TResult>(Fn<TResult> method)
+ {
+ if (_owner.InvokeRequired)
+ return (TResult)_owner.Invoke(method);
+ else
+ return method();
+ }
+
+ private void _ownerInvoke(Fn method)
+ {
+ if (_owner.InvokeRequired)
+ _owner.Invoke(method);
+ else
+ method();
+ }
}
}
6 WebKitCore/WebKitBrowserCore.cs
View
@@ -928,7 +928,7 @@ public object GetGlobalScriptContext()
public void Print()
{
PrintDocument doc = this.GetCommonPrintDocument();
- PrintManager pm = new PrintManager(doc, this, false);
+ PrintManager pm = new PrintManager(doc, this.host, this, false);
pm.Print();
}
@@ -957,7 +957,7 @@ public void ShowPrintDialog()
if (printDlg.ShowDialog() == DialogResult.OK)
{
- PrintManager pm = new PrintManager(doc, this, false);
+ PrintManager pm = new PrintManager(doc, this.host, this, false);
pm.Print();
}
}
@@ -971,7 +971,7 @@ public void ShowPrintPreviewDialog()
PrintPreviewDialog printDlg = new PrintPreviewDialog();
PrintDocument doc = this.GetCommonPrintDocument();
printDlg.Document = doc;
- PrintManager pm = new PrintManager(doc, this, true);
+ PrintManager pm = new PrintManager(doc, this.host, this, true);
pm.Print();
printDlg.ShowDialog();
}
Please sign in to comment.
Something went wrong with that request. Please try again.