Recent

Author Topic: TWebBrowser control for LCL Cocoa  (Read 13469 times)

Phil

  • Hero Member
  • *****
  • Posts: 2737
TWebBrowser control for LCL Cocoa
« on: July 05, 2017, 10:15:42 pm »
Here's an initial proof-of-concept Web browser control:

https://macpgmr.github.io/MacXPlatform/lclwebbrowser-src.zip

Currently implemented only for LCL Cocoa widgetset by wrapping the WebView class that's part of every Mac's WebKit framework.

You can install this control in Lazarus with any widgetset to use as a placeholder when designing, but when run nothing will appear in the web control's area on the form since it's not implemented for other widgetsets (yet).

Note that by default macOS blocks calls to insecure HTTP sites. You can relax this restriction by setting the NSAllowsArbitraryLoads key in your app's Info.plist file, but for testing you might stick with HTTPS sites.

Note also that only a single method is implemented: LoadPage, which you pass a URL into.

This control does not add any bulk to your app and does not require installation of any additional libraries. Everything it needs is already on your Mac and is accessible via FPC CocoaAll unit.

Tested with 64-bit Cocoa on trunk, but will also work with 32-bit Cocoa and probably works with Laz 1.8, maybe even 1.6.4 - depends on whether anything critical to custom controls has been added to the Cocoa widgetset since those releases.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #1 on: July 06, 2017, 03:46:32 am »
Tested with 64-bit Cocoa on trunk, but will also work with 32-bit Cocoa and probably works with Laz 1.8, maybe even 1.6.4 - depends on whether anything critical to custom controls has been added to the Cocoa widgetset since those releases.

Sounds like one of the obstacles is just compiling the Cocoa widgetset in trunk such that the existing Lazarus installation doesn't get messed up. Here are some brief steps that might make the it a little easier and explain a bit about it in the process:

https://macpgmr.github.io/MacXPlatform/UsingCocoaFromTrunk.html


Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #2 on: July 07, 2017, 10:16:38 pm »

Added OnPageLoaded and OnPageLoadError event properties to TWebBrowser so you can know when the page is done loading or whether there was an error.

I was going to look at what might be involved in adding support for Qt to TWebBrowser, but the Qt 5 QWebEngineView class and related classes are missing from qt56.pas in Laz 1.8 and trunk. Does anybody know anything about that? Can't proceed without them.

Also, has anyone ever compiled an LCL app on Mac with the Qt5 widgetset?


carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: TWebBrowser control for LCL Cocoa
« Reply #3 on: July 08, 2017, 01:01:24 am »
Hi Phil, I've tried it in Lazarus 1.8.0 RC3 on the Apple Mac on 10.12.5 Sierra. I've simply got a form with:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.   WebBrowser1.LoadPage('https://www.google.com');
  4. end;
  5.  

But I'm not seeing any web page.

I also added an OnPageLoadError event handler but that's not getting fired.

Cheers,
Carl
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #4 on: July 08, 2017, 01:12:20 am »

But I'm not seeing any web page.

I also added an OnPageLoadError event handler but that's not getting fired.


You're probably using the Carbon widgetset, which is the Lazarus default. It won't work with Carbon, so you're seeing the right result. You need to use the Cocoa widgetset.

Read this and see if it makes sense to you:

https://macpgmr.github.io/MacXPlatform/UsingCocoaFromTrunk.html

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: TWebBrowser control for LCL Cocoa
« Reply #5 on: July 08, 2017, 01:21:00 am »
Thanks, Phil. Actually, it's pretty late here in the UK. It'll probably make a lot more sense in the morning after a coffee or two!
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #6 on: July 08, 2017, 01:28:35 am »
Thanks, Phil. Actually, it's pretty late here in the UK. It'll probably make a lot more sense in the morning after a coffee or two!

I just tested and it does work with RC3 Cocoa. Do this:

In your test project's Project Options, under Additions and Overrides, change Carbon to Cocoa. Then recompile. That will create a 32-bit Cocoa app.

See if that works for you.

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: TWebBrowser control for LCL Cocoa
« Reply #7 on: July 08, 2017, 01:37:49 am »
Yay! That works. Thanks, Phil.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #8 on: July 08, 2017, 01:41:19 am »
Yay! That works. Thanks, Phil.

With Cocoa, I would usually recommend working with the trunk code. The Cocoa widgetset source included with RC3 (and what will ship with final 1.8) is already 3 months behind where the trunk Cocoa code is. More than half of the Cocoa widgetset source files have been modified in the last 3 months. The Cocoa widgetset is still incomplete, but the trunk version is less incomplete than what 1.8 will have.


Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #9 on: July 18, 2017, 07:03:32 pm »
Preliminary support added to TWebBrowser for qt widgetset (Qt4).

https://macpgmr.github.io/MacXPlatform/lclwebbrowser-src.zip

Also added an example app that shows how to embed a Web app into a form in a Lazarus app. Basically the Lazarus version of this Xcode app:

https://macpgmr.github.io/MacXPlatform/WebAppOverview_3.html#EmbeddedBrowser

With Qt4, your mileage my vary. On Mac, the final version of Qt 4.8 (4.8.6) is 64-bit only, so the available 32-bit interface framework won't work. My attempts to build the interface framework for 64-bit failed.

On Windows, not sure that Qt's QWebView ever worked that well. It seems to have problems with some Web sites.

Also, on 64-bit Windows, the Lazarus installer puts the Qt4Pas.dll interface library in the _wrong_ place, so you'll need to copy it to your app's directory or put it somewhere on the path for your app to find it. And don't forget the 6 other DLLs needed (QtCore.dll, etc.).


Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #10 on: July 19, 2017, 08:58:44 pm »
With Qt4, your mileage my vary.

The PasMap example app for TWebBrowser works fine on Ubuntu 14.04 with the final Qt 4 libraries installed (4.8.6). I used the libQt4Pas.so interface library as is without building it myself.

Alas, on Windows, the Qt4Pas5.dll interface library is incompatible with the final Qt 4 libraries (4.8.6 from May 2014). So not only does Lazarus install this library in the wrong place (maybe just as well), the library can't even be used with Qt 4.8.6.


Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #11 on: July 20, 2017, 12:53:26 am »
Here's a useful tidbit I just discovered. On Mac, TWebBrowser can load .pdf files as well as Web (HTML) pages.

Test this yourself. Create a new project in Laz, drop a TWebBrowser on the main form, then add a button and a click handler that has code that looks something like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.LoadPage('https://www.freepascal.org/~michael/articles/lazonmac/lazonmac.pdf');
end;

When you click the button, Michael's .pdf will be loaded. Right-click on it to view a popup menu for zooming the document, etc.

Sorry, not supported by Qt's QWebView class, Cocoa only.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #12 on: July 22, 2017, 05:14:56 pm »
Added EvaluateJavaScript method to TWebBrowser. Example code:

     WebBrowser1.EvaluateJavaScript(
      'function DoSum(){var a = 1; var b = 2; var c = a + b; return c;} DoSum();');

Method returns '3'.

Works with both Cocoa and Qt widgetsets.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TWebBrowser control for LCL Cocoa
« Reply #13 on: July 26, 2017, 07:50:53 pm »
Added OnLinkClick event property to TWebBrowser so you can handle clicks on links that need to be opened in an external browser (target='_blank').

https://macpgmr.github.io/MacXPlatform/lclwebbrowser-src.zip

Supports Cocoa widgetset on Mac and Qt widgetset with Qt 4.8.6 libraries.

Use cases for TWebBrowser:

- Display within your app local or Web-based HTML files for help or other docs. Feel free to use HTML5, CSS and JavaScript as needed in your docs.

- Embed a Web app within your app.
  - "Appify" a Web app as a way to avoid writing it twice.
  - "Hybrid" app where users are restricted to specific Web pages.

The included PasMap app is an example of an embedded Web app, where it embeds the qooxdoo- / OpenLayers-based QxMap Web app in the desktop app.

- Display HTML that your app generates on the fly, for example reports. Again, include CSS and JS as needed.

Possible future use: Since WebKit-based browsers can edit as well as display HTML, it seems like someone could write a THtmlMemo control analogous to TMemo and TRichMemo, only instead of text or RTF, it edits HTML.

SunyD

  • Guest
Re: TWebBrowser control for LCL Cocoa
« Reply #14 on: November 22, 2017, 02:32:25 pm »
Hello,
I want create InternetExplorer part of this package. I started it, but I have problems with creating widget. I don't know about Widgetsets.
If someone can solve this problem (creating the widget) I can fill it with "life".

I downloaded this https://macpgmr.github.io/MacXPlatform/lclwebbrowser-src.zip and worked on it.  Here is my work:


 

TinyPortal © 2005-2018