''' <summary>
''' A <b>TabPage</b> that displays a <b>WebBrowser</b> control.
''' </summary>
''' <remarks></remarks>
Public Class WebBrowserTabPage
    Inherits System.Windows.Forms.TabPage

#Region " Constants "

    ''' <summary>
    ''' The title to display on the tab when no page is loaded.
    ''' </summary>
    Private Const EMPTY_PAGE_TITLE As String = "(Empty)"

#End Region 'Constants

#Region " Variables "

    ''' <summary>
    ''' The <b>WebBrowser</b> control displayed on the page.
    ''' </summary>
    Private WithEvents _browser As WebBrowser

#End Region 'Variables

#Region " Properties "

    ''' <summary>
    ''' Gets the <b>WebBrowser</b> control displayed on the page.
    ''' </summary>
    ''' <value>
    ''' A <b>WebBrowser</b> object.
    ''' </value>
    Public ReadOnly Property Browser() As WebBrowser
        Get
            Return Me._browser
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether a previous page in navigation history is available, which allows the <see cref="GoBack" /> method to succeed.
    ''' </summary>
    ''' <value>
    ''' <b>True</b> if the control can navigate backward; otherwise, <b>False</b>.
    ''' </value>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.CanGoBack</b> property.
    ''' </remarks>
    Public ReadOnly Property CanGoBack() As Boolean
        Get
            Return Me.Browser.CanGoBack
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether a subsequent page in navigation history is available, which allows the <see cref="GoForward" /> method to succeed.
    ''' </summary>
    ''' <value>
    ''' <b>True</b> if the control can navigate forward; otherwise, <b>False</b>.
    ''' </value>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.CanGoForward</b> property.
    ''' </remarks>
    Public ReadOnly Property CanGoForward() As Boolean
        Get
            Return Me.Browser.CanGoForward
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether the internal <b>WebBrowser</b> control is currently loading a new document.
    ''' </summary>
    ''' <value>
    ''' <b>True</b> if the control is busy loading a document; otherwise, <b>False</b>.
    ''' </value>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.IsBusy</b> property.
    ''' </remarks>
    Public ReadOnly Property IsBusy() As Boolean
        Get
            Return Me.Browser.IsBusy
        End Get
    End Property

    ''' <summary>
    ''' Gets the status text of the internal <b>WebBrowser</b> control.
    ''' </summary>
    ''' <value>
    ''' The status text.
    ''' </value>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.StatusText</b> property.
    ''' </remarks>
    Public Overridable ReadOnly Property StatusText() As String
        Get
            Return Me.Browser.StatusText
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets the URL of the current document.
    ''' </summary>
    ''' <value>
    ''' A <b>Uri</b> representing the URL of the current document.
    ''' </value>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Url</b> property.
    ''' </remarks>
    Public Property Url() As Uri
        Get
            Return Me.Browser.Url
        End Get
        Set(ByVal value As Uri)
            Me.Browser.Url = value
        End Set
    End Property

#End Region 'Properties

#Region " Constructors "

    ''' <summary>
    ''' Creates a new instance of the <see cref="WebBrowserTabPage" /> class.
    ''' </summary>
    ''' <remarks>
    ''' This constructor creates and displays a new <b>WebBrowser</b> control.
    ''' </remarks>
    Public Sub New()
        Me.New(New WebBrowser)
    End Sub

    ''' <summary>
    ''' Creates a new instance of the <see cref="WebBrowserTabPage" /> class and displays the specified <b>WebBrowser</b> control.
    ''' </summary>
    ''' <param name="browser">
    ''' The <b>WebBrowser</b> control to display on the page.
    ''' </param>
    Public Sub New(ByVal browser As WebBrowser)
        browser.Dock = DockStyle.Fill
        Me.Controls.Add(browser)
        Me._browser = browser

        If browser.DocumentTitle = String.Empty Then
            Me.Text = EMPTY_PAGE_TITLE
        Else
            Me.Text = browser.DocumentTitle
        End If
    End Sub

#End Region 'Constructors

#Region " Events "

    ''' <summary>
    ''' Occurs when the <see cref="CanGoBack" /> property value changes.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.CanGoBackChanged</b> event.
    ''' </remarks>
    Public Event CanGoBackChanged As EventHandler
    ''' <summary>
    ''' Occurs when the <see cref="CanGoForward" /> property value changes.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.CanGoForwardChanged</b> event.
    ''' </remarks>
    Public Event CanGoForwardChanged As EventHandler
    ''' <summary>
    ''' Occurs when the internal <b>WebBrowser</b> control finishes loading a document.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.DocumentCompleted</b> event.
    ''' </remarks>
    Public Event DocumentCompleted As EventHandler(Of WebBrowserDocumentCompletedEventArgs)
    ''' <summary>
    ''' Occurs when the internal <b>WebBrowser</b> control has navigated to a new document and has begun loading it.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Navigated</b> event.
    ''' </remarks>
    Public Event Navigated As EventHandler(Of WebBrowserNavigatedEventArgs)
    ''' <summary>
    ''' Occurs before the internal <b>WebBrowser</b> control navigates to a new document.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Navigating</b> event.
    ''' </remarks>
    Public Event Navigating As EventHandler(Of WebBrowserNavigatingEventArgs)
    ''' <summary>
    ''' Occurs before a new browser window is opened.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.NewWindow</b> event.
    ''' </remarks>
    Public Event NewWindow As EventHandler(Of System.ComponentModel.CancelEventArgs)

#End Region 'Events

#Region " Event Handlers "

    Private Sub _browser_CanGoBackChanged(ByVal sender As Object, _
                                          ByVal e As EventArgs) Handles _browser.CanGoBackChanged
        Me.OnCanGoBackChanged(e)
    End Sub

    Private Sub _browser_CanGoForwardChanged(ByVal sender As Object, _
                                             ByVal e As EventArgs) Handles _browser.CanGoForwardChanged
        Me.OnCanGoForwardChanged(e)
    End Sub

    Private Sub _browser_DocumentCompleted(ByVal sender As Object, _
                                           ByVal e As WebBrowserDocumentCompletedEventArgs) Handles _browser.DocumentCompleted
        Me.OnDocumentCompleted(e)
    End Sub

    Private Sub _browser_DocumentTitleChanged(ByVal sender As Object, _
                                              ByVal e As EventArgs) Handles _browser.DocumentTitleChanged
        'Display the document title each time a new document is loaded.
        Me.Text = Me.Browser.DocumentTitle
    End Sub

    Private Sub _browser_Navigated(ByVal sender As Object, _
                                   ByVal e As WebBrowserNavigatedEventArgs) Handles _browser.Navigated
        Me.OnNavigated(e)
    End Sub

    Private Sub _browser_Navigating(ByVal sender As Object, _
                                    ByVal e As WebBrowserNavigatingEventArgs) Handles _browser.Navigating
        Me.OnNavigating(e)
    End Sub

    Private Sub _browser_NewWindow(ByVal sender As Object, _
                                   ByVal e As System.ComponentModel.CancelEventArgs) Handles _browser.NewWindow
        Me.OnNewWindow(e)
    End Sub

#End Region 'Event Handlers

#Region " Methods "

#Region " Public Methods "

    ''' <summary>
    ''' Navigates the <see cref="WebBrowserTabPage" /> control to the previous page in the navigation history, if one is available.
    ''' </summary>
    ''' <returns>
    ''' <b>True</b> if the navigation succeeds; <b>False</b> if a previous page in the navigation history is not available.
    ''' </returns>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.GoBack</b> method.
    ''' </remarks>
    Public Function GoBack() As Boolean
        Return Me.Browser.GoBack()
    End Function

    ''' <summary>
    ''' Navigates the <see cref="WebBrowserTabPage" /> control to the next page in the navigation history, if one is available.
    ''' </summary>
    ''' <returns>
    ''' <b>True</b> if the navigation succeeds; <b>False</b> if a subsequent page in the navigation history is not available.
    ''' </returns>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.GoForward</b> method.
    ''' </remarks>
    Public Function GoForward() As Boolean
        Return Me.Browser.GoForward()
    End Function

    ''' <summary>
    ''' Loads the document at the specified Uniform Resource Locator (URL) into the internal <b>WebBrowser</b> control, replacing the previous document.
    ''' </summary>
    ''' <param name="urlString">
    ''' The URL of the document to load.
    ''' </param>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Navigate</b> method.
    ''' </remarks>
    Public Overloads Sub Navigate(ByVal urlString As String)
        Me.Browser.Navigate(urlString)
    End Sub

    ''' <summary>
    ''' Loads the document at the location indicated by the specified <b>Uri</b> into the <b>WebBrowser</b> control, replacing the previous document.
    ''' </summary>
    ''' <param name="url">
    ''' A <b>Uri</b> representing the URL of the document to load.
    ''' </param>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Navigate</b> method.
    ''' </remarks>
    Public Overloads Sub Navigate(ByVal url As Uri)
        Me.Browser.Navigate(url)
    End Sub

    ''' <summary>
    ''' Prints the document currently displayed in the internal <b>WebBrowser</b> control using the current print and page settings.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Print</b> method.
    ''' </remarks>
    Public Sub Print()
        Me.Browser.Print()
    End Sub

    ''' <summary>
    ''' Opens the Internet Explorer Page Setup dialog box.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.ShowPageSetupDialog</b> method.
    ''' </remarks>
    Public Sub ShowPageSetupDialog()
        Me.Browser.ShowPageSetupDialog()
    End Sub

    ''' <summary>
    ''' Opens the Internet Explorer Print dialog box without setting header and footer values.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.ShowPrintDialog</b> method.
    ''' </remarks>
    Public Sub ShowPrintDialog()
        Me.Browser.ShowPrintDialog()
    End Sub

    ''' <summary>
    ''' Opens the Internet Explorer Print Preview dialog box.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.ShowPrintPreviewDialog</b> method.
    ''' </remarks>
    Public Sub ShowPrintPreviewDialog()
        Me.Browser.ShowPrintPreviewDialog()
    End Sub

    ''' <summary>
    ''' Opens the Internet Explorer Properties dialog box for the current document.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.ShowPropertiesDialog</b> method.
    ''' </remarks>
    Public Sub ShowPropertiesDialog()
        Me.Browser.ShowPropertiesDialog()
    End Sub

    ''' <summary>
    ''' Opens the Internet Explorer Save Web Page dialog box or the Save dialog box of the hosted document if it is not an HTML page.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.ShowSaveAsDialog</b> method.
    ''' </remarks>
    Public Sub ShowSaveAsDialog()
        Me.Browser.ShowSaveAsDialog()
    End Sub

    ''' <summary>
    ''' Cancels any pending navigation and stops any dynamic page elements, such as background sounds and animations.
    ''' </summary>
    ''' <remarks>
    ''' For more information see the <b>WebBrowser.Stop</b> method.
    ''' </remarks>
    Public Sub [Stop]()
        Me.Browser.Stop()
    End Sub

#End Region 'Public Methods

#Region " Protected Methods "

    ''' <summary>
    ''' Raises the <see cref="CanGoBackChanged" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' An <b>EventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnCanGoBackChanged(ByVal e As EventArgs)
        RaiseEvent CanGoBackChanged(Me, e)
    End Sub

    ''' <summary>
    ''' Raises the <see cref="CanGoForward" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' An <b>EventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnCanGoForwardChanged(ByVal e As EventArgs)
        RaiseEvent CanGoForwardChanged(Me, e)
    End Sub

    ''' <summary>
    ''' Raises the <see cref="DocumentCompleted" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' A <b>WebBrowserDocumentCompletedEventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnDocumentCompleted(ByVal e As WebBrowserDocumentCompletedEventArgs)
        RaiseEvent DocumentCompleted(Me, e)
    End Sub

    ''' <summary>
    ''' Raises the <see cref="Navigated" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' A <b>WebBrowserNavigatedEventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnNavigated(ByVal e As WebBrowserNavigatedEventArgs)
        RaiseEvent Navigated(Me, e)
    End Sub

    ''' <summary>
    ''' Raises the <see cref="Navigating" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' A <b>WebBrowserNavigatingEventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnNavigating(ByVal e As WebBrowserNavigatingEventArgs)
        RaiseEvent Navigating(Me, e)
    End Sub

    ''' <summary>
    ''' Raises the <see cref="NewWindow" /> event.
    ''' </summary>
    ''' <param name="e">
    ''' A <b>CancelEventArgs</b> that contains the event data.
    ''' </param>
    Protected Overridable Sub OnNewWindow(ByVal e As System.ComponentModel.CancelEventArgs)
        RaiseEvent NewWindow(Me, e)
    End Sub

#End Region 'Protected Methods

#End Region 'Methods

End Class
