mirror of
https://github.com/salvadordf/CEF4Delphi.git
synced 2025-06-02 21:57:37 +02:00
Added custom popup windows to the TabbedBrowser2 demo
This commit is contained in:
parent
8f88a31440
commit
28958bf7b6
@ -41,16 +41,17 @@ program TabbedBrowser2;
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
{$IFDEF DELPHI16_UP}
|
{$IFDEF DELPHI16_UP}
|
||||||
WinApi.Windows,
|
|
||||||
Vcl.Forms,
|
Vcl.Forms,
|
||||||
|
WinApi.Windows,
|
||||||
{$ELSE}
|
{$ELSE}
|
||||||
Forms,
|
Forms,
|
||||||
Windows,
|
Windows,
|
||||||
{$ENDIF}
|
{$ENDIF }
|
||||||
uCEFApplication,
|
uCEFApplication,
|
||||||
uMainForm in 'uMainForm.pas' {MainForm},
|
uMainForm in 'uMainForm.pas' {MainForm},
|
||||||
uBrowserFrame in 'uBrowserFrame.pas' {BrowserFrame: TFrame},
|
uBrowserFrame in 'uBrowserFrame.pas' {BrowserFrame: TFrame},
|
||||||
uBrowserTab in 'uBrowserTab.pas';
|
uBrowserTab in 'uBrowserTab.pas',
|
||||||
|
uChildForm in 'uChildForm.pas' {ChildForm};
|
||||||
|
|
||||||
{$R *.res}
|
{$R *.res}
|
||||||
|
|
||||||
|
@ -135,6 +135,10 @@
|
|||||||
<DesignClass>TFrame</DesignClass>
|
<DesignClass>TFrame</DesignClass>
|
||||||
</DCCReference>
|
</DCCReference>
|
||||||
<DCCReference Include="uBrowserTab.pas"/>
|
<DCCReference Include="uBrowserTab.pas"/>
|
||||||
|
<DCCReference Include="uChildForm.pas">
|
||||||
|
<Form>ChildForm</Form>
|
||||||
|
<FormType>dfm</FormType>
|
||||||
|
</DCCReference>
|
||||||
<BuildConfiguration Include="Release">
|
<BuildConfiguration Include="Release">
|
||||||
<Key>Cfg_2</Key>
|
<Key>Cfg_2</Key>
|
||||||
<CfgParent>Base</CfgParent>
|
<CfgParent>Base</CfgParent>
|
||||||
|
@ -95,6 +95,8 @@ type
|
|||||||
FOnBrowserDestroyed : TNotifyEvent;
|
FOnBrowserDestroyed : TNotifyEvent;
|
||||||
FOnBrowserTitleChange : TBrowserTitleEvent;
|
FOnBrowserTitleChange : TBrowserTitleEvent;
|
||||||
|
|
||||||
|
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
|
||||||
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
|
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
|
||||||
procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY;
|
procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY;
|
||||||
|
|
||||||
@ -116,6 +118,9 @@ implementation
|
|||||||
|
|
||||||
{$R *.dfm}
|
{$R *.dfm}
|
||||||
|
|
||||||
|
uses
|
||||||
|
uBrowserTab;
|
||||||
|
|
||||||
constructor TBrowserFrame.Create(AOwner : TComponent);
|
constructor TBrowserFrame.Create(AOwner : TComponent);
|
||||||
begin
|
begin
|
||||||
inherited Create(AOwner);
|
inherited Create(AOwner);
|
||||||
@ -181,8 +186,7 @@ begin
|
|||||||
Chromium1.LoadURL(URLCbx.Text);
|
Chromium1.LoadURL(URLCbx.Text);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1AfterCreated(Sender: TObject;
|
procedure TBrowserFrame.Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
|
||||||
const browser: ICefBrowser);
|
|
||||||
begin
|
begin
|
||||||
PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
|
PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
|
||||||
end;
|
end;
|
||||||
@ -192,42 +196,72 @@ begin
|
|||||||
Chromium1.GoBack;
|
Chromium1.GoBack;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1AddressChange(Sender: TObject;
|
procedure TBrowserFrame.Chromium1AddressChange( Sender : TObject;
|
||||||
const browser: ICefBrowser; const frame: ICefFrame; const url: ustring);
|
const browser : ICefBrowser;
|
||||||
|
const frame : ICefFrame;
|
||||||
|
const url : ustring);
|
||||||
begin
|
begin
|
||||||
if (URLCbx.Items.IndexOf(url) < 0) then URLCbx.Items.Add(url);
|
if (URLCbx.Items.IndexOf(url) < 0) then URLCbx.Items.Add(url);
|
||||||
|
|
||||||
URLCbx.Text := url;
|
URLCbx.Text := url;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1BeforeClose(Sender: TObject;
|
procedure TBrowserFrame.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
|
||||||
const browser: ICefBrowser);
|
|
||||||
begin
|
begin
|
||||||
if assigned(FOnBrowserDestroyed) then FOnBrowserDestroyed(self);
|
if assigned(FOnBrowserDestroyed) then FOnBrowserDestroyed(self);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1BeforePopup(Sender: TObject;
|
procedure TBrowserFrame.Chromium1BeforePopup( Sender : TObject;
|
||||||
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
|
const browser : ICefBrowser;
|
||||||
targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
|
const frame : ICefFrame;
|
||||||
userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
|
const targetUrl : ustring;
|
||||||
var windowInfo: TCefWindowInfo; var client: ICefClient;
|
const targetFrameName : ustring;
|
||||||
var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
|
targetDisposition : TCefWindowOpenDisposition;
|
||||||
var noJavascriptAccess, Result: Boolean);
|
userGesture : Boolean;
|
||||||
|
const popupFeatures : TCefPopupFeatures;
|
||||||
|
var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
var settings : TCefBrowserSettings;
|
||||||
|
var extra_info : ICefDictionaryValue;
|
||||||
|
var noJavascriptAccess : Boolean;
|
||||||
|
var Result : Boolean);
|
||||||
|
begin
|
||||||
|
case targetDisposition of
|
||||||
|
WOD_NEW_FOREGROUND_TAB,
|
||||||
|
WOD_NEW_BACKGROUND_TAB,
|
||||||
|
WOD_NEW_WINDOW : Result := True; // For simplicity, this demo blocks new tabs and new windows.
|
||||||
|
|
||||||
|
WOD_NEW_POPUP : Result := not(CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures));
|
||||||
|
|
||||||
|
else Result := False;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TBrowserFrame.Chromium1OpenUrlFromTab( Sender : TObject;
|
||||||
|
const browser : ICefBrowser;
|
||||||
|
const frame : ICefFrame;
|
||||||
|
const targetUrl : ustring;
|
||||||
|
targetDisposition : TCefWindowOpenDisposition;
|
||||||
|
userGesture : Boolean;
|
||||||
|
out Result : Boolean);
|
||||||
begin
|
begin
|
||||||
// For simplicity, this demo blocks all popup windows and new tabs
|
|
||||||
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1Close(Sender: TObject;
|
procedure TBrowserFrame.Chromium1Close( Sender : TObject;
|
||||||
const browser: ICefBrowser; var aAction: TCefCloseBrowserAction);
|
const browser : ICefBrowser;
|
||||||
|
var aAction : TCefCloseBrowserAction);
|
||||||
begin
|
begin
|
||||||
PostMessage(Handle, CEF_DESTROY, 0, 0);
|
PostMessage(Handle, CEF_DESTROY, 0, 0);
|
||||||
aAction := cbaDelay;
|
aAction := cbaDelay;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1LoadError(Sender: TObject;
|
procedure TBrowserFrame.Chromium1LoadError( Sender : TObject;
|
||||||
const browser: ICefBrowser; const frame: ICefFrame; errorCode: Integer;
|
const browser : ICefBrowser;
|
||||||
const errorText, failedUrl: ustring);
|
const frame : ICefFrame;
|
||||||
|
errorCode : Integer;
|
||||||
|
const errorText : ustring;
|
||||||
|
const failedUrl : ustring);
|
||||||
var
|
var
|
||||||
TempString : string;
|
TempString : string;
|
||||||
begin
|
begin
|
||||||
@ -241,8 +275,11 @@ begin
|
|||||||
Chromium1.LoadString(TempString, frame);
|
Chromium1.LoadString(TempString, frame);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1LoadingStateChange(Sender: TObject;
|
procedure TBrowserFrame.Chromium1LoadingStateChange( Sender : TObject;
|
||||||
const browser: ICefBrowser; isLoading, canGoBack, canGoForward: Boolean);
|
const browser : ICefBrowser;
|
||||||
|
isLoading : Boolean;
|
||||||
|
canGoBack : Boolean;
|
||||||
|
canGoForward : Boolean);
|
||||||
begin
|
begin
|
||||||
BackBtn.Enabled := canGoBack;
|
BackBtn.Enabled := canGoBack;
|
||||||
ForwardBtn.Enabled := canGoForward;
|
ForwardBtn.Enabled := canGoForward;
|
||||||
@ -259,23 +296,16 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1OpenUrlFromTab(Sender: TObject;
|
procedure TBrowserFrame.Chromium1StatusMessage( Sender : TObject;
|
||||||
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl: ustring;
|
const browser : ICefBrowser;
|
||||||
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
|
const value : ustring);
|
||||||
out Result: Boolean);
|
|
||||||
begin
|
|
||||||
// For simplicity, this demo blocks all popup windows and new tabs
|
|
||||||
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1StatusMessage(Sender: TObject;
|
|
||||||
const browser: ICefBrowser; const value: ustring);
|
|
||||||
begin
|
begin
|
||||||
StatusBar1.Panels[0].Text := value;
|
StatusBar1.Panels[0].Text := value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBrowserFrame.Chromium1TitleChange(Sender: TObject;
|
procedure TBrowserFrame.Chromium1TitleChange( Sender : TObject;
|
||||||
const browser: ICefBrowser; const title: ustring);
|
const browser : ICefBrowser;
|
||||||
|
const title : ustring);
|
||||||
begin
|
begin
|
||||||
if not(assigned(FOnBrowserTitleChange)) then exit;
|
if not(assigned(FOnBrowserTitleChange)) then exit;
|
||||||
|
|
||||||
@ -296,6 +326,16 @@ begin
|
|||||||
CEFWindowParent1.Free;
|
CEFWindowParent1.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TBrowserFrame.CreateClientHandler(var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
const targetFrameName : string;
|
||||||
|
const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
begin
|
||||||
|
Result := assigned(Parent) and
|
||||||
|
(Parent is TBrowserTab) and
|
||||||
|
TBrowserTab(Parent).CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures);
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ uses
|
|||||||
Windows, Classes, Messages, ComCtrls, Controls,
|
Windows, Classes, Messages, ComCtrls, Controls,
|
||||||
Forms,
|
Forms,
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
uBrowserFrame;
|
uCEFInterfaces, uCEFTypes, uBrowserFrame;
|
||||||
|
|
||||||
type
|
type
|
||||||
TBrowserTab = class(TTabSheet)
|
TBrowserTab = class(TTabSheet)
|
||||||
@ -73,6 +73,7 @@ type
|
|||||||
procedure CloseBrowser;
|
procedure CloseBrowser;
|
||||||
procedure ShowBrowser;
|
procedure ShowBrowser;
|
||||||
procedure HideBrowser;
|
procedure HideBrowser;
|
||||||
|
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
|
||||||
property TabID : cardinal read FTabID;
|
property TabID : cardinal read FTabID;
|
||||||
end;
|
end;
|
||||||
@ -161,4 +162,17 @@ begin
|
|||||||
Caption := aTitle;
|
Caption := aTitle;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TBrowserTab.CreateClientHandler(var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
const targetFrameName : string;
|
||||||
|
const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
var
|
||||||
|
TempForm : TCustomForm;
|
||||||
|
begin
|
||||||
|
TempForm := ParentForm;
|
||||||
|
Result := (TempForm <> nil) and
|
||||||
|
(TempForm is TMainForm) and
|
||||||
|
TMainForm(TempForm).CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures);
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
37
demos/Delphi_VCL/TabbedBrowser2/uChildForm.dfm
Normal file
37
demos/Delphi_VCL/TabbedBrowser2/uChildForm.dfm
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
object ChildForm: TChildForm
|
||||||
|
Left = 0
|
||||||
|
Top = 0
|
||||||
|
Caption = 'Popup'
|
||||||
|
ClientHeight = 256
|
||||||
|
ClientWidth = 352
|
||||||
|
Color = clBtnFace
|
||||||
|
Font.Charset = DEFAULT_CHARSET
|
||||||
|
Font.Color = clWindowText
|
||||||
|
Font.Height = -11
|
||||||
|
Font.Name = 'Tahoma'
|
||||||
|
Font.Style = []
|
||||||
|
OldCreateOrder = False
|
||||||
|
Position = poScreenCenter
|
||||||
|
OnClose = FormClose
|
||||||
|
OnCloseQuery = FormCloseQuery
|
||||||
|
OnCreate = FormCreate
|
||||||
|
OnDestroy = FormDestroy
|
||||||
|
PixelsPerInch = 96
|
||||||
|
TextHeight = 13
|
||||||
|
object CEFWindowParent1: TCEFWindowParent
|
||||||
|
Left = 0
|
||||||
|
Top = 0
|
||||||
|
Width = 352
|
||||||
|
Height = 256
|
||||||
|
Align = alClient
|
||||||
|
TabOrder = 0
|
||||||
|
end
|
||||||
|
object Chromium1: TChromium
|
||||||
|
OnTitleChange = Chromium1TitleChange
|
||||||
|
OnBeforePopup = Chromium1BeforePopup
|
||||||
|
OnBeforeClose = Chromium1BeforeClose
|
||||||
|
OnClose = Chromium1Close
|
||||||
|
Left = 24
|
||||||
|
Top = 56
|
||||||
|
end
|
||||||
|
end
|
257
demos/Delphi_VCL/TabbedBrowser2/uChildForm.pas
Normal file
257
demos/Delphi_VCL/TabbedBrowser2/uChildForm.pas
Normal file
@ -0,0 +1,257 @@
|
|||||||
|
// ************************************************************************
|
||||||
|
// ***************************** CEF4Delphi *******************************
|
||||||
|
// ************************************************************************
|
||||||
|
//
|
||||||
|
// CEF4Delphi is based on DCEF3 which uses CEF 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 © 2020 Salvador Diaz Fau. All rights reserved.
|
||||||
|
//
|
||||||
|
// ************************************************************************
|
||||||
|
// ************ 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 uChildForm;
|
||||||
|
|
||||||
|
{$I cef.inc}
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
{$IFDEF DELPHI16_UP}
|
||||||
|
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
|
||||||
|
System.SyncObjs, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
|
||||||
|
Vcl.ExtCtrls,
|
||||||
|
{$ELSE}
|
||||||
|
Windows, Messages, SysUtils, Variants, Classes, SyncObjs,
|
||||||
|
Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
|
||||||
|
{$ENDIF}
|
||||||
|
uCEFChromium, uCEFTypes, uCEFInterfaces, uCEFConstants, uCEFWindowParent, uCEFWinControl,
|
||||||
|
uCEFChromiumCore;
|
||||||
|
|
||||||
|
type
|
||||||
|
TChildForm = class(TForm)
|
||||||
|
Chromium1: TChromium;
|
||||||
|
CEFWindowParent1: TCEFWindowParent;
|
||||||
|
|
||||||
|
procedure FormCreate(Sender: TObject);
|
||||||
|
procedure FormDestroy(Sender: TObject);
|
||||||
|
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
||||||
|
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
||||||
|
|
||||||
|
procedure Chromium1BeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue; var noJavascriptAccess: Boolean; var Result: Boolean);
|
||||||
|
procedure Chromium1TitleChange(Sender: TObject; const browser: ICefBrowser; const title: ustring);
|
||||||
|
procedure Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
|
||||||
|
procedure Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
|
||||||
|
|
||||||
|
protected
|
||||||
|
FCanClose : boolean;
|
||||||
|
FClosing : boolean;
|
||||||
|
FClientInitialized : boolean;
|
||||||
|
FPopupFeatures : TCefPopupFeatures;
|
||||||
|
|
||||||
|
procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
|
||||||
|
procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
|
||||||
|
procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP;
|
||||||
|
procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP;
|
||||||
|
procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY;
|
||||||
|
|
||||||
|
public
|
||||||
|
procedure AfterConstruction; override;
|
||||||
|
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
procedure ApplyPopupFeatures;
|
||||||
|
|
||||||
|
property ClientInitialized : boolean read FClientInitialized;
|
||||||
|
property Closing : boolean read FClosing;
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
{$R *.dfm}
|
||||||
|
|
||||||
|
uses
|
||||||
|
{$IFDEF DELPHI16_UP}
|
||||||
|
System.Math,
|
||||||
|
{$ELSE}
|
||||||
|
Math,
|
||||||
|
{$ENDIF}
|
||||||
|
uCEFMiscFunctions, uCEFApplication, uMainForm;
|
||||||
|
|
||||||
|
// Destruction steps
|
||||||
|
// =================
|
||||||
|
// 1. FormCloseQuery sets CanClose to FALSE calls TChromium.CloseBrowser which triggers the TChromium.OnClose event.
|
||||||
|
// 2. TChromium.OnClose sends a CEFBROWSER_DESTROY message to destroy CEFWindowParent1 in the main thread, which triggers the TChromium.OnBeforeClose event.
|
||||||
|
// 3. TChromium.OnBeforeClose sets FCanClose := True and sends WM_CLOSE to the form.
|
||||||
|
|
||||||
|
procedure TChildForm.AfterConstruction;
|
||||||
|
begin
|
||||||
|
inherited AfterConstruction;
|
||||||
|
|
||||||
|
CreateHandle;
|
||||||
|
|
||||||
|
CEFWindowParent1.CreateHandle;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TChildForm.CreateClientHandler(var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
const targetFrameName : string;
|
||||||
|
const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
var
|
||||||
|
TempRect : TRect;
|
||||||
|
begin
|
||||||
|
if CEFWindowParent1.HandleAllocated and
|
||||||
|
Chromium1.CreateClientHandler(client, False) then
|
||||||
|
begin
|
||||||
|
Result := True;
|
||||||
|
FClientInitialized := True;
|
||||||
|
FPopupFeatures := popupFeatures;
|
||||||
|
TempRect := CEFWindowParent1.ClientRect;
|
||||||
|
|
||||||
|
if (FPopupFeatures.widthset <> 0) then TempRect.Right := max(FPopupFeatures.width, 100);
|
||||||
|
if (FPopupFeatures.heightset <> 0) then TempRect.Bottom := max(FPopupFeatures.height, 100);
|
||||||
|
|
||||||
|
WindowInfoAsChild(windowInfo, CEFWindowParent1.Handle, TempRect, '');
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Result := False;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.ApplyPopupFeatures;
|
||||||
|
begin
|
||||||
|
if (FPopupFeatures.xset <> 0) then Chromium1.SetFormLeftTo(FPopupFeatures.x);
|
||||||
|
if (FPopupFeatures.yset <> 0) then Chromium1.SetFormTopTo(FPopupFeatures.y);
|
||||||
|
if (FPopupFeatures.widthset <> 0) then Chromium1.ResizeFormWidthTo(FPopupFeatures.width);
|
||||||
|
if (FPopupFeatures.heightset <> 0) then Chromium1.ResizeFormHeightTo(FPopupFeatures.height);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
|
||||||
|
begin
|
||||||
|
FCanClose := True;
|
||||||
|
PostMessage(Handle, WM_CLOSE, 0, 0);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.Chromium1BeforePopup(Sender : TObject;
|
||||||
|
const browser : ICefBrowser;
|
||||||
|
const frame : ICefFrame;
|
||||||
|
const targetUrl : ustring;
|
||||||
|
const targetFrameName : ustring;
|
||||||
|
targetDisposition : TCefWindowOpenDisposition;
|
||||||
|
userGesture : Boolean;
|
||||||
|
const popupFeatures : TCefPopupFeatures;
|
||||||
|
var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
var settings : TCefBrowserSettings;
|
||||||
|
var extra_info : ICefDictionaryValue;
|
||||||
|
var noJavascriptAccess : Boolean;
|
||||||
|
var Result : Boolean);
|
||||||
|
begin
|
||||||
|
case targetDisposition of
|
||||||
|
WOD_NEW_FOREGROUND_TAB,
|
||||||
|
WOD_NEW_BACKGROUND_TAB,
|
||||||
|
WOD_NEW_WINDOW : Result := True; // For simplicity, this demo blocks new tabs and new windows.
|
||||||
|
|
||||||
|
WOD_NEW_POPUP : Result := not(TMainForm(Owner).CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures));
|
||||||
|
|
||||||
|
else Result := False;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
|
||||||
|
begin
|
||||||
|
PostMessage(Handle, CEF_DESTROY, 0, 0);
|
||||||
|
aAction := cbaDelay;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.Chromium1TitleChange(Sender: TObject; const browser: ICefBrowser; const title: ustring);
|
||||||
|
begin
|
||||||
|
Caption := title;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.WMMove(var aMessage : TWMMove);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
|
||||||
|
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.WMMoving(var aMessage : TMessage);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
|
||||||
|
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.WMEnterMenuLoop(var aMessage: TMessage);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
|
||||||
|
if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := True;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.WMExitMenuLoop(var aMessage: TMessage);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
|
||||||
|
if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := False;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
||||||
|
begin
|
||||||
|
Action := caFree;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
||||||
|
begin
|
||||||
|
CanClose := FCanClose;
|
||||||
|
|
||||||
|
if not(FClosing) then
|
||||||
|
begin
|
||||||
|
FClosing := True;
|
||||||
|
Visible := False;
|
||||||
|
Chromium1.CloseBrowser(True);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.FormCreate(Sender: TObject);
|
||||||
|
begin
|
||||||
|
FCanClose := False;
|
||||||
|
FClosing := False;
|
||||||
|
FClientInitialized := False;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.FormDestroy(Sender: TObject);
|
||||||
|
begin
|
||||||
|
if FClientInitialized and TMainForm(Owner).HandleAllocated then
|
||||||
|
PostMessage(TMainForm(Owner).Handle, CEF_CHILDDESTROYED, 0, 0);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TChildForm.BrowserDestroyMsg(var aMessage : TMessage);
|
||||||
|
begin
|
||||||
|
CEFWindowParent1.Free;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
@ -14,6 +14,7 @@ object MainForm: TMainForm
|
|||||||
Position = poScreenCenter
|
Position = poScreenCenter
|
||||||
OnCloseQuery = FormCloseQuery
|
OnCloseQuery = FormCloseQuery
|
||||||
OnCreate = FormCreate
|
OnCreate = FormCreate
|
||||||
|
OnDestroy = FormDestroy
|
||||||
OnShow = FormShow
|
OnShow = FormShow
|
||||||
PixelsPerInch = 96
|
PixelsPerInch = 96
|
||||||
TextHeight = 13
|
TextHeight = 13
|
||||||
|
@ -43,19 +43,19 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
{$IFDEF DELPHI16_UP}
|
{$IFDEF DELPHI16_UP}
|
||||||
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
|
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.SyncObjs,
|
||||||
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
|
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, Vcl.Buttons, Vcl.ExtCtrls,
|
||||||
Vcl.ComCtrls, Vcl.ToolWin, Vcl.Buttons, Vcl.ExtCtrls,
|
|
||||||
{$ELSE}
|
{$ELSE}
|
||||||
Windows, Messages, SysUtils, Variants, Classes, Graphics,
|
Windows, Messages, SysUtils, Variants, Classes, Graphics, SyncObjs,
|
||||||
Controls, Forms, Dialogs,
|
Controls, Forms, Dialogs, ComCtrls, ToolWin, Buttons, ExtCtrls,
|
||||||
ComCtrls, ToolWin, Buttons, ExtCtrls,
|
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
uCEFApplication, uCEFTypes, uCEFConstants;
|
uCEFApplication, uCEFInterfaces, uCEFTypes, uCEFConstants, uChildForm;
|
||||||
|
|
||||||
const
|
const
|
||||||
CEF_INITIALIZED = WM_APP + $100;
|
CEF_INITIALIZED = WM_APP + $A50;
|
||||||
CEF_DESTROYTAB = WM_APP + $101;
|
CEF_DESTROYTAB = WM_APP + $A51;
|
||||||
|
CEF_CREATENEXTCHILD = WM_APP + $A52;
|
||||||
|
CEF_CHILDDESTROYED = WM_APP + $A53;
|
||||||
|
|
||||||
HOMEPAGE_URL = 'https://www.google.com';
|
HOMEPAGE_URL = 'https://www.google.com';
|
||||||
DEFAULT_TAB_CAPTION = 'New tab';
|
DEFAULT_TAB_CAPTION = 'New tab';
|
||||||
@ -73,28 +73,36 @@ type
|
|||||||
procedure FormShow(Sender: TObject);
|
procedure FormShow(Sender: TObject);
|
||||||
procedure FormCreate(Sender: TObject);
|
procedure FormCreate(Sender: TObject);
|
||||||
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
||||||
|
procedure FormDestroy(Sender: TObject);
|
||||||
|
|
||||||
protected
|
protected
|
||||||
// Variables to control when can we destroy the form safely
|
FChildForm : TChildForm;
|
||||||
FCanClose : boolean;
|
FCriticalSection : TCriticalSection;
|
||||||
FClosing : boolean;
|
FCanClose : boolean;
|
||||||
|
FClosing : boolean; // Set to True in the CloseQuery event.
|
||||||
FLastTabID : cardinal; // Used by NextTabID to generate unique tab IDs
|
FLastTabID : cardinal; // Used by NextTabID to generate unique tab IDs
|
||||||
|
|
||||||
function GetNextTabID : cardinal;
|
function GetNextTabID : cardinal;
|
||||||
|
function GetPopupChildCount : integer;
|
||||||
|
|
||||||
procedure EnableButtonPnl;
|
procedure EnableButtonPnl;
|
||||||
function CloseAllTabs : boolean;
|
function CloseAllBrowsers : boolean;
|
||||||
procedure CloseTab(aIndex : integer);
|
procedure CloseTab(aIndex : integer);
|
||||||
|
|
||||||
procedure CEFInitializedMsg(var aMessage : TMessage); message CEF_INITIALIZED;
|
procedure CEFInitializedMsg(var aMessage : TMessage); message CEF_INITIALIZED;
|
||||||
procedure DestroyTabMsg(var aMessage : TMessage); message CEF_DESTROYTAB;
|
procedure DestroyTabMsg(var aMessage : TMessage); message CEF_DESTROYTAB;
|
||||||
|
procedure CreateNextChildMsg(var aMessage : TMessage); message CEF_CREATENEXTCHILD;
|
||||||
|
procedure ChildDestroyedMsg(var aMessage : TMessage); message CEF_CHILDDESTROYED;
|
||||||
procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
|
procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
|
||||||
procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
|
procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
|
||||||
procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP;
|
procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP;
|
||||||
procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP;
|
procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP;
|
||||||
|
|
||||||
property NextTabID : cardinal read GetNextTabID;
|
property NextTabID : cardinal read GetNextTabID;
|
||||||
|
property PopupChildCount : integer read GetPopupChildCount;
|
||||||
|
|
||||||
|
public
|
||||||
|
function CreateClientHandler(var windowInfo : TCefWindowInfo; var client : ICefClient; const targetFrameName : string; const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
@ -173,6 +181,28 @@ begin
|
|||||||
Result := FLastTabID;
|
Result := FLastTabID;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TMainForm.GetPopupChildCount : integer;
|
||||||
|
var
|
||||||
|
i : integer;
|
||||||
|
TempForm : TCustomForm;
|
||||||
|
begin
|
||||||
|
Result := 0;
|
||||||
|
i := pred(screen.CustomFormCount);
|
||||||
|
|
||||||
|
while (i >= 0) do
|
||||||
|
begin
|
||||||
|
TempForm := screen.CustomForms[i];
|
||||||
|
|
||||||
|
// Only count the fully initialized child forms and not the one waiting to be used.
|
||||||
|
|
||||||
|
if (TempForm is TChildForm) and
|
||||||
|
TChildForm(TempForm).ClientInitialized then
|
||||||
|
inc(Result);
|
||||||
|
|
||||||
|
dec(i);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TMainForm.AddTabBtnClick(Sender: TObject);
|
procedure TMainForm.AddTabBtnClick(Sender: TObject);
|
||||||
var
|
var
|
||||||
TempNewTab : TBrowserTab;
|
TempNewTab : TBrowserTab;
|
||||||
@ -188,6 +218,9 @@ end;
|
|||||||
procedure TMainForm.CEFInitializedMsg(var aMessage : TMessage);
|
procedure TMainForm.CEFInitializedMsg(var aMessage : TMessage);
|
||||||
begin
|
begin
|
||||||
EnableButtonPnl;
|
EnableButtonPnl;
|
||||||
|
|
||||||
|
if (FChildForm = nil) then
|
||||||
|
TChildForm.Create(self);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TMainForm.DestroyTabMsg(var aMessage : TMessage);
|
procedure TMainForm.DestroyTabMsg(var aMessage : TMessage);
|
||||||
@ -209,13 +242,39 @@ begin
|
|||||||
inc(i);
|
inc(i);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
if FClosing and (BrowserPageCtrl.PageCount = 0) then
|
if FClosing and (PopupChildCount = 0) and (BrowserPageCtrl.PageCount = 0) then
|
||||||
begin
|
begin
|
||||||
FCanClose := True;
|
FCanClose := True;
|
||||||
PostMessage(Handle, WM_CLOSE, 0, 0);
|
PostMessage(Handle, WM_CLOSE, 0, 0);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TMainForm.ChildDestroyedMsg(var aMessage : TMessage);
|
||||||
|
begin
|
||||||
|
if FClosing and (PopupChildCount = 0) and (BrowserPageCtrl.PageCount = 0) then
|
||||||
|
begin
|
||||||
|
FCanClose := True;
|
||||||
|
PostMessage(Handle, WM_CLOSE, 0, 0);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TMainForm.CreateNextChildMsg(var aMessage : TMessage);
|
||||||
|
begin
|
||||||
|
try
|
||||||
|
FCriticalSection.Acquire;
|
||||||
|
|
||||||
|
if (FChildForm <> nil) then
|
||||||
|
begin
|
||||||
|
FChildForm.ApplyPopupFeatures;
|
||||||
|
FChildForm.Show;
|
||||||
|
end;
|
||||||
|
|
||||||
|
FChildForm := TChildForm.Create(self);
|
||||||
|
finally
|
||||||
|
FCriticalSection.Release;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
||||||
begin
|
begin
|
||||||
CanClose := FCanClose;
|
CanClose := FCanClose;
|
||||||
@ -225,7 +284,7 @@ begin
|
|||||||
FClosing := True;
|
FClosing := True;
|
||||||
ButtonPnl.Enabled := False;
|
ButtonPnl.Enabled := False;
|
||||||
|
|
||||||
if not(CloseAllTabs) then
|
if not(CloseAllBrowsers) then
|
||||||
begin
|
begin
|
||||||
FCanClose := True;
|
FCanClose := True;
|
||||||
PostMessage(Handle, WM_CLOSE, 0, 0);
|
PostMessage(Handle, WM_CLOSE, 0, 0);
|
||||||
@ -235,15 +294,27 @@ end;
|
|||||||
|
|
||||||
procedure TMainForm.FormCreate(Sender: TObject);
|
procedure TMainForm.FormCreate(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
FCanClose := False;
|
FCanClose := False;
|
||||||
FClosing := False;
|
FClosing := False;
|
||||||
FLastTabID := 0;
|
FLastTabID := 0;
|
||||||
|
FChildForm := nil;
|
||||||
|
FCriticalSection := TCriticalSection.Create;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TMainForm.FormDestroy(Sender: TObject);
|
||||||
|
begin
|
||||||
|
FreeAndNil(FCriticalSection);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TMainForm.FormShow(Sender: TObject);
|
procedure TMainForm.FormShow(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
if (GlobalCEFApp <> nil) and GlobalCEFApp.GlobalContextInitialized then
|
if (GlobalCEFApp <> nil) and GlobalCEFApp.GlobalContextInitialized then
|
||||||
EnableButtonPnl;
|
begin
|
||||||
|
EnableButtonPnl;
|
||||||
|
|
||||||
|
if (FChildForm = nil) then
|
||||||
|
FChildForm := TChildForm.Create(self);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TMainForm.RemoveTabBtnClick(Sender: TObject);
|
procedure TMainForm.RemoveTabBtnClick(Sender: TObject);
|
||||||
@ -251,12 +322,30 @@ begin
|
|||||||
CloseTab(BrowserPageCtrl.ActivePageIndex);
|
CloseTab(BrowserPageCtrl.ActivePageIndex);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TMainForm.CloseAllTabs : boolean;
|
function TMainForm.CloseAllBrowsers : boolean;
|
||||||
var
|
var
|
||||||
i : integer;
|
i : integer;
|
||||||
|
TempForm : TCustomForm;
|
||||||
begin
|
begin
|
||||||
Result := False;
|
Result := False;
|
||||||
i := pred(BrowserPageCtrl.PageCount);
|
i := pred(screen.CustomFormCount);
|
||||||
|
|
||||||
|
while (i >= 0) do
|
||||||
|
begin
|
||||||
|
TempForm := screen.CustomForms[i];
|
||||||
|
|
||||||
|
if (TempForm is TChildForm) and
|
||||||
|
TChildForm(TempForm).ClientInitialized and
|
||||||
|
not(TChildForm(TempForm).Closing) then
|
||||||
|
begin
|
||||||
|
PostMessage(TempForm.Handle, WM_CLOSE, 0, 0);
|
||||||
|
Result := True;
|
||||||
|
end;
|
||||||
|
|
||||||
|
dec(i);
|
||||||
|
end;
|
||||||
|
|
||||||
|
i := pred(BrowserPageCtrl.PageCount);
|
||||||
|
|
||||||
while (i >= 0) do
|
while (i >= 0) do
|
||||||
begin
|
begin
|
||||||
@ -316,4 +405,20 @@ begin
|
|||||||
GlobalCEFApp.OsmodalLoop := False;
|
GlobalCEFApp.OsmodalLoop := False;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TMainForm.CreateClientHandler(var windowInfo : TCefWindowInfo;
|
||||||
|
var client : ICefClient;
|
||||||
|
const targetFrameName : string;
|
||||||
|
const popupFeatures : TCefPopupFeatures) : boolean;
|
||||||
|
begin
|
||||||
|
try
|
||||||
|
FCriticalSection.Acquire;
|
||||||
|
|
||||||
|
Result := (FChildForm <> nil) and
|
||||||
|
FChildForm.CreateClientHandler(windowInfo, client, targetFrameName, popupFeatures) and
|
||||||
|
PostMessage(Handle, CEF_CREATENEXTCHILD, 0, 0);
|
||||||
|
finally
|
||||||
|
FCriticalSection.Release;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"UpdateLazPackages" : [
|
"UpdateLazPackages" : [
|
||||||
{
|
{
|
||||||
"ForceNotify" : true,
|
"ForceNotify" : true,
|
||||||
"InternalVersion" : 208,
|
"InternalVersion" : 209,
|
||||||
"Name" : "cef4delphi_lazarus.lpk",
|
"Name" : "cef4delphi_lazarus.lpk",
|
||||||
"Version" : "87.1.12.0"
|
"Version" : "87.1.12.0"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user