2021-01-01 18:51:09 +02:00
|
|
|
unit uCEFPrintHandler;
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 19:56:41 +02:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2021-01-01 18:51:09 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes, uCEFApplicationCore;
|
|
|
|
|
|
|
|
type
|
|
|
|
TCefPrintHandlerOwn = class(TCefBaseRefCountedOwn, ICefPrintHandler)
|
|
|
|
protected
|
|
|
|
procedure OnPrintStart(const browser: ICefBrowser); virtual; abstract;
|
|
|
|
procedure OnPrintSettings(const browser: ICefBrowser; const settings: ICefPrintSettings; getDefaults: boolean); virtual; abstract;
|
|
|
|
procedure OnPrintDialog(const browser: ICefBrowser; hasSelection: boolean; const callback: ICefPrintDialogCallback; var aResult: boolean); virtual;
|
|
|
|
procedure OnPrintJob(const browser: ICefBrowser; const documentName, PDFFilePath: ustring; const callback: ICefPrintJobCallback; var aResult: boolean); virtual;
|
|
|
|
procedure OnPrintReset(const browser: ICefBrowser); virtual; abstract;
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure GetPDFPaperSize(const browser: ICefBrowser; deviceUnitsPerInch: Integer; var aResult: TCefSize); virtual;
|
2021-01-01 18:51:09 +02:00
|
|
|
|
|
|
|
procedure RemoveReferences; virtual; abstract;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCustomPrintHandler = class(TCefPrintHandlerOwn)
|
|
|
|
protected
|
2021-04-18 19:36:20 +02:00
|
|
|
FEvents : Pointer;
|
2021-01-01 18:51:09 +02:00
|
|
|
|
|
|
|
procedure OnPrintStart(const browser: ICefBrowser); override;
|
|
|
|
procedure OnPrintSettings(const browser: ICefBrowser; const settings: ICefPrintSettings; getDefaults: boolean); override;
|
|
|
|
procedure OnPrintDialog(const browser: ICefBrowser; hasSelection: boolean; const callback: ICefPrintDialogCallback; var aResult: boolean); override;
|
|
|
|
procedure OnPrintJob(const browser: ICefBrowser; const documentName, PDFFilePath: ustring; const callback: ICefPrintJobCallback; var aResult: boolean); override;
|
|
|
|
procedure OnPrintReset(const browser: ICefBrowser); override;
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure GetPDFPaperSize(const browser: ICefBrowser; deviceUnitsPerInch: Integer; var aResult: TCefSize); override;
|
2021-01-01 18:51:09 +02:00
|
|
|
|
|
|
|
procedure RemoveReferences; override;
|
|
|
|
|
|
|
|
public
|
2021-04-18 19:36:20 +02:00
|
|
|
constructor Create(const events : IChromiumEvents); reintroduce; virtual;
|
2021-01-01 18:51:09 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.SysUtils,
|
|
|
|
{$ELSE}
|
|
|
|
SysUtils,
|
|
|
|
{$ENDIF}
|
|
|
|
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFPrintSettings,
|
|
|
|
uCEFPrintDialogCallback, uCEFPrintJobCallback;
|
|
|
|
|
|
|
|
|
|
|
|
// TCefPrintHandlerOwn
|
|
|
|
|
|
|
|
procedure cef_print_handler_on_print_start(self: PCefPrintHandler; browser: PCefBrowser); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
|
|
|
TCefPrintHandlerOwn(TempObject).OnPrintStart(TCefBrowserRef.UnWrap(browser));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure cef_print_handler_on_print_settings(self : PCefPrintHandler;
|
|
|
|
browser : PCefBrowser;
|
|
|
|
settings : PCefPrintSettings;
|
|
|
|
get_defaults : Integer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
|
|
|
TCefPrintHandlerOwn(TempObject).OnPrintSettings(TCefBrowserRef.UnWrap(browser),
|
|
|
|
TCefPrintSettingsRef.UnWrap(settings),
|
|
|
|
get_defaults <> 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_print_handler_on_print_dialog(self : PCefPrintHandler;
|
|
|
|
browser : PCefBrowser;
|
|
|
|
has_selection : Integer;
|
|
|
|
callback : PCefPrintDialogCallback): Integer; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
TempResult : boolean;
|
|
|
|
begin
|
|
|
|
TempResult := False;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
|
|
|
TCefPrintHandlerOwn(TempObject).OnPrintDialog(TCefBrowserRef.UnWrap(browser),
|
|
|
|
has_selection <> 0,
|
|
|
|
TCefPrintDialogCallbackRef.UnWrap(callback),
|
|
|
|
TempResult);
|
|
|
|
|
|
|
|
Result := ord(TempResult);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_print_handler_on_print_job( self : PCefPrintHandler;
|
|
|
|
browser : PCefBrowser;
|
|
|
|
const document_name : PCefString;
|
|
|
|
const pdf_file_path : PCefString;
|
|
|
|
callback : PCefPrintJobCallback): Integer; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
TempResult : boolean;
|
|
|
|
begin
|
|
|
|
TempResult := False;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
|
|
|
TCefPrintHandlerOwn(TempObject).OnPrintJob(TCefBrowserRef.UnWrap(browser),
|
|
|
|
CefString(document_name),
|
|
|
|
CefString(pdf_file_path),
|
|
|
|
TCefPrintJobCallbackRef.UnWrap(callback),
|
|
|
|
TempResult);
|
|
|
|
|
|
|
|
Result := ord(TempResult);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure cef_print_handler_on_print_reset(self : PCefPrintHandler;
|
|
|
|
browser : PCefBrowser); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
|
|
|
TCefPrintHandlerOwn(TempObject).OnPrintReset(TCefBrowserRef.UnWrap(browser));
|
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_print_handler_get_pdf_paper_size(self : PCefPrintHandler;
|
2021-04-18 19:36:20 +02:00
|
|
|
browser : PCefBrowser;
|
2021-01-01 18:51:09 +02:00
|
|
|
device_units_per_inch : Integer): TCefSize; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
|
|
|
TempSize : TCefSize;
|
|
|
|
begin
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
TempSize.Width := 0;
|
|
|
|
TempSize.Height := 0;
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefPrintHandlerOwn) then
|
2021-04-18 19:36:20 +02:00
|
|
|
TCefPrintHandlerOwn(TempObject).GetPDFPaperSize(TCefBrowserRef.UnWrap(browser),
|
|
|
|
device_units_per_inch,
|
|
|
|
TempSize);
|
2021-01-01 18:51:09 +02:00
|
|
|
|
|
|
|
Result := TempSize;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TCefPrintHandlerOwn.Create;
|
|
|
|
begin
|
|
|
|
inherited CreateData(SizeOf(TCefPrintHandler));
|
|
|
|
|
|
|
|
with PCefPrintHandler(FData)^ do
|
|
|
|
begin
|
|
|
|
on_print_start := {$IFDEF FPC}@{$ENDIF}cef_print_handler_on_print_start;
|
|
|
|
on_print_settings := {$IFDEF FPC}@{$ENDIF}cef_print_handler_on_print_settings;
|
|
|
|
on_print_dialog := {$IFDEF FPC}@{$ENDIF}cef_print_handler_on_print_dialog;
|
|
|
|
on_print_job := {$IFDEF FPC}@{$ENDIF}cef_print_handler_on_print_job;
|
|
|
|
on_print_reset := {$IFDEF FPC}@{$ENDIF}cef_print_handler_on_print_reset;
|
|
|
|
get_pdf_paper_size := {$IFDEF FPC}@{$ENDIF}cef_print_handler_get_pdf_paper_size;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCefPrintHandlerOwn.OnPrintDialog(const browser: ICefBrowser; hasSelection: boolean; const callback: ICefPrintDialogCallback; var aResult: boolean);
|
|
|
|
begin
|
|
|
|
aResult := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCefPrintHandlerOwn.OnPrintJob(const browser: ICefBrowser; const documentName, PDFFilePath: ustring; const callback: ICefPrintJobCallback; var aResult: boolean);
|
|
|
|
begin
|
|
|
|
aResult := False;
|
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TCefPrintHandlerOwn.GetPDFPaperSize(const browser: ICefBrowser; deviceUnitsPerInch: Integer; var aResult: TCefSize);
|
2021-01-01 18:51:09 +02:00
|
|
|
begin
|
|
|
|
aResult.Width := 0;
|
|
|
|
aResult.Height := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
// TCustomPrintHandler
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
constructor TCustomPrintHandler.Create(const events : IChromiumEvents);
|
2021-01-01 18:51:09 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
FEvents := Pointer(events);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCustomPrintHandler.Destroy;
|
|
|
|
begin
|
|
|
|
RemoveReferences;
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.RemoveReferences;
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
FEvents := nil;
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.OnPrintStart(const browser : ICefBrowser);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnPrintStart(browser);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.OnPrintSettings(const browser : ICefBrowser;
|
|
|
|
const settings : ICefPrintSettings;
|
|
|
|
getDefaults : boolean);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnPrintSettings(browser, settings, getDefaults);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.OnPrintDialog(const browser : ICefBrowser;
|
|
|
|
hasSelection : boolean;
|
|
|
|
const callback : ICefPrintDialogCallback;
|
|
|
|
var aResult : boolean);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnPrintDialog(browser, hasSelection, callback, aResult);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.OnPrintJob(const browser : ICefBrowser;
|
|
|
|
const documentName : ustring;
|
|
|
|
const PDFFilePath : ustring;
|
|
|
|
const callback : ICefPrintJobCallback;
|
|
|
|
var aResult : boolean);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnPrintJob(browser, documentName, PDFFilePath, callback, aResult);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomPrintHandler.OnPrintReset(const browser : ICefBrowser);
|
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnPrintReset(browser);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
2021-04-18 19:36:20 +02:00
|
|
|
procedure TCustomPrintHandler.GetPDFPaperSize(const browser : ICefBrowser;
|
|
|
|
deviceUnitsPerInch : Integer;
|
|
|
|
var aResult : TCefSize);
|
2021-01-01 18:51:09 +02:00
|
|
|
begin
|
2021-04-18 19:36:20 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IChromiumEvents(FEvents).doOnGetPDFPaperSize(browser, deviceUnitsPerInch, aResult);
|
2021-01-01 18:51:09 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|