CEF4Delphi/source/uCEFContextMenuHandler.pas

275 lines
12 KiB
ObjectPascal
Raw Normal View History

2017-01-27 17:29:37 +01:00
// ************************************************************************
// ***************************** CEF4Delphi *******************************
// ************************************************************************
//
// CEF4Delphi is based on DCEF3 which uses CEF3 to embed a chromium-based
// browser in Delphi applications.
//
// The original license of DCEF3 still applies to CEF4Delphi.
//
// For more information about CEF4Delphi visit :
// https://www.briskbard.com/index.php?lang=en&pageid=cef
//
// Copyright <20> 2019 Salvador Diaz Fau. All rights reserved.
2017-01-27 17:29:37 +01:00
//
// ************************************************************************
// ************ vvvv Original license and comments below vvvv *************
// ************************************************************************
(*
* Delphi Chromium Embedded 3
*
* Usage allowed under the restrictions of the Lesser GNU General Public License
* or alternatively the restrictions of the Mozilla Public License 1.1
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Unit owner : Henri Gourvest <hgourvest@gmail.com>
* Web site : http://www.progdigy.com
* Repository : http://code.google.com/p/delphichromiumembedded/
* Group : http://groups.google.com/group/delphichromiumembedded
*
* Embarcadero Technologies, Inc is not permitted to use or redistribute
* this source code without explicit permission.
*
*)
unit uCEFContextMenuHandler;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-01-27 17:29:37 +01:00
2017-02-05 20:56:46 +01:00
{$I cef.inc}
2017-01-27 17:29:37 +01:00
interface
uses
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
2017-01-27 17:29:37 +01:00
type
TCefContextMenuHandlerOwn = class(TCefBaseRefCountedOwn, ICefContextMenuHandler)
2017-01-27 17:29:37 +01:00
protected
procedure OnBeforeContextMenu(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); virtual;
function RunContextMenu(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel; const callback: ICefRunContextMenuCallback): Boolean; virtual;
function OnContextMenuCommand(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; eventFlags: TCefEventFlags): Boolean; virtual;
procedure OnContextMenuDismissed(const browser: ICefBrowser; const frame: ICefFrame); virtual;
procedure RemoveReferences; virtual;
2017-01-27 17:29:37 +01:00
public
constructor Create; virtual;
end;
TCustomContextMenuHandler = class(TCefContextMenuHandlerOwn)
protected
FEvents : Pointer;
2017-01-27 17:29:37 +01:00
procedure OnBeforeContextMenu(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); override;
function RunContextMenu(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel; const callback: ICefRunContextMenuCallback): Boolean; override;
2017-01-27 17:29:37 +01:00
function OnContextMenuCommand(const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; eventFlags: TCefEventFlags): Boolean; override;
procedure OnContextMenuDismissed(const browser: ICefBrowser; const frame: ICefFrame); override;
procedure RemoveReferences; override;
2017-01-27 17:29:37 +01:00
public
constructor Create(const events: Pointer); reintroduce; virtual;
destructor Destroy; override;
2017-01-27 17:29:37 +01:00
end;
implementation
uses
{$IFDEF DELPHI16_UP}
System.SysUtils,
{$ELSE}
SysUtils,
{$ENDIF}
2017-01-27 17:29:37 +01:00
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFFrame, uCEFContextMenuParams,
uCEFMenuModel, uCEFRunContextMenuCallback;
2018-03-29 20:02:04 +02:00
procedure cef_context_menu_handler_on_before_context_menu(self : PCefContextMenuHandler;
browser : PCefBrowser;
frame : PCefFrame;
params : PCefContextMenuParams;
model : PCefMenuModel); stdcall;
var
TempObject : TObject;
2017-01-27 17:29:37 +01:00
begin
2018-03-29 20:02:04 +02:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefContextMenuHandlerOwn) then
TCefContextMenuHandlerOwn(TempObject).OnBeforeContextMenu(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefContextMenuParamsRef.UnWrap(params),
TCefMenuModelRef.UnWrap(model));
2017-01-27 17:29:37 +01:00
end;
2018-03-29 20:02:04 +02:00
function cef_context_menu_handler_run_context_menu(self : PCefContextMenuHandler;
browser : PCefBrowser;
frame : PCefFrame;
params : PCefContextMenuParams;
model : PCefMenuModel;
callback : PCefRunContextMenuCallback): Integer; stdcall;
var
TempObject : TObject;
2017-01-27 17:29:37 +01:00
begin
2018-03-29 20:02:04 +02:00
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefContextMenuHandlerOwn) then
Result := Ord(TCefContextMenuHandlerOwn(TempObject).RunContextMenu(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefContextMenuParamsRef.UnWrap(params),
TCefMenuModelRef.UnWrap(model),
TCefRunContextMenuCallbackRef.UnWrap(callback)));
2017-01-27 17:29:37 +01:00
end;
2018-03-29 20:02:04 +02:00
function cef_context_menu_handler_on_context_menu_command(self : PCefContextMenuHandler;
browser : PCefBrowser;
frame : PCefFrame;
params : PCefContextMenuParams;
command_id : Integer;
event_flags : TCefEventFlags): Integer; stdcall;
var
TempObject : TObject;
2017-01-27 17:29:37 +01:00
begin
2018-03-29 20:02:04 +02:00
Result := Ord(False);
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefContextMenuHandlerOwn) then
Result := Ord(TCefContextMenuHandlerOwn(TempObject).OnContextMenuCommand(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame),
TCefContextMenuParamsRef.UnWrap(params),
command_id,
event_flags));
2017-01-27 17:29:37 +01:00
end;
2018-03-29 20:02:04 +02:00
procedure cef_context_menu_handler_on_context_menu_dismissed(self : PCefContextMenuHandler;
browser : PCefBrowser;
frame : PCefFrame); stdcall;
var
TempObject : TObject;
2017-01-27 17:29:37 +01:00
begin
2018-03-29 20:02:04 +02:00
TempObject := CefGetObject(self);
if (TempObject <> nil) and (TempObject is TCefContextMenuHandlerOwn) then
TCefContextMenuHandlerOwn(TempObject).OnContextMenuDismissed(TCefBrowserRef.UnWrap(browser),
TCefFrameRef.UnWrap(frame));
2017-01-27 17:29:37 +01:00
end;
constructor TCefContextMenuHandlerOwn.Create;
begin
inherited CreateData(SizeOf(TCefContextMenuHandler));
2017-01-27 17:29:37 +01:00
with PCefContextMenuHandler(FData)^ do
begin
on_before_context_menu := {$IFDEF FPC}@{$ENDIF}cef_context_menu_handler_on_before_context_menu;
run_context_menu := {$IFDEF FPC}@{$ENDIF}cef_context_menu_handler_run_context_menu;
on_context_menu_command := {$IFDEF FPC}@{$ENDIF}cef_context_menu_handler_on_context_menu_command;
on_context_menu_dismissed := {$IFDEF FPC}@{$ENDIF}cef_context_menu_handler_on_context_menu_dismissed;
end;
2017-01-27 17:29:37 +01:00
end;
procedure TCefContextMenuHandlerOwn.OnBeforeContextMenu(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
const model : ICefMenuModel);
2017-01-27 17:29:37 +01:00
begin
//
2017-01-27 17:29:37 +01:00
end;
function TCefContextMenuHandlerOwn.OnContextMenuCommand(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
commandId : Integer;
eventFlags : TCefEventFlags): Boolean;
2017-01-27 17:29:37 +01:00
begin
Result := False;
end;
procedure TCefContextMenuHandlerOwn.OnContextMenuDismissed(const browser : ICefBrowser;
const frame : ICefFrame);
2017-01-27 17:29:37 +01:00
begin
//
2017-01-27 17:29:37 +01:00
end;
function TCefContextMenuHandlerOwn.RunContextMenu(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
const model : ICefMenuModel;
const callback : ICefRunContextMenuCallback): Boolean;
2017-01-27 17:29:37 +01:00
begin
Result := False;
end;
procedure TCefContextMenuHandlerOwn.RemoveReferences;
begin
//
end;
2017-01-27 17:29:37 +01:00
// TCustomContextMenuHandler
constructor TCustomContextMenuHandler.Create(const events: Pointer);
2017-01-27 17:29:37 +01:00
begin
inherited Create;
FEvents := events;
2017-01-27 17:29:37 +01:00
end;
destructor TCustomContextMenuHandler.Destroy;
begin
RemoveReferences;
inherited Destroy;
end;
procedure TCustomContextMenuHandler.RemoveReferences;
begin
FEvents := nil;
end;
procedure TCustomContextMenuHandler.OnBeforeContextMenu(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
const model : ICefMenuModel);
2017-01-27 17:29:37 +01:00
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnBeforeContextMenu(browser, frame, params, model);
2017-01-27 17:29:37 +01:00
end;
function TCustomContextMenuHandler.RunContextMenu(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
const model : ICefMenuModel;
const callback : ICefRunContextMenuCallback): Boolean;
begin
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doRunContextMenu(browser, frame, params, model, callback)
else
Result := inherited RunContextMenu(browser, frame, params, model, callback);
end;
function TCustomContextMenuHandler.OnContextMenuCommand(const browser : ICefBrowser;
const frame : ICefFrame;
const params : ICefContextMenuParams;
commandId : Integer;
eventFlags : TCefEventFlags): Boolean;
2017-01-27 17:29:37 +01:00
begin
if (FEvents <> nil) then
Result := IChromiumEvents(FEvents).doOnContextMenuCommand(browser, frame, params, commandId, eventFlags)
else
Result := inherited OnContextMenuCommand(browser, frame, params, commandId, eventFlags);
2017-01-27 17:29:37 +01:00
end;
procedure TCustomContextMenuHandler.OnContextMenuDismissed(const browser : ICefBrowser;
const frame : ICefFrame);
2017-01-27 17:29:37 +01:00
begin
if (FEvents <> nil) then IChromiumEvents(FEvents).doOnContextMenuDismissed(browser, frame);
2017-01-27 17:29:37 +01:00
end;
end.