Class

DesktopHTMLViewer


Description

Renders HTML and provides basic navigation features.

Methods

Name

Parameters

Returns

Shared

AcceptFileDrop

FileType As String

AcceptPictureDrop

AcceptRawDataDrop

Type As String

AcceptTextDrop

Cancel

Close

DrawInto

g As Graphics, x As Integer, y As Integer

ExecuteJavaScript

js As String

ExecuteJavaScriptSync

js As String

Variant

GoBack

GoForward

LoadPage

Source As String, RelativeTo As FolderItem

File As FolderItem

LoadURL

URL As String

Print

showPrintDialog As Boolean = True

Refresh

immediately As Boolean = False

SetFocus

ZoomTextIn

ZoomTextOut

Property descriptions


DesktopHTMLViewer.Active

Active As Boolean

Indicates whether the control is active.

This property is read-only.

Active is False when the control's window is not in the foreground. When a DesktopWindow is deactivated, its controls are automatically deactivated unless AllowAutoDeactivate is set to False.


DesktopHTMLViewer.AllowAutoDeactivate

AllowAutoDeactivate As Boolean

Determines whether the control should be deactivated (on macOS) when the parent window is deactivated. The default is True.


DesktopHTMLViewer.AllowTabStop

AllowTabStop As Boolean

If True, the control is in the Tab Order and accepts the focus when the user tabs into it. The default is True. If False, the user cannot tab into it to give it the focus. However, the control can gain the focus by other means, such as the user's clicking on it or by setting the focus in code.

This example removes the control from the Tab Order:

Me.AllowTabStop = False

DesktopHTMLViewer.CanGoBack

CanGoBack As Boolean

If True, it indicates that there is a prior page that can be navigated to using GoBack.

This property is read-only.

This code in the DocumentComplete event handler disables a Back button if there is no prior page:

BackButton.Enabled = Me.CanGoBack

DesktopHTMLViewer.CanGoForward

CanGoForward As Boolean

If True, it indicates that there is a next page that can be navigated to using GoForward.

This property is read-only.

This code in the DocumentComplete event handler disables a Forward button if there is no next page:

ForwardButton.Enabled = Me.CanGoForward

DesktopHTMLViewer.Enabled

Enabled As Boolean

Determines if the control should be enabled when the owning window is opened.

A disabled control cannot be clicked and cannot receive the focus.

This example disables the control.

Me.Enabled = False

DesktopHTMLViewer.Handle

Handle As Ptr

Returns a handle to the control.

This property is read-only.

For interfacing with Mac APIs using Declares, DesktopControl.Handle returns NSViews (except for DesktopToolbar).

On Windows returns the HWND of the control.

On Linux it returns a GtkWidget.

The following gets a handle to the control.

Var p As Ptr = Me.Handle

DesktopHTMLViewer.Height

Height As Integer

The height (in points) of the control.

This example sets the height of the control to 100:

Me.Height = 100

DesktopHTMLViewer.Index

Index As Integer

If the control is used in a control set, this specifies the control's index in the set.

This property is read-only.


DesktopHTMLViewer.IsAvailable

IsAvailable As Boolean

If True, it indicates that the required support libraries required by the control were loaded.

This property is read-only.

On Windows, HTMLViewer uses Internet Explorer or WebKit (depending on the DesktopHTMLViewer property), on macOS and Linux, it uses WebKit.

This example in the Open event handler calls the IsAvailable property prior to proceeding:

If Me.IsAvailable Then
  Me.LoadURL("http://www.wikipedia.org")
End If

DesktopHTMLViewer.Left

Left As Integer

The distance from the left side of the control to the left side of its containing window or container.

The following example moves the control 100 points from the left side of the window:

Me.Left = 150

DesktopHTMLViewer.LockBottom

LockBottom As Boolean

Determines whether the bottom edge of the control should stay at a set distance from the bottom edge of the parent control, if there is one, or the owning window.

This property can be set in the control's Inspector. The following example sets it in code.

Me.LockBottom = True

DesktopHTMLViewer.LockLeft

LockLeft As Boolean

Determines whether the left edge of the control should stay at a set distance from the left edge of the parent control, if there is one, or the owning window.

LockLeft and Locktop default to True when you add a new control to a window. Existing controls will be altered only if LockRight and/or LockBottom are not set. LockLeft has no effect unless LockRight is True.

This property can be set in the control's Inspector. The following example sets it in code.

Me.LockLeft = True

DesktopHTMLViewer.LockRight

LockRight As Boolean

Determines whether the right edge of the control should stay at a set distance from the right edge of the parent control, if there is one, or the owning window.

This property can be set in the control's Inspector. The following example sets it in code.

Me.LockRight = True

DesktopHTMLViewer.LockTop

LockTop As Boolean

Determines whether the top edge of the control should stay at a set distance from the top edge of the parent control, if there is one, or the owning window.

LockTop and LockLeft default to True when you add a control to a window. Existing controls will be altered only if LockRight and/or LockBottom are not set. LockTop has no effect unless LockBottom is True.

This property can be set in the control's Inspector. The following example sets it in code.

Me.LockTop = True

DesktopHTMLViewer.MouseCursor

MouseCursor As MouseCursor

The cursor to be displayed while the mouse is within the control and both the DesktopApplication and DesktopWindow class's MouseCursor properties are Nil.

If the DesktopApplication class's MouseCursor property is not Nil or the DesktopWindow's MouseCursor property is not Nil, then any control's MouseCursor property is ignored. You can use a cursor stored in the Cursors module. On Macintosh, you can also obtain a MouseCursor from a resource file.

This line in the Opening event of the control sets the default cursor to the finger pointer.

Me.MouseCursor = System.Cursors.FingerPointer

DesktopHTMLViewer.Name

Name As String

The name of the control. Set the name of the control in the Inspector.

This property is read-only.


DesktopHTMLViewer.PanelIndex

PanelIndex As Integer

If the control has been placed on a DesktopTabPanel or DesktopPagePanel control, this is the panel (page/tab) that the control is on. If the control is not on a panel, it returns -1.

The first panel is numbered zero. If the control has been placed on a panel of a DesktopTabPanel or DesktopPagePanel control, it returns the panel number. If the control is not on a DesktopPagePanel or DesktopTabPanel, it returns -1. If you change the PanelIndex to a nonexistent panel, the control will disappear until you give it a PanelIndex value that corresponds to a panel that exists.

If you are looking to change the currently selected panel (page/tab), use DesktopPagePanel.

This code displays the panel index of the control that is on the page.

MessageBox(Me.SelectedPanelIndex.ToString)

DesktopHTMLViewer.Parent

Parent As Object

Used to get and set the control's parent control or window.

If the control is on the window, the Parent will be the Window. If the control is on the container, the Parent will be the container. If the control is completed enclosed by another control, the Parent will be that control.

If you do not want the enclosing control to be the parent, set the Parent property of that control to Nil to make it the Window.

If the parent control is somehow in another window, an InvalidParentException will occur.

The following example sets the parent of the control to the window.

Me.Parent = Nil

DesktopHTMLViewer.Scope

Scope As Integer

Used to determine whether access to the control is Public (0) or Private (2). The default is Public.

This property is read-only.

Note

This is a designtime-only property and thus can only be set in the Inspector and is not accessible via code.

If the Scope of a control is set to Private, it cannot be accessed from outside its parent window.


DesktopHTMLViewer.TabIndex

TabIndex As Integer

The control's position in the Tab Order. The control with whose TabIndex is 0 will get the focus first.

On macOS, only controls where you enter data from the keyboard typically get the focus. In order to manually set focus to controls that don't allow keyboard entry, go to System Preferences, click on the Keyboard icon then on the Shortcuts tab and then check the Use keyboard navigation to move focus between controls checkbox.

This example sets the control's TabIndex.

Me.TabIndex = 2

DesktopHTMLViewer.Tooltip

Tooltip As String

Text of help message displayed as a Windows or Linux "tooltip" or macOS help tag.

The tip/tag is displayed when the user hovers the mouse cursor over the control.

This example adds a tooltip to a control:

Me.Tooltip = "Click to bark."

DesktopHTMLViewer.Top

Top As Integer

The distance from the top of the control to the top of its containing window or container.

This example sets the top of the control to 140 points from the top of the window:

Me.Top = 140

DesktopHTMLViewer.Transparent

Transparent As Boolean

Determines whether the control is transparent on Windows. The default is False. Has no effect on macOS or Linux.

Transparent controls draw more slowly and use more memory in order to cache the background. Unless you absolutely require transparency, leave this set to False.

For projects that were created prior to 2018r1, this property defaults to True to emulate previous behavior. Consider changing it to False to improve performance if you do not require transparency.


DesktopHTMLViewer.UserAgent

UserAgent As String

Supports getting and setting a custom user agent string.

UserAgent works on macOS and Windows (using the Native Renderer). Changing it has no effect on Linux or when on Windows using the WebKit DesktopHTMLViewer.

This example sets the UserAgent.

If Me.IsAvailable Then
  Var agent As String
  agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16"
  Me.UserAgent = agent
End If

DesktopHTMLViewer.UseSandbox

UseSandbox As String

If True, the JavaScript (if any) embedded in the page being viewed in the HTMLViewer will be unable to access the HTMLViewer's parent webpage.


DesktopHTMLViewer.Visible

Visible As Boolean

Determines whether the control is visible when its owning window is opened. The default is True: the control is visible.

The following code makes the control invisible:

Me.Visible = False

DesktopHTMLViewer.Width

Width As Integer

The width (in points) of the control.

The following example resizes the control:

Me.Width = 200

DesktopHTMLViewer.Window

Window As DesktopWindow

The control's parent window.

This property is read-only.

This code gets the parent window's Title property.

MessageBox(Me.Window.Title)

Method descriptions


DesktopHTMLViewer.AcceptFileDrop

AcceptFileDrop(FileType As String)

Permits documents of type FileType to be dropped on the control. FileType must be a file type that you defined in via the FileType class or the File Type Sets Editor.

This code in the Opening event makes it possible for the user to drop either a picture or a file that is a jpeg image. The File Type Sets editor was used to define the “image/jpeg” file type. It is one of the “Common File Types” that is available in the editor.

Me.AcceptPictureDrop
Me.AcceptFileDrop("image/jpeg")

To restrict file drops to just folders (and not files), you can put this code in the DragEnter event:

If Not obj.FolderItem.IsFolder Then Return True

DesktopHTMLViewer.AcceptPictureDrop

AcceptPictureDrop

Permits pictures to be dropped on the control.

If a control should accept pictures in a drag and drop, then AcceptPictureDrop needs to be called prior to the drop. Typically, it is in the Opening event of the control itself. For example, the line:

Me.AcceptPictureDrop

in the Opening event of the control that will receive the dragged pictures is needed. When the picture is dropped, the DropObject event is called and this is where you will put your code to handle the drop.

Opening Event:

Me.AcceptPictureDrop

DropObject Event:

If obj.PictureAvailable Then
  Me.Backdrop = obj.Picture
End If

DesktopHTMLViewer.AcceptRawDataDrop

AcceptRawDataDrop(Type As String)

Permits data (of the Type specified) to be dropped on the control.

The following specfies a generic file type defined in the File Type Sets editor.

Me.AcceptRawDataDrop("????")

DesktopHTMLViewer.AcceptTextDrop

AcceptTextDrop

Permits text to be dropped on the control.

This line in the Opening event of a control that can accept dragged text.

Me.AcceptTextDrop

DesktopHTMLViewer.Cancel

Cancel

Cancels any current operations.

Me.Cancel

DesktopHTMLViewer.Close

Close

Closes a control.

Closing a control permanently removes the control from memory, making it impossible to access. You can close both non-indexed controls and indexed controls. When you close an indexed control, the indexes for the remaining controls will shift downward so that the indexes start with zero and are consecutive.

The following code closes the control. When this is executed from a visible control, the control disappears from the window.

Me.Close

DesktopHTMLViewer.DrawInto

DrawInto(g As Graphics, x As Integer, y As Integer)

Draws the contents of the control into the specified Graphics context. The parameters x and y are the coordinates of the top, left corner.

Warning

Although the DesktopHTMLViewer has this method, it's not supported.


DesktopHTMLViewer.ExecuteJavaScript

ExecuteJavaScript(js As String)

Asynchronously executes the passed JavaScript in the context of the currently loaded page.

Attempting to execute JavaScript before the page finishes loading (or if no page is loaded) is undefined and is unlikely to work.

In order to call JavaScript on Windows using the Native renderer, a document must already be loaded into the HTMLViewer. If no document is loaded, an HTMLViewerException is raised.

This code will populate the search field on the wikipedia page (assuming it was previously loaded):

Var jsSrc As String
jsSrc = "document.forms[0].elements['search'].value=""xojo"";"
HTMLViewer1.ExecuteJavaScript(jsSrc)

This code writes text to the HTML document:

Var this As String
this = "document.writeln(""hello"");"
HTMLViewer1.ExecuteJavaScript(this)

DesktopHTMLViewer.ExecuteJavaScriptSync

ExecuteJavaScriptSync(js As String) As Variant

Synchronously executes the passed JavaScript in the context of the currently loaded page and returns the result. Only strings and numbers may be returned.

Attempting to execute JavaScript before the page finishes loading (or if no page is loaded) is undefined and is unlikely to work.

In order to call JavaScript on Windows using the Native renderer, a document must already be loaded into the HTMLViewer. If no document is loaded, an HTMLViewerException is raised.


DesktopHTMLViewer.GoBack

GoBack

Navigates to the prior page.

On a back button Pressed event handler, you can write:

HTMLViewer1.GoBack

DesktopHTMLViewer.GoForward

GoForward

Navigates to the next page.

On a forward button Pressed event handler, you can write:

HTMLViewer1.GoForward

DesktopHTMLViewer.LoadPage

LoadPage(Source As String, RelativeTo As FolderItem)

Loads the the supplied File into the HTMLViewer. File must be an actual file and not a folder.


DesktopHTMLViewer.LoadPage

LoadPage(File As FolderItem)

Loads the the supplied File into the HTMLViewer. File must be an actual file and not a folder.

Any links will be resolved relative to the passed RelativeTo FolderItem.

Warning

This method signature is not supported on Windows due to CEF Security.

In this example, the raw HTML is in a TextArea:

Var f As FolderItem = FolderItem.TemporaryFile
HTMLViewer1.LoadPage(TextArea1.Text, f)

This example loads an HTML file from disk. The file type has been added using the File Types Set Editor:

Var f As FolderItem

f = FolderItem.ShowOpenFileDialog("text/html")
If f <> Nil Then
  HTMLViewer1.LoadPage(f)
End If

DesktopHTMLViewer.LoadURL

LoadURL(URL As String)

Loads the passed HTTP URL. Any protocol other than HTTP may or may not work as that is dependent on operating system behavior.

On macOS, you need to ensure the URL is prefixed with "http://" or "https://" in order for it to load.

This example loads a URL entered by the user in a DesktopTextField:

Var url As String = URLField.Text
If url.Left(7) <> "http://" Then url = "http://" + url
HtmlViewer1.LoadURL(url)

DesktopHTMLViewer.Print

Print(showPrintDialog As Boolean = True)

Prints the contents of the HTMLViewer. On Linux, this requires Webkit 1.1.5 or newer.

This example prints the current page.

HTMLViewer1.Print

DesktopHTMLViewer.Refresh

Refresh(immediately As Boolean = False)

Redraws the portion specified of the contents of the control the next time the OS redraws the control or immediately if True is passed.

If you are calling this so frequently that you experience a slowdown in your code, pass True for the immediately parameter.

Calling this method causes the Render event to fire.

Refresh the entire area immediately:

Me.Refresh(True)

Refresh a portion of the area the next time the OS redraws the control:

Me.Refresh(100, 150, 200, 300)

DesktopHTMLViewer.SetFocus

SetFocus

If applicable, sets the focus to the DesktopUIControl. KeyDown events are directed to the control.

If the control cannot get the focus on the platform on which the application is running, SetFocus does nothing. The SetFocus method of the DesktopWindow class or the ClearFocus method can be used to remove the focus from the control that currently has the focus, leaving no control with the focus.

Note

On the Mac you need Full Keyboard Access turned on in System Preferences (Keyboard->Shortcuts) in order to manually set focus to non-text controls.

The following example in the Opening event sets the focus to the that control. If another control has the focus when this line is executed, then the user sees this control gets the focus.

Me.SetFocus

DesktopHTMLViewer.ZoomTextIn

ZoomTextIn

Augments the size of the text used to display the HTML page content. You can use ZoomTextIn several times.

The reverse action is achieved with the ZoomTextOut method.

This example is the Pressed event of a DesktopSegmentedButton:

If segmentIndex = 0 Then
  HtmlViewer1.ZoomTextIn
ElseIf segmentIndex = 1 Then
  HtmlViewer1.ZoomTextOut
End If

DesktopHTMLViewer.ZoomTextOut

ZoomTextOut

Reduces the size of the text used to display the HTML page content. You can use ZoomTextOut several times.

The reverse action is achieved with the ZoomTextIn method.

This example is the Pressed event of a DesktopSegmentedButton:

If itemIndex = 0 Then
  HtmlViewer1.ZoomTextIn
ElseIf itemIndex = 1 Then
  HtmlViewer1.ZoomTextOut
End If

Event descriptions


DesktopHTMLViewer.CancelLoad

CancelLoad(URL As String) As Boolean

Returns a Boolean. Return True to cancel loading the page.


DesktopHTMLViewer.Closing

Closing

The control is closing.


DesktopHTMLViewer.ConstructContextualMenu

ConstructContextualMenu(base As DesktopMenuItem, x As Integer, y As Integer) As Boolean

This event is called when it is appropriate to display a contextual menu for the control.

This event handler is the recommended way to handle contextual menus because this event figures out whether the user has requested the contextual menu, regardless of how they did it. Depending on platform, it might be in the MouseUp or MouseDown event and it might be a right+click or by pressing the contextual menu key on the keyboard, for example.

Base is analogous to the menu bar for the contextual menu. Any items you add to Base will be shown as menu items. If you return False, the event is passed up the parent hierarchy.

If you return True, the contextual menu is displayed. The parameters x and y are the mouse locations. If the event was fired because of a non-mouse event, then x and y are both set to -1. See the example of a contextual menu in the following section.

The following ConstructContextualMenu event handler builds a menu with three menu items plus a submenu with three additional menu items.

// Add some items
base.AddMenu(New DesktopMenuItem("Test 1"))
base.AddMenu(New DesktopMenuItem("Test 2"))
base.AddMenu(New DesktopMenuItem("Test 3"))

// Add a Separator
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))

// Add a sub menu
Var submenu As New DesktopMenuItem("SubMenu")
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 1"))
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 2"))
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 3"))
base.AddMenu(submenu)

// Add a Separator
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))

Return True

DesktopHTMLViewer.ContextualMenuItemSelected

ContextualMenuItemSelected(hitItem As DesktopMenuItem) As Boolean

Fires when a contextual menuitem selectedItem was selected but the MenuItemSelected event and the MenuHandler for the DesktopMenuItem did not handle the menu selection.

This event gives you a chance to handle the menu selection by inspecting the menuitem's Text or Tag properties to see which item was selected. Use this in conjunction with ConstructContextualMenu if you have not specified the MenuItemSelected event or the Menu Handler for the items on the contextual menu. See the example of a contextual menu in the examples for the DesktopUIControl class.

Return True if this event has handled the item the user chose from the contextual menu. Returning False will cause the control's parent to execute its ContextualMenuAction event. This can be handy if you have the same contextual menu for several controls who share the same Parent (several on the same window for example). By returning False you can handle them all in a single event.

This simple event handler displays the value of the selected menu item.

If selectedItem <> Nil Then MessageBox(selectedItem.Text)
Return True

DesktopHTMLViewer.DocumentBegin

DocumentBegin(URL As String)

Fires when the HTML page is starting to load.


DesktopHTMLViewer.DocumentComplete

DocumentComplete(URL As String)

Called when the HTML page is finished loading.


DesktopHTMLViewer.DocumentProgressChanged

DocumentProgressChanged(URL As String, percentageComplete As Integer)

Fires when the progress has been updated. If percentageComplete is -1, the percentage cannot be determined.


DesktopHTMLViewer.Error

Error(errorNumber As Integer, errorMessage As String)

Fires when an error occurs.

The errors you receive are the standard HTTP Status Codes.


DesktopHTMLViewer.FocusLost

FocusLost

The control has lost the focus.

This property must be set to True for the control to be capable of getting then losing the focus.

Note

On macOS, controls other than text fields and lists will accept and lose focus only if the full keyboard access option is enabled in System Preferences/Keyboard.


DesktopHTMLViewer.FocusReceived

FocusReceived

The control has received the focus and has a selection rectangle around it.

This property must be set to True for the control to be capable of getting the focus.

Note

On macOS, controls other than text fields and lists will accept focus only if the full keyboard access option is enabled in System Preferences/Keyboard.


DesktopHTMLViewer.JavaScriptRequest

JavaScriptRequest(method As String, parameters() As Variant) As String

Called when a JavaScript executed via ExecuteJavaScript or ExecuteJavaScriptSync calls ExecuteInXojo or ExecuteInXojoSync.

A JavaScript executed via ExecuteJavaScript or ExecuteJavaScriptSync can call executeInXojo() or executeInXojoSync() to pass data back to the HTMLViewer via this event. Both can be passed any number of parameters though the first parameter must be a string. For all subsequent parameters, only strings and numbers are supported. The executeInXojo() method is asynchronous and thus will not receive a value returned by this event. The executeInXojoSync() method is synchronous and thus should be called as a function as it will receive the value returned by this event.


DesktopHTMLViewer.KeyDown

KeyDown(key As String) As Boolean

The user has pressed the Key passed while the control has the focus.

Returning True prevents the KeyDown event on the parent control (usually the window) from executing. Returning False results in the execution of the KeyDown event of the parent control.


DesktopHTMLViewer.KeyUp

KeyUp(key As String)

Fires when the passed Key is released in the control that has the focus.

It is not guaranteed to be the same key that received the KeyDown event.


DesktopHTMLViewer.MouseDown

MouseDown(x As Integer, y As Integer) As Boolean

The mouse button was pressed inside the control's region at the location passed in to x, y.

This event fires repeatedly while the mouse button is being held down.

The coordinates x and y are local to the control, i.e. they represent the position of the mouse click relative to the upper-left corner or the control.

Return True if you are going to handle the MouseDown. In such a case:

  • The Pressed event, if any, will not execute and the state of the object will not change.

  • You will receive the MouseUp event.

If you return False, the system handles the MouseDown so the MouseUp event handler do not get called.

The MouseDown event uses the DragItem constructor when the user drags the contents of the control. It is:

Var d As DragItem
d = New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
d.Picture = Me.Image
d.Drag // Allow the drag

DesktopHTMLViewer.MouseDrag

MouseDrag(x As Integer, y As Integer)

The mouse button was pressed inside the control and moved (dragged) at the location local to the control passed in to x, y.

This event fires repeatedly while the mouse button is down regardless of whether or not the mouse coordinates are changing.

This event will not occur unless you return True in the MouseDown event.


DesktopHTMLViewer.MouseUp

MouseUp(x As Integer, y As Integer)

The mouse button was released. Use the x and y parameters to determine if the mouse button was released within the control's boundaries.

Note

This event will not occur unless you return True in the MouseDown event. The return value is ignored.

The parameters x and y are local coordinates, i.e. they represent the position of the mouse click relative to the upper-left corner or the control. Mouse clicks that are released to the left or above a control are negative.


DesktopHTMLViewer.NewWindow

NewWindow(URL As String) As HTMLViewer

Called when the HTML on page requests that a URL be opened in a new HTML viewer (a new window or tab depending how the user chooses to open it).

Returns a new instance of an HTMLViewer to load the page in that viewer or Return Nil when you do not want to allow the window to open.


DesktopHTMLViewer.Opening

Opening

The control is about to be displayed. Use this event to initialize a control.

The Opening event is called after the Constructor.

Be warned that initializing control property values using the Constructor instead of the Opening event may result in those property values being overwritten by what is set in the Inspector. For best results, use the Opening event for control initialization rather than the control Constructor.

If the control is supposed to handle drag and drop, you need to tell it which type of item it needs to be able to handle. The following example informs the control that pictures and files can be dropped on it. The type of the file it needs to support is specified via the File Types Editor.

Sub Opening()
  Me.AcceptPictureDrop
  Me.AcceptFileDrop("JPEG")
End Sub

DesktopHTMLViewer.SecurityChanged

SecurityChanged(isSecure As Boolean)

Fires when the document's security has changed. For example, entering a secure web site will cause this event to fire.

This event is only called on Microsoft Windows when using the Native renderer.


DesktopHTMLViewer.StatusChanged

StatusChanged(newStatus As String)

Fires when the Status text has changed for the window.

Due to changes made to the CEF3 library (Chromium Embedded Framework) this event is not called on Windows when Renderer = WebKit.


DesktopHTMLViewer.TitleChanged

TitleChanged(newTitle As String)

Fires when the Title of the Web page has been determined or has changed.

The renderer, which varies by OS, determines the maximum number of characters that are returned in NewTitle. You should test this in your projects to determine a maximum number of characters, keeping in mind that this could change at any time due to the underlying renderer and operating system.

Notes

On macOS, the HTMLViewer supports getting and setting a custom user agent string and increasing or decreasing the font size.


Exchanging data with javascript

A JavaScript executed via ExecuteJavaScript or ExecuteJavaScriptSync can call executeInXojo() or executeInXojoSync() to pass data back to the HTMLViewer via the JavaScriptRequest event. Both can be passed any number of parameters though the first parameter must be a string. For all subsequent parameters, only strings and numbers are supported. The executeInXojo() method is asynchronous and thus will not receive a value returned by the JavaScriptRequest event. The executeInXojoSync() method is synchronous and thus should be called as a function as it will receive the value returned by the JavaScriptRequest event.


App transport security

Starting with MacOS 10.11 and Xojo 2018r4, your apps have to use secure "https" connections or you will get this error: "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection". You can work around this by providing a plist with your app to indicate what non-secure URLs you are using. More information is available in the Using Non-Secure URLs on macOS and iOS topic.


Webkit on windows

To make plugins available to the DesktopHTMLViewer on Windows, create a folder called WebKitPlugins within your Libs folder in your application folder and place the plugins you need there.

The Handle property returns a pointer to the cef_browser_t struct.


Linux support

If it is available, the Linux HTMLViewer uses WebKit (libwebkitgtk version 3).

Refer to the System Requirements for current release section for commands that you can use to install libwebkitgtk v3 on various Linux distributions.

On Linux, HTMLViewer.Handle returns the GtkScrolledWindow that hosts the WebKitWebView control (please refer to the WebKitGTK site for information on how to use this system control).

Sample code

Var s As String = HTMLViewer1.ExecuteJavaScriptSync("document.documentElement.innerHTML;")

Create a window with a large DesktopHTMLViewer control. Set its LockLeft, LockTop, LockRight, and LockBottom properties so that it resizes as the user resizes the window. Use a DesktopTextField as the URL entry area and a DesktopButton as the browser's Go button. That is, the user clicks Go to display the URL entered into the DesktopTextField.

In this example, the HTMLViewer is named “HTML”. The following code is in the Pressed event of the DesktopButton:

HTML.LoadURL(TextField.Text)

You can use a DesktopProgressBar to indicate that the web page is loading. In the DesktopHTMLViewer's DocumentBegin event, initialize and show the DesktopProgressBar with the code:

ProgressBar1.Value = 0
ProgressBar1.Visible = True

In the DocumentProgressChanged event, increment the value of the DesktopProgressBar. This event handler is passed the value of PercentageComplete (0 to 100).

If percentageComplete = -1 Then // if it cannot be determined
  ProgressBar1.Maximum = 0 // display indeterminate progress
Else
  ProgressBar1.Maximum = 100
End if

ProgressBar1.Value = percentageComplete

In the DocumentComplete event handler, hide the ProgressBar with the line:

ProgressBar1.Visible = False

When the title of the web page has been received, you can display it in the window's Title bar using the DesktopHTMLViewer's TitleChanged event. It is passed the new title in the String parameter newTitle. Update the window title with the line:

Title = newTitle

Use a second DesktopTextField to display the status of the load process. In the HTMLViewer's StatusChanged event handler, set the value of this DesktopTextField. This event handler is passed the current status in the String parameter, NewStatus. Display this string with the following line in the StatusChanged event:

TextField1.Text = NewStatus

If a new browser window is supposed to open, you need to insert some code to handle this event. For example, the user clicks a link that is supposed to display the new page in another window. Use the NewWindow event handler to create the window. The following code assumes that the browser is contained in a window called MainWindow.

Var w As New MainWindow
Title = "New Window" //Title property of new window
w.Show
Return w.HTML

Compatibility

Desktop projects on all supported operating systems.

See also

DesktopUIControl parent class; URLConnection class