Difference between revisions of "Webbrowser"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
Webbrowser
+
{{Webbrowser}}
 +
 
 +
This page summarizes various ways to display HTML pages from a Pascal program, ranging from calling an external browser to adding a component capable of rendering the html page into a form.
 +
 
 +
=Starting a web browser to show a page/URL=
 +
 
 +
==OpenURL==
 +
 
 +
OpenURL is simplest way of opening URLs in general. This procedure will detect default web browser and open URL from parameter in it. It can be used for mail address of the form <nowiki>'mailto:aname@AnAddress?subject=:::A subject line'</nowiki>, web addresses <nowiki>'http://'</nowiki> and files <nowiki>'file:///'</nowiki>.
 +
 
 +
<syntaxhighlight>
 +
uses LCLIntf;
 +
...
 +
OpenURL('http://www.lazarus.freepascal.org');
 +
</syntaxhighlight>
 +
 
 +
==Finding the default webbrowser==
 +
 
 +
Every platform has a mechanism for its default platform. The LCL unit '''lazhelphtml''' contains a class THTMLBrowserHelpViewer to start a web browser for the LCL help system.
 +
You can use its method FindDefaultBrowser to find the default browser and the parameter to start. For example:
 +
 
 +
<syntaxhighlight>
 +
uses
 +
  Classes, ..., LCLProc, LazHelpHTML;
 +
 
 +
...
 +
 
 +
implementation
 +
 
 +
procedure TMainForm.Button1Click(Sender: TObject);
 +
var
 +
  v: THTMLBrowserHelpViewer;
 +
  BrowserPath, BrowserParams: string;
 +
begin
 +
  v:=THTMLBrowserHelpViewer.Create(nil);
 +
  v.FindDefaultBrowser(BrowserPath,BrowserParams);
 +
  debugln(['Path=',BrowserPath,' Params=',BrowserParams]);
 +
  v.Free;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
This gives for example under Ubuntu (Linux):
 +
Browser=/usr/bin/xdg-open Params=%s
 +
Under Windows you can get:
 +
Browser=C:\windows\system32\rundll32.exe Params=url.dll,FileProtocolHandler %s
 +
 
 +
==Starting the browser==
 +
 
 +
Once you know the command line and parameters you can use TProcessUTF8 to start the browser:
 +
 
 +
<syntaxhighlight>
 +
uses
 +
  Classes, ..., LCLProc, LazHelpHTML, UTF8Process;
 +
 
 +
...
 +
 
 +
implementation
 +
 
 +
procedure TMainForm.Button1Click(Sender: TObject);
 +
var
 +
  v: THTMLBrowserHelpViewer;
 +
  BrowserPath, BrowserParams: string;
 +
  p: LongInt;
 +
  URL: String;
 +
  BrowserProcess: TProcessUTF8;
 +
begin
 +
  v:=THTMLBrowserHelpViewer.Create(nil);
 +
  try
 +
    v.FindDefaultBrowser(BrowserPath,BrowserParams);
 +
    debugln(['Path=',BrowserPath,' Params=',BrowserParams]);
 +
 
 +
    URL:='http://www.lazarus.freepascal.org';
 +
    p:=System.Pos('%s', BrowserParams);
 +
    System.Delete(BrowserParams,p,2);
 +
    System.Insert(URL,BrowserParams,p);
 +
 
 +
    // start browser
 +
    BrowserProcess:=TProcessUTF8.Create(nil);
 +
    try
 +
      BrowserProcess.CommandLine:=BrowserPath+' '+BrowserParams;
 +
      BrowserProcess.Execute;
 +
    finally
 +
      BrowserProcess.Free;
 +
    end;
 +
  finally
 +
    v.Free;
 +
  end;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
=Integrate web browser component in application=
 +
 
 +
==Using the Turbopower internet pro control==
 +
 
 +
Lazarus provides the package TurboPowerIPro (lazarus/components/turbopower_ipro/turbopoweripro.lpk) with the following features:
 +
* It contains a control to put onto a form. When you install the package in the IDE, you get some new components in the palette, so you can drop them onto a form just like any LCL control.
 +
* It is written completely in pascal and therefore works on all platforms out of the box without any extra installation.
 +
* You have the full control, what files/urls are opened.
 +
* It does not have all the features of a full webbrowser. No multimedia stuff, javascript or flash. This must be implemented by you.
 +
 
 +
There are some examples in lazarus/components/turbopower_ipro/examples demonstrating how to open a local html file with some images, hyperlinks and history buttons.
 +
 
 +
==QT Webkit==
 +
 
 +
When using the Qt WidgetSet, you can use the Qt WebKit to embed a browser into a LCL form.
 +
 
 +
Lazarus LCL/Qt WebKit Demo available on [http://users.telenet.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html FPC Qt4 Binding Web Page]
 +
 
 +
==[[THtmlPort]]==
 +
 
 +
THtmlPort is a Lazarus/Free Pascal version of Dave Baldwin's HTML Components, including THtmlViewer, TFrameViewer and TFrameBrowser.
 +
 
 +
==[[GeckoPort_version2|GeckoPort]]==
 +
 
 +
GeckoPort is a Lazarus/Free Pascal version of Takanori Ito's Gecko SDK for Delphi, including the TGeckoBrowser component.
 +
 
 +
=See also=
 +
 
 +
* [[Executing External Programs]]
 +
 
 +
[[Category:Tutorials]]

Revision as of 17:01, 11 August 2012

Deutsch (de) English (en) español (es) русский (ru)

This page summarizes various ways to display HTML pages from a Pascal program, ranging from calling an external browser to adding a component capable of rendering the html page into a form.

Starting a web browser to show a page/URL

OpenURL

OpenURL is simplest way of opening URLs in general. This procedure will detect default web browser and open URL from parameter in it. It can be used for mail address of the form 'mailto:aname@AnAddress?subject=:::A subject line', web addresses 'http://' and files 'file:///'.

uses LCLIntf;
...
OpenURL('http://www.lazarus.freepascal.org');

Finding the default webbrowser

Every platform has a mechanism for its default platform. The LCL unit lazhelphtml contains a class THTMLBrowserHelpViewer to start a web browser for the LCL help system. You can use its method FindDefaultBrowser to find the default browser and the parameter to start. For example:

uses 
  Classes, ..., LCLProc, LazHelpHTML;

...

implementation

procedure TMainForm.Button1Click(Sender: TObject);
var
  v: THTMLBrowserHelpViewer;
  BrowserPath, BrowserParams: string;
begin
  v:=THTMLBrowserHelpViewer.Create(nil);
  v.FindDefaultBrowser(BrowserPath,BrowserParams);
  debugln(['Path=',BrowserPath,' Params=',BrowserParams]);
  v.Free;
end;

This gives for example under Ubuntu (Linux):

Browser=/usr/bin/xdg-open Params=%s

Under Windows you can get:

Browser=C:\windows\system32\rundll32.exe Params=url.dll,FileProtocolHandler %s

Starting the browser

Once you know the command line and parameters you can use TProcessUTF8 to start the browser:

uses 
  Classes, ..., LCLProc, LazHelpHTML, UTF8Process;

...

implementation

procedure TMainForm.Button1Click(Sender: TObject);
var
  v: THTMLBrowserHelpViewer;
  BrowserPath, BrowserParams: string;
  p: LongInt;
  URL: String;
  BrowserProcess: TProcessUTF8;
begin
  v:=THTMLBrowserHelpViewer.Create(nil);
  try
    v.FindDefaultBrowser(BrowserPath,BrowserParams);
    debugln(['Path=',BrowserPath,' Params=',BrowserParams]);

    URL:='http://www.lazarus.freepascal.org';
    p:=System.Pos('%s', BrowserParams);
    System.Delete(BrowserParams,p,2);
    System.Insert(URL,BrowserParams,p);

    // start browser
    BrowserProcess:=TProcessUTF8.Create(nil);
    try
      BrowserProcess.CommandLine:=BrowserPath+' '+BrowserParams;
      BrowserProcess.Execute;
    finally
      BrowserProcess.Free;
    end;
  finally
    v.Free;
  end;
end;

Integrate web browser component in application

Using the Turbopower internet pro control

Lazarus provides the package TurboPowerIPro (lazarus/components/turbopower_ipro/turbopoweripro.lpk) with the following features:

  • It contains a control to put onto a form. When you install the package in the IDE, you get some new components in the palette, so you can drop them onto a form just like any LCL control.
  • It is written completely in pascal and therefore works on all platforms out of the box without any extra installation.
  • You have the full control, what files/urls are opened.
  • It does not have all the features of a full webbrowser. No multimedia stuff, javascript or flash. This must be implemented by you.

There are some examples in lazarus/components/turbopower_ipro/examples demonstrating how to open a local html file with some images, hyperlinks and history buttons.

QT Webkit

When using the Qt WidgetSet, you can use the Qt WebKit to embed a browser into a LCL form.

Lazarus LCL/Qt WebKit Demo available on FPC Qt4 Binding Web Page

THtmlPort

THtmlPort is a Lazarus/Free Pascal version of Dave Baldwin's HTML Components, including THtmlViewer, TFrameViewer and TFrameBrowser.

GeckoPort

GeckoPort is a Lazarus/Free Pascal version of Takanori Ito's Gecko SDK for Delphi, including the TGeckoBrowser component.

See also