Imports EnvDTE
Imports System.Windows.Forms

Public Module WindowMacros

    Function GetTextWindow(ByVal win As Window) As TextWindow
        ' Description: Returns the TextWindow object for a given window,
        '              or Nothing if not a text window

        Dim txtWin As TextWindow = Nothing

        ' Check for TextWindow
        If TypeOf win.Object Is TextWindow Then
            txtWin = win.Object

            ' Otherwise, check for EnvDTE.HTMLWindow, then TextWindow
        ElseIf TypeOf win.Object Is EnvDTE.HTMLWindow Then
            Dim htmlWin As EnvDTE.HTMLWindow = win.Object

            If htmlWin.CurrentTab = vsHTMLTabs.vsHTMLTabsSource Then
                txtWin = htmlWin.CurrentTabObject
            End If
        End If

        Return txtWin
    End Function

    Function IsEditorWindow(ByVal win As Window) As Boolean
        ' Description: Returns whether the window is an editor window

        Dim txtDoc As TextDocument

        If Not win.Document Is Nothing Then
            txtDoc = win.Document.Object("TextDocument")
        End If

        Return Not txtDoc Is Nothing
    End Function

    Function IsTextWindow(ByVal win As Window) As Boolean
        ' Description: Returns whether the window is a text window
        '              (TextWindow or EnvDTE.HTMLWindow in HTML view)

        Dim isTxtWin As Boolean = False

        If TypeOf win.Object Is TextWindow Then
            isTxtWin = True
        ElseIf TypeOf win.Object Is EnvDTE.HTMLWindow Then
            Dim htmlWin As EnvDTE.HTMLWindow = win.Object

            If htmlWin.CurrentTab = vsHTMLTabs.vsHTMLTabsSource Then
                isTxtWin = True
            End If
        End If

        Return isTxtWin
    End Function

    Function IsHTMLWindow(ByVal win As Window) As Boolean
        ' Description: Returns whether the window is an HTML window

        Return TypeOf win.Object Is EnvDTE.HTMLWindow
    End Function

End Module
