Welcome!

PowerBuilder Authors: Dan Joe Barry, Ian Thain, Yakov Werde, Paul Slater, Bruce Armstrong

Related Topics: PowerBuilder

PowerBuilder: Article

Writing Your Own Web Browser

Writing Your Own Web Browser

Imagine creating your own Web browser that can provide the full power of Internet Explorer (IE) including scripting, parsing and rendering of HTML pages. It has become standard for stand-alone and client/server applications to contain Web browsers. A perfect example is Intuit's popular Quicken program. Quicken has the normal look and feel of a stand-alone application, but the startup sheet is a Web browser that presents up-to-the-minute Intuit information to the user. You too can build this functionality into your PowerBuilder application. In this article I'll describe how to use Microsoft's WebBrowser ActiveX control to build your own Web browser application in PowerBuilder.

About Microsoft's WebBrowser ActiveX Control
The ActiveX control, necessary files and registry settings are all included in the installation of Internet Explorer. The entire functionality of the control is in the file SHDOCVW.DLL, which is usually located in the system32 directory. This control lets you build applications that allow users to browse sites on the intranet/Internet, as well as folders in the local file system and on a network; however, there are restrictions on navigation. Like the full version of IE, it maintains a history list and allows the user to browse forward and backward through previously browsed documents. Applications can use this control as a document container to host documents, such as Excel spreadsheets or Word documents, that can be opened and edited within this control. It can also be a container to host any other ActiveX control. In addition, this control allows access to the DHTML document object model and extends it. Imagine creating a Web application without a Web server!

Note: To distribute your application with this control to users who don't have IE installed, you need to obtain a license to distribute IE from Microsoft.

Getting Started
First, make sure you have IE. If not, you can install it (the latest version is 5.01) by downloading it from Microsoft's Web site, www.microsoft.com/windows/ie.The WebBrowser control should be ready for use after installation. To use the ActiveX control you'll need the 32-bit version of PB for development. I developed my sample Web browser application using PB version 7.0 and Internet Explorer version 5.0

Creating the Browser Visual User Object
First, create a PowerBuilder application with the Application Painter. Then create a custom visual user object, add an OLE control and select the Microsoft WebBrowser control from the Insert Control tab as shown in Figure 1.

All the events, properties and methods of this control are well documented at the Web site, http://msdn.microsoft.com/workshop/browser. In the constructor event add the following line of code that will open up the home page by default when the browser is visible.

ole_1.object.GoHome()
The GoHome method of the browser control takes you to the home page, which depends on the registry key value of HKEY_CURRENT_USER/Software/ Microsoft/Internet Explorer/Main/Start Page. I had set it to "about:blank", so it would initially show me a blank page on the browser. If you want the home page to be the PBDJ Web site, set the registry key value to www.sys-con.com/pbdj. To see this effect, save the user object (u_browser) and close its painter. Create a window object (w_browser), place the user object in it and save. Open the browser window by adding appropriate code in the application's open event and run the application. Voilà! The browser is now ready for surfing the PBDJ Web site.

Navigating with the Browser Control
Let's now add browsing functionality by adding buttons, statictexts and a drop-down listbox to the user object (u_browser) as shown in Figure 2. In the modified event of the drop-down listbox add the following lines of code to navigate to a URL.

ole_1.Object.Navigate2(Text)
IF ddlb_1.FindItem(text, 1) < 0 THEN
ddlb_1.AddItem(Text)
END IF
The Navigate2 method of the browser control actually does the trick. The other lines above add the sites to the drop-down list as in IE. Add the following line of code in the clicked event of Back button.
ole_1.object.GoBack()
Add the following lines of code in the clicked event of Forward button.
ole_1.object.GoForward()

The GoBack and GoForward methods are used to browse back and forth through the sites maintained in the browser's history list during each browser session. The status of the browser control can be obtained using the Busy property, which indicates whether the control is engaged in a navigation or downloading operation. Using this property, add the following lines of code to the clicked event of the Stop button.

IF ole_1.Object.Busy THEN
ole_1.object.Stop()
END IF
You can set the Cancel property to true for the Stop button so that the clicked event gets fired when you press the escape (Esc) key, as in IE. Just as the Stop method of the browser control stops a Web page from loading, we have a Refresh method to refresh the contents of the current page. Add the following lines of code to the clicked event of the Refresh button.
IF NOT ole_1.object.busy THEN
ole_1.object.Refresh()
END IF
Call the constructor event of the browser user object from the clicked event of the Home button to navigate directly to the home page, as shown below.
Parent.Event Trigger Constructor()
To utilize the search engines, use the GoSearch method and add the following line of code in the clicked event of the Search button.
ole_1.object.GoSearch()
The GoSearch method, which is similar to the GoHome method, depends on the registry key value of HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer/Main/Search Page. I had set it to www.yahoo.com, so it would show me the home page of Yahoo! MSN is the search page by default when you install this browser control.

Useful Events of the Browser Control
The browser control triggers a number of different events to notify an application of user- and browser-generated activity. When the browser is about to navigate to a new location, it triggers a BeforeNavigate2 event. This event also includes a flag that can be set to cancel the pending navigation request. I added the following lines of code in this event to prevent the user from browsing a network-mapped drive U or a site that contains the word "sex" in the URL.

IF Pos(Lower(ddlb_1.text), "u:") > 0 OR &
Pos(Lower(ddlb_1.text), "sex") > 0 THEN
MessageBox("Navigation Restriction."', "Cannot Access this drive.")
cancel = true
END IF

This event is one of the most powerful events for canceling the navigation request to the URL for unauthorized Web sites or local and network folders. The NavigateComplete2 event is fired after the browser has navigated to a new location. Add the following line of code in the NavigateComplete2 event so that the drop-down listbox reflects the correct URL currently being navigated.

ddlb_1.text = ole_1.object.LocationURL
Caution, BeforeNavigate2 and NavigateComplete2 events don't work in PB 6.x but they work in PB 7.0. Add the following line of code in the StatusTextChange event of the browser control to show the exact activity of the browser control.
st_2.text = text
This is the event used by IE to show the current downloading status. Add the following lines of code in the TitleChange event of the browser control to set the title of the window in which this user object is placed.
window lw_browser
lw_browser = this.GetParent().GetParent()
lw_browser.title = text
The text, if present within the <TITLE> tag of the HTML page, is actually displayed on the titlebar of the window. There's a trick to disabling and enabling the Back and Forward command buttons. The CommandStateChange event of the browser control is fired when the Forward or Back buttons need to be enabled or disabled, so add the following lines of code in this event.
CHOOSE CASE command
CASE 1 //CSC_NAVIGATEFORWARD
cb_2.enabled = enable
CASE 2 //CSC_NAVIGATEBACK
cb_1.enabled = enable
END CHOOSE
The two arguments - command and enable - of the above event are used to enable or disable the appropriate Back and Forward command buttons in the browser user object. The browser control supports several common file operations, such as Print, Page Setup, Save and Properties, with the ExecWB method. I'll show you how to add the save HTML document functionality using the ExecWB method. Add a command button called Save As (see Figure 2) and add the following lines of code in the clicked event.
ole_1.object.ExecWB( 4, 1)

Value 4 for the first argument and value 1 for the second argument bring up the Save HTML document dialog box. Similarly, value 6 would bring up the Print dialog box and value 8 would bring up the Page Setup dialog box. To print the Web page, you can use the Print menu item from the popup menu that appears when the user right-clicks on the browser control at runtime. It's good practice to trap the ExternalException event for this control as it returns an OLE exception whenever anything fails, such as trying to navigate backward or forward further than the existing pages or selecting the cancel button in a Save As dialog box. For this purpose, add the following lines of code in this event for a simple error handler to prevent the application from halting because of an Application System Error.

MessageBox("OLE External Exception", source) action = ExceptionIgnore!
Running the Customized Browser Application
Save the user object with all the above lines of code and close the user object painter. Run the application. The customized browser application is now ready with most of the features of IE.

Conclusion
With the quantity of intranet/Internet applications mushrooming, this ActiveX control can be a key element to Web-enabling your applications. Coupled with the new Web DataWindow features, this object can add a lot of power to business applications. This control allows us to take advantage of the rich GUI that can be seen in Web pages. Another advantage of this control is to track and verify user navigation, thus securing your Web-enabled application. By reusing this control on the client side with DHTML forms and embedded ActiveX controls delivered from a Web server, the client's application or a remote machine on the network, difficult application development and deployment problems can be solved.

You can download this article and the source code developed using PowerBuilder version 7.0 from the URL www.intac.com/~kaudata/download

More Stories By Kaushik Datta

Kaushik Datta is a senior technical consultant with Patel Consultants Corporation in New Jersey. He has over eight years of experience in systems and application software development using tools like PowerBuilder, Visual Basic and JDK.

He can be reached at: kaudata@castle.net

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.