2017-01-27 18:29:37 +02:00
|
|
|
// ************************************************************************
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
// ************************************************************************
|
|
|
|
//
|
2019-10-19 10:58:34 +02:00
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based
|
2017-01-27 18:29:37 +02:00
|
|
|
// 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
|
|
|
|
//
|
2021-01-01 12:11:30 +02:00
|
|
|
// Copyright � 2021 Salvador Diaz Fau. All rights reserved.
|
2017-01-27 18:29:37 +02: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 uCEFChromiumWindow;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2018-11-22 16:02:21 +02:00
|
|
|
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
2017-01-27 18:29:37 +02:00
|
|
|
|
2017-02-05 21:56:46 +02:00
|
|
|
{$I cef.inc}
|
|
|
|
|
2017-01-27 18:29:37 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-02-05 21:56:46 +02:00
|
|
|
{$IFDEF DELPHI16_UP}
|
2018-09-16 10:39:41 +02:00
|
|
|
{$IFDEF MSWINDOWS}WinApi.Windows, WinApi.Messages,{$ENDIF} System.Classes, Vcl.Controls,
|
2017-02-05 21:56:46 +02:00
|
|
|
{$ELSE}
|
2018-06-03 17:18:54 +02:00
|
|
|
{$IFDEF MSWINDOWS}Windows,{$ENDIF} Classes, Forms, Controls, Graphics,
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
LCLProc, LCLType, LCLIntf, LResources, LMessages, InterfaceBase,
|
|
|
|
{$ELSE}
|
|
|
|
Messages,
|
|
|
|
{$ENDIF}
|
2017-02-05 21:56:46 +02:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
uCEFWindowParent, uCEFChromium, uCEFInterfaces, uCEFConstants, uCEFTypes, uCEFWinControl;
|
2017-01-27 18:29:37 +02:00
|
|
|
|
|
|
|
type
|
2018-12-09 12:47:32 +02:00
|
|
|
{$IFNDEF FPC}{$IFDEF DELPHI16_UP}[ComponentPlatformsAttribute(pidWin32 or pidWin64)]{$ENDIF}{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
TChromiumWindow = class(TCEFWinControl)
|
2017-01-27 18:29:37 +02:00
|
|
|
protected
|
2017-09-07 10:58:09 +02:00
|
|
|
FChromium : TChromium;
|
|
|
|
FOnClose : TNotifyEvent;
|
2017-12-07 11:49:51 +02:00
|
|
|
FOnBeforeClose : TNotifyEvent;
|
2017-09-07 10:58:09 +02:00
|
|
|
FOnAfterCreated : TNotifyEvent;
|
2018-12-18 18:07:50 +02:00
|
|
|
FUseSetFocus : boolean;
|
2017-01-27 18:29:37 +02:00
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure SetVisible(Value: Boolean); override;
|
|
|
|
{$ENDIF}
|
2017-10-28 18:48:53 +02:00
|
|
|
function GetBrowserInitialized : boolean;
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2018-09-16 10:39:41 +02:00
|
|
|
function GetChildWindowHandle : THandle; override;
|
|
|
|
|
|
|
|
procedure WndProc(var aMessage: TMessage); override;
|
2017-01-27 18:29:37 +02:00
|
|
|
|
|
|
|
procedure OnCloseMsg(var aMessage : TMessage); message CEF_DOONCLOSE;
|
2017-09-07 10:58:09 +02:00
|
|
|
procedure OnAfterCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
|
2019-01-08 20:15:25 +02:00
|
|
|
{$ENDIF}
|
2019-03-28 11:40:36 +02:00
|
|
|
procedure WebBrowser_OnClose(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
|
2017-12-07 11:49:51 +02:00
|
|
|
procedure WebBrowser_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
2017-09-07 10:58:09 +02:00
|
|
|
procedure WebBrowser_OnAfterCreated(Sender: TObject; const browser: ICefBrowser);
|
2017-01-27 18:29:37 +02:00
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure BrowserAfterCreated(Data: PtrInt);
|
|
|
|
procedure BrowserOnCLose(Data: PtrInt);
|
|
|
|
{$ENDIF}
|
|
|
|
|
2017-01-27 18:29:37 +02:00
|
|
|
public
|
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
procedure AfterConstruction; override;
|
2017-10-28 18:48:53 +02:00
|
|
|
function CreateBrowser : boolean;
|
2017-01-27 18:29:37 +02:00
|
|
|
procedure CloseBrowser(aForceClose : boolean);
|
2018-05-12 14:50:54 +02:00
|
|
|
procedure LoadURL(const aURL : ustring);
|
2017-12-07 11:49:51 +02:00
|
|
|
procedure NotifyMoveOrResizeStarted;
|
2021-02-20 18:14:15 +02:00
|
|
|
procedure UpdateSize; override;
|
2017-01-27 18:29:37 +02:00
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
property ChromiumBrowser : TChromium read FChromium;
|
|
|
|
property Initialized : boolean read GetBrowserInitialized;
|
2017-01-27 18:29:37 +02:00
|
|
|
|
|
|
|
published
|
2018-12-18 18:07:50 +02:00
|
|
|
property UseSetFocus : boolean read FUseSetFocus write FUseSetFocus default True;
|
2017-09-07 10:58:09 +02:00
|
|
|
property OnClose : TNotifyEvent read FOnClose write FOnClose;
|
2017-12-07 11:49:51 +02:00
|
|
|
property OnBeforeClose : TNotifyEvent read FOnBeforeClose write FOnBeforeClose;
|
2017-09-07 10:58:09 +02:00
|
|
|
property OnAfterCreated : TNotifyEvent read FOnAfterCreated write FOnAfterCreated;
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure Register;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2019-10-21 11:17:47 +02:00
|
|
|
// *********************************************************
|
|
|
|
// ********************** ATTENTION ! **********************
|
|
|
|
// *********************************************************
|
|
|
|
// ** **
|
|
|
|
// ** MANY OF THE EVENTS IN CEF4DELPHI COMPONENTS LIKE **
|
|
|
|
// ** TCHROMIUM, TFMXCHROMIUM OR TCEFAPPLICATION ARE **
|
|
|
|
// ** EXECUTED IN A CEF THREAD BY DEFAULT. **
|
|
|
|
// ** **
|
|
|
|
// ** WINDOWS CONTROLS MUST BE CREATED AND DESTROYED IN **
|
|
|
|
// ** THE SAME THREAD TO AVOID ERRORS. **
|
|
|
|
// ** SOME OF THEM RECREATE THE HANDLERS IF THEY ARE **
|
|
|
|
// ** MODIFIED AND CAN CAUSE THE SAME ERRORS. **
|
|
|
|
// ** **
|
|
|
|
// ** DON'T CREATE, MODIFY OR DESTROY WINDOWS CONTROLS **
|
|
|
|
// ** INSIDE THE CEF4DELPHI EVENTS AND USE **
|
|
|
|
// ** SYNCHRONIZATION OBJECTS TO PROTECT VARIABLES AND **
|
|
|
|
// ** FIELDS IF THEY ARE ALSO USED IN THE MAIN THREAD. **
|
|
|
|
// ** **
|
|
|
|
// ** READ THIS FOR MORE INFORMATION : **
|
|
|
|
// ** https://www.briskbard.com/index.php?pageid=cef **
|
|
|
|
// ** **
|
|
|
|
// ** USE OUR FORUMS FOR MORE QUESTIONS : **
|
|
|
|
// ** https://www.briskbard.com/forum/ **
|
|
|
|
// ** **
|
|
|
|
// *********************************************************
|
|
|
|
// *********************************************************
|
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
// This component should *ONLY* be used in emtremely simple applications with simple browsers.
|
|
|
|
// In other cases it's recomended using a TChromium with a TCEFWindowParent as shown in the
|
|
|
|
// SimpleBrowser2 demo.
|
|
|
|
|
2017-01-27 18:29:37 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-02-05 21:56:46 +02:00
|
|
|
{$IFDEF DELPHI16_UP}
|
2017-01-27 18:29:37 +02:00
|
|
|
System.SysUtils;
|
2017-02-05 21:56:46 +02:00
|
|
|
{$ELSE}
|
|
|
|
SysUtils;
|
|
|
|
{$ENDIF}
|
2017-01-27 18:29:37 +02:00
|
|
|
|
|
|
|
constructor TChromiumWindow.Create(AOwner: TComponent);
|
|
|
|
begin
|
|
|
|
inherited Create(AOwner);
|
|
|
|
|
2017-09-07 10:58:09 +02:00
|
|
|
FChromium := nil;
|
|
|
|
FOnClose := nil;
|
2017-12-07 11:49:51 +02:00
|
|
|
FOnBeforeClose := nil;
|
2017-09-07 10:58:09 +02:00
|
|
|
FOnAfterCreated := nil;
|
2018-12-18 18:07:50 +02:00
|
|
|
FUseSetFocus := True;
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TChromiumWindow.AfterConstruction;
|
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
if not(csDesigning in ComponentState) then
|
|
|
|
begin
|
2017-09-07 10:58:09 +02:00
|
|
|
FChromium := TChromium.Create(self);
|
2018-05-12 14:50:54 +02:00
|
|
|
FChromium.OnClose := {$IFDEF FPC}@{$ENDIF}WebBrowser_OnClose;
|
|
|
|
FChromium.OnBeforeClose := {$IFDEF FPC}@{$ENDIF}WebBrowser_OnBeforeClose;
|
|
|
|
FChromium.OnAfterCreated := {$IFDEF FPC}@{$ENDIF}WebBrowser_OnAfterCreated;
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2017-01-27 18:29:37 +02:00
|
|
|
function TChromiumWindow.GetChildWindowHandle : THandle;
|
|
|
|
begin
|
2018-06-27 12:02:29 +02:00
|
|
|
Result := 0;
|
|
|
|
|
|
|
|
if (FChromium <> nil) then Result := FChromium.WindowHandle;
|
|
|
|
|
|
|
|
if (Result = 0) then Result := inherited GetChildWindowHandle;
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-16 10:39:41 +02:00
|
|
|
procedure TChromiumWindow.WndProc(var aMessage: TMessage);
|
|
|
|
var
|
|
|
|
TempHandle : THandle;
|
|
|
|
begin
|
|
|
|
case aMessage.Msg of
|
|
|
|
WM_SETFOCUS:
|
|
|
|
begin
|
2018-12-18 18:07:50 +02:00
|
|
|
if FUseSetFocus and (FChromium <> nil) then
|
2018-09-16 10:39:41 +02:00
|
|
|
FChromium.SetFocus(True)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
TempHandle := ChildWindowHandle;
|
|
|
|
if (TempHandle <> 0) then PostMessage(TempHandle, WM_SETFOCUS, aMessage.WParam, 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
inherited WndProc(aMessage);
|
|
|
|
end;
|
|
|
|
|
|
|
|
WM_ERASEBKGND:
|
|
|
|
if (ChildWindowHandle = 0) then inherited WndProc(aMessage);
|
|
|
|
|
|
|
|
CM_WANTSPECIALKEY:
|
|
|
|
if not(TWMKey(aMessage).CharCode in [VK_LEFT .. VK_DOWN, VK_RETURN, VK_ESCAPE]) then
|
|
|
|
aMessage.Result := 1
|
|
|
|
else
|
|
|
|
inherited WndProc(aMessage);
|
|
|
|
|
|
|
|
WM_GETDLGCODE : aMessage.Result := DLGC_WANTARROWS or DLGC_WANTCHARS;
|
|
|
|
|
|
|
|
else inherited WndProc(aMessage);
|
|
|
|
end;
|
|
|
|
end;
|
2019-01-08 20:15:25 +02:00
|
|
|
{$ENDIF}
|
2018-09-16 10:39:41 +02:00
|
|
|
|
2017-10-28 18:48:53 +02:00
|
|
|
function TChromiumWindow.GetBrowserInitialized : boolean;
|
|
|
|
begin
|
|
|
|
Result := (FChromium <> nil) and FChromium.Initialized;
|
|
|
|
end;
|
|
|
|
|
2019-03-28 11:40:36 +02:00
|
|
|
procedure TChromiumWindow.WebBrowser_OnClose(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
|
2017-01-27 18:29:37 +02:00
|
|
|
begin
|
2019-03-28 11:40:36 +02:00
|
|
|
aAction := cbaClose;
|
2017-03-15 15:53:45 +02:00
|
|
|
if assigned(FOnClose) then
|
|
|
|
begin
|
2021-02-20 18:14:15 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
PostMessage(Handle, CEF_DOONCLOSE, 0, 0);
|
|
|
|
aAction := cbaDelay;
|
|
|
|
{$ELSE}
|
|
|
|
{$IFDEF FPC}
|
|
|
|
Application.QueueAsyncCall(@BrowserOnClose, 0);
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
2019-01-08 20:15:25 +02:00
|
|
|
end;
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
|
2017-12-07 11:49:51 +02:00
|
|
|
procedure TChromiumWindow.WebBrowser_OnBeforeClose(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
begin
|
2019-07-10 14:31:45 +02:00
|
|
|
if assigned(FOnBeforeClose) then FOnBeforeClose(self);
|
2017-12-07 11:49:51 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-07 10:58:09 +02:00
|
|
|
procedure TChromiumWindow.WebBrowser_OnAfterCreated(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
begin
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2021-02-20 18:14:15 +02:00
|
|
|
PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
|
|
|
|
{$ELSE}
|
|
|
|
{$IFDEF FPC}
|
|
|
|
Application.QueueAsyncCall(@BrowserAfterCreated, 0);
|
|
|
|
{$ENDIF}
|
2019-01-08 20:15:25 +02:00
|
|
|
{$ENDIF}
|
2017-09-07 10:58:09 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2017-01-27 18:29:37 +02:00
|
|
|
procedure TChromiumWindow.OnCloseMsg(var aMessage : TMessage);
|
|
|
|
begin
|
|
|
|
if assigned(FOnClose) then FOnClose(self);
|
|
|
|
end;
|
|
|
|
|
2017-09-07 10:58:09 +02:00
|
|
|
procedure TChromiumWindow.OnAfterCreatedMsg(var aMessage : TMessage);
|
|
|
|
begin
|
2017-09-08 17:44:32 +02:00
|
|
|
UpdateSize;
|
2017-09-07 10:58:09 +02:00
|
|
|
if assigned(FOnAfterCreated) then FOnAfterCreated(self);
|
|
|
|
end;
|
2019-01-08 20:15:25 +02:00
|
|
|
{$ENDIF}
|
2017-09-07 10:58:09 +02:00
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure TChromiumWindow.BrowserAfterCreated(Data: PtrInt);
|
|
|
|
begin
|
|
|
|
UpdateSize;
|
|
|
|
if assigned(FOnAfterCreated) then FOnAfterCreated(self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TChromiumWindow.BrowserOnCLose(Data: PtrInt);
|
|
|
|
begin
|
|
|
|
if assigned(FOnClose) then FOnClose(self);
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2017-10-28 18:48:53 +02:00
|
|
|
function TChromiumWindow.CreateBrowser : boolean;
|
2017-01-27 18:29:37 +02:00
|
|
|
begin
|
2017-10-28 18:48:53 +02:00
|
|
|
Result := not(csDesigning in ComponentState) and
|
|
|
|
(FChromium <> nil) and
|
|
|
|
FChromium.CreateBrowser(self, '');
|
2017-01-27 18:29:37 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TChromiumWindow.CloseBrowser(aForceClose : boolean);
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then FChromium.CloseBrowser(aForceClose);
|
|
|
|
end;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
procedure TChromiumWindow.LoadURL(const aURL : ustring);
|
2017-01-27 18:29:37 +02:00
|
|
|
begin
|
|
|
|
if not(csDesigning in ComponentState) and (FChromium <> nil) then
|
|
|
|
FChromium.LoadURL(aURL);
|
|
|
|
end;
|
|
|
|
|
2017-12-07 11:49:51 +02:00
|
|
|
procedure TChromiumWindow.NotifyMoveOrResizeStarted;
|
|
|
|
begin
|
|
|
|
if (FChromium <> nil) then FChromium.NotifyMoveOrResizeStarted;
|
|
|
|
end;
|
|
|
|
|
2021-02-20 18:14:15 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure TChromiumWindow.SetVisible(Value: Boolean);
|
|
|
|
{$IFDEF LINUX}
|
|
|
|
var
|
|
|
|
TempChanged : boolean;
|
|
|
|
{$ENDIF}
|
|
|
|
begin
|
|
|
|
{$IFDEF LINUX}
|
|
|
|
TempChanged := (Visible <> Value);
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
inherited SetVisible(Value);
|
|
|
|
|
|
|
|
{$IFDEF LINUX}
|
|
|
|
if not(csDesigning in ComponentState) and
|
|
|
|
TempChanged and
|
|
|
|
(FChromium <> nil) and
|
|
|
|
FChromium.Initialized then
|
|
|
|
FChromium.UpdateXWindowVisibility(Visible);
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
procedure TChromiumWindow.UpdateSize;
|
|
|
|
begin
|
|
|
|
{$IFDEF LINUX}
|
|
|
|
if not(csDesigning in ComponentState) and
|
|
|
|
(FChromium <> nil) and
|
|
|
|
FChromium.Initialized then
|
|
|
|
FChromium.UpdateBrowserSize(Left, Top, Width, Height);
|
|
|
|
{$ELSE}
|
|
|
|
inherited UpdateSize;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
procedure Register;
|
|
|
|
begin
|
2018-06-03 17:18:54 +02:00
|
|
|
{$I res/tchromiumwindow.lrs}
|
2018-05-12 14:50:54 +02:00
|
|
|
RegisterComponents('Chromium', [TChromiumWindow]);
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2017-01-27 18:29:37 +02:00
|
|
|
end.
|