Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Chapter09

    Sub EnumWindows()
        Dim window As EnvDTE.Window
        For Each window In DTE.Windows
            MsgBox(window.Caption)
        Next
    End Sub

    Sub EnumWindows2()
        Dim window As EnvDTE.Window
        Dim i As Integer
        For i = 1 To DTE.Windows.Count
            MsgBox(DTE.Windows.Item(i).Caption)
        Next
    End Sub

    Sub FindTaskListWindow()
        Dim objWindow As EnvDTE.Window
        objWindow = DTE.Windows.Item("Aufgabenliste")
    End Sub

    Sub FindTaskListWindow2()
        Dim objWindow As EnvDTE.Window
        objWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindTaskList)
    End Sub

    Sub FindTheSourceControlExplorerWindow()
        Dim window As EnvDTE.Window
        window = DTE.Windows.Item("{99B8FA2F-AB90-4F57-9C32-949F146F1914}")
    End Sub

    Sub FindTheSourceControlExplorerWindow2()
        Dim window As EnvDTE.Window
        'You should show the Source Control Explorer window
        ' before calling this code!
        window = DTE.Windows.Item("Quellcodeverwaltungs-Explorer")
        MsgBox(window.ObjectKind)
    End Sub

    Sub GetTaskListObject()
        Dim window As EnvDTE.Window
        Dim taskList As EnvDTE.TaskList
        window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindTaskList)
        taskList = CType(window.Object, EnvDTE.TaskList)
    End Sub

    Sub FindTaskList()
        Dim taskList As TaskList

        taskList = CType(DTE, DTE2).ToolWindows.TaskList
    End Sub

    Sub FindTheMainWindow()
        Dim mainWindow As EnvDTE.Window
        mainWindow = DTE.MainWindow
    End Sub

    Sub ShowFileOpenDialog()
        Dim openFile As New System.Windows.Forms.OpenFileDialog
        openFile.ShowDialog(New WinWrapper)
    End Sub

    Sub GetTopLevelUIHierItems()
        Dim macroExplWin As Window
        Dim uiHierarchy As EnvDTE.UIHierarchy
        Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
        'Find the macro explorer window, and the UIHierarchy
        ' object for this window:
        macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
        uiHierarchy = macroExplWin.Object
        'Get the top level collection of items:
        uiHierarchyItems = uiHierarchy.UIHierarchyItems
        'Display the name of the first node in this collection:
        MsgBox(uiHierarchyItems.Item(1).Name)
    End Sub

    Sub WalkMacroProjects()
        Dim macroExplWin As Window
        Dim uiHierarchy As EnvDTE.UIHierarchy
        Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
        Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
        Dim uiHierarchyItem2 As EnvDTE.UIHierarchyItem
        'Find the Macro Explorer window, and the UIHierarchy
        ' object for this window:
        macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
        uiHierarchy = macroExplWin.Object
        'Get the first node in this collection, the Macros node:
        uiHierarchyItem = uiHierarchy.UIHierarchyItems.Item(1)
        'Walk all the items in this collection, which is
        ' the list of macro projects:
        For Each uiHierarchyItem2 In uiHierarchyItem.UIHierarchyItems
            MsgBox(uiHierarchyItem2.Name)
        Next
    End Sub

    Sub FindUIHierItemForInsertDateMacro()
        Dim macroExplWin As Window
        Dim uiHierarchy As EnvDTE.UIHierarchy
        Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
        Dim uiHierarchyItems As EnvDTE.UIHierarchyItems
        macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
        uiHierarchy = macroExplWin.Object
        uiHierarchyItems = uiHierarchy.UIHierarchyItems
        uiHierarchyItem = uiHierarchyItems.Item("Makros")
        uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
        uiHierarchyItem = uiHierarchyItems.Item("Samples")
        uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
        uiHierarchyItem = uiHierarchyItems.Item("VSEditor")
        uiHierarchyItems = uiHierarchyItem.UIHierarchyItems
        uiHierarchyItem = uiHierarchyItems.Item("InsertDate")
        MsgBox(uiHierarchyItem.Name)
    End Sub

    Sub FindUIHierItemForInsertDateMacro2()
        Dim macroExplWin As Window
        Dim uiHierarchy As EnvDTE.UIHierarchy
        Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
        macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
        uiHierarchy = macroExplWin.Object
        uiHierarchyItem = _
            uiHierarchy.GetItem("Makros\Samples\VSEditor\InsertDate")
        MsgBox(uiHierarchyItem.Name)
    End Sub

    Sub GetUIHierSelectedItems()
        Dim macroExplWin As Window
        Dim uiHierarchy As EnvDTE.UIHierarchy
        Dim selectedItems As Object()
        Dim uiHierarchyItem As EnvDTE.UIHierarchyItem
        macroExplWin = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
        uiHierarchy = macroExplWin.Object
        selectedItems = uiHierarchy.SelectedItems
        For Each uiHierarchyItem In selectedItems
            MsgBox(uiHierarchyItem.Name)
        Next
    End Sub

    Sub GetUIHierItemObject()
        Dim uihier As EnvDTE.UIHierarchy
        Dim uihierProj As EnvDTE.UIHierarchyItem
        Dim uihierProjItem As EnvDTE.UIHierarchyItem
        Dim project As EnvDTE.Project
        Dim projItem As EnvDTE.ProjectItem
        uihier = DTE.Windows.Item( _
            Constants.vsWindowKindSolutionExplorer).Object
        uihierProj = uihier.UIHierarchyItems.Item(1).UIHierarchyItems.Item(1)
        project = uihierProj.Object
        uihierProjItem = uihierProj.UIHierarchyItems.Item(2)
        projItem = uihierProjItem.Object
    End Sub

    Sub FindTheToolBox()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
    End Sub

    Sub AddNewToolBoxTab()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
        toolBox.ToolBoxTabs.Add("My commonly used items").Activate()
    End Sub

    Sub EnumerateToolBoxContents()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        Dim toolBoxTab As ToolBoxTab
        Dim outputWindow As New _
            InsideVSNET.Utilities.OutputWindowPaneEx(DTE, "Toolbox contents")
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
        For Each toolBoxTab In toolBox.ToolBoxTabs
            Dim toolBoxItem As ToolBoxItem
            outputWindow.WriteLine(toolBoxTab.Name)
            For Each toolBoxItem In toolBoxTab.ToolBoxItems
                outputWindow.WriteLine(vbTab + toolBoxItem.Name)
            Next
        Next
    End Sub

    Sub AddTextToTheToolBox()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        Dim toolBoxTab As EnvDTE.ToolBoxTab
        Dim toolBoxItems As EnvDTE.ToolBoxItems
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
        toolBoxTab = toolBox.ToolBoxTabs.Item("Allgemein")
        toolBoxItems = toolBoxTab.ToolBoxItems
        toolBoxItems.Add("My Text", "This is some text", _
            vsToolBoxItemFormat.vsToolBoxItemFormatText)
    End Sub

    Sub AddHTMLToTheToolBox()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        Dim toolBoxTab As EnvDTE.ToolBoxTab
        Dim toolBoxItems As EnvDTE.ToolBoxItems
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
        toolBoxTab = toolBox.ToolBoxTabs.Item("Allgemein")
        toolBoxItems = toolBoxTab.ToolBoxItems
        toolBoxItems.Add("My HTML", "<b>This is bold HTML</b>", _
            vsToolBoxItemFormat.vsToolBoxItemFormatHTML)
    End Sub

    Sub AddCOMObjectToTheToolBox()
        Dim toolBoxWindow As EnvDTE.Window
        Dim toolBox As EnvDTE.ToolBox
        Dim toolBoxTab As EnvDTE.ToolBoxTab
        Dim toolBoxItems As EnvDTE.ToolBoxItems
        toolBoxWindow = DTE.Windows.Item(Constants.vsWindowKindToolbox)
        toolBox = toolBoxWindow.Object
        toolBoxTab = toolBox.ToolBoxTabs.Item("Allgemein")
        toolBoxItems = toolBoxTab.ToolBoxItems
        toolBoxItems.Add("Name", "{22D6F312-B0F6-11D0-94AB- 0080C74C7E95}", _
            vsToolBoxItemFormat.vsToolBoxItemFormatGUID)
        toolBoxItems.Add("Name", "MediaPlayer.MediaPlayer.1", _
            vsToolBoxItemFormat.vsToolBoxItemFormatGUID)
    End Sub

    Sub TLAddItems()
        Dim taskList As EnvDTE.TaskList
        taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
        taskList.TaskItems.Add("Category", "", "Description2")
        taskList.TaskItems.Add("Category", "", "Description1")
    End Sub

    Sub TLAddItemsPriority()
        Dim taskList As EnvDTE.TaskList
        taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
        taskList.TaskItems.Add("Category", "", _
            "Description1", vsTaskPriority.vsTaskPriorityHigh)
        taskList.TaskItems.Add("Category", "", _
           "Description2", vsTaskPriority.vsTaskPriorityLow)
        taskList.TaskItems.Add("Category", "", _
           "Description3", vsTaskPriority.vsTaskPriorityMedium)
    End Sub

    Sub ModifyTaskDescription()
        Dim taskList As EnvDTE.TaskList
        Dim task As EnvDTE.TaskItem
        taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
        task = taskList.TaskItems.Item(1)
        If (task.IsSettable(vsTaskListColumn.vsTaskListColumnDescription)) Then
            task.Description = "A new description"
        End If
    End Sub

    Sub GetTokensArray()
        Dim tokens As Object()
        Dim prop As EnvDTE.Property
        Dim props As EnvDTE.Properties
        props = DTE.Properties("Environment", "TaskList")
        prop = props.Item("CommentTokens")
        tokens = prop.Value
    End Sub

    Sub AddSecurityToken()
        Dim tokens As Object()
        Dim token As String
        Dim prop As EnvDTE.Property
        Dim props As EnvDTE.Properties
        'Find the property holding the known tokens
        props = DTE.Properties("Environment", "TaskList")
        prop = props.Item("CommentTokens")
        tokens = prop.Value
        'Add one to the list of known tokens to hold
        ' the new SECURITY token
        ReDim Preserve tokens(tokens.Length)
        'Add the new token
        tokens(tokens.Length - 1) = "SECURITY:3"
        'Set the list of known tokens
        prop.Value = tokens
    End Sub

    Sub RemoveSecurityToken()
        Dim tokens As Object()
        Dim newTokens As Object()
        Dim token As String
        Dim i As Integer = 0
        Dim found As Boolean = False
        Dim prop As EnvDTE.Property
        Dim props As EnvDTE.Properties
        props = DTE.Properties("Environment", "TaskList")
        prop = props.Item("CommentTokens")
        tokens = prop.Value
        'Don't want to shrink the array if 
        '  the token is not available
        For Each token In tokens
            If token = "SECURITY:3" Then
                found = True
                Exit For
            End If
        Next
        'If the SECURITY token was not found, then
        ' there is nothing to remove so we can exit
        If found = False Then
            Exit Sub
        End If
        'Resize the newTokens array
        ReDim newTokens(tokens.Length - 2)
        'Copy the list of tokens into the newTokens array
        ' skipping the SECURITY token
        For Each token In tokens
            If token <> "SECURITY:3" Then
                newTokens(i) = token
                i = i + 1
            End If
        Next
        'Set the list of tokens
        prop.Value = newTokens
    End Sub

    Sub InsertTLTokenCode()
        Dim classString As String
        Dim taskList As EnvDTE.TaskList
        taskList = DTE.Windows.Item(Constants.vsWindowKindTaskList).Object
        classString = "Public Class AClass" + Chr(13)
        classString = classString + Chr(9) + "'" + taskList.DefaultCommentToken
        classString = classString + ": Insert your code here" + Chr(13)
        classString = classString + "End Class"
    End Sub

    Sub FindErrorList()
        Dim errorList As ErrorList

        'Find the ErrorList object using the Windows.Item(...).Object style
        errorList = DTE.Windows.Item(EnvDTE80.WindowKinds.vsWindowKindErrorList).Object

        'Find the ErrorList object using the ToolWindows style
        errorList = CType(DTE, DTE2).ToolWindows.ErrorList
    End Sub

    Sub FindOutputWindow()
        Dim window As EnvDTE.Window
        Dim outputWindow As EnvDTE.OutputWindow
        window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        outputWindow = CType(window.Object, EnvDTE.OutputWindow)
    End Sub

    Sub EnumOutputWindowPanes()
        Dim window As EnvDTE.Window
        Dim outputWindow As EnvDTE.OutputWindow
        Dim outputWindowPanes As EnvDTE.OutputWindowPanes
        Dim outputWindowPane As EnvDTE.OutputWindowPane
        'Find the OutputWindow object
        window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        outputWindow = CType(window.Object, EnvDTE.OutputWindow)
        'Retrieve the OutputWindowPanes object
        outputWindowPanes = outputWindow.OutputWindowPanes
        'Enumerate each OutputWindowPane
        For Each outputWindowPane In outputWindowPanes
            MsgBox(outputWindowPane.Name)
        Next
    End Sub

    Sub CreateOutputWindowPane()
        Dim window As EnvDTE.Window
        Dim outputWindow As EnvDTE.OutputWindow
        Dim outputWindowPanes As EnvDTE.OutputWindowPanes
        Dim outputWindowPane As EnvDTE.OutputWindowPane
        'Find the OutputWindow object
        window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        outputWindow = CType(window.Object, EnvDTE.OutputWindow)
        'Retrieve the OutputWindowPanes object
        outputWindowPanes = outputWindow.OutputWindowPanes
        'Add a new pane:
        outputWindowPane = outputWindowPanes.Add("My New Pane")
    End Sub

    Sub DisplaySolutionDirectory()
        Dim files As String()
        Dim file As String
        Dim directoryOutputWindowPane As OutputWindowPane
        Dim fullName As String
        Dim outputWindow As OutputWindow
        outputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object

        'Find the folder the solution is in, as well as the files that are
        ' in that folder:
        fullName = System.IO.Path.GetDirectoryName(DTE.Solution.FullName)
        files = System.IO.Directory.GetFiles(fullName)

        'Try to find a "Solution Directory" pane, if one does not exist,
        ' create it:
        With outputWindow.OutputWindowPanes
            Try
                directoryOutputWindowPane = .Item("Solution Directory")
                'Show the pane:
                directoryOutputWindowPane.Activate()
            Catch
                directoryOutputWindowPane = .Add("Solution Directory")
            End Try
        End With
        'Clear the pane:
        directoryOutputWindowPane.Clear()
        For Each file In files
            'Display the file path, with a line break between each line
            directoryOutputWindowPane.OutputString(file + Chr(13))
        Next
    End Sub

    Sub GetVBTextEditorProperties()
        Dim properties As Properties
        properties = DTE.Properties("TextEditor", "Basic")
    End Sub

    Sub WalkPropertyNames()
        Dim categoryName As String
        Dim key As Microsoft.Win32.RegistryKey
        Dim newDocument As Document
        Dim selection As TextSelection
        'Open a new document to store the information
        newDocument = DTE.ItemOperations.NewFile("Allgemein\Textdatei").Document
        selection = newDocument.Selection
        'Open the registry key that holds the list of categories:
        key = Microsoft.Win32.Registry.LocalMachine
        key = key.OpenSubKey( _
           "SOFTWARE\Microsoft\VisualStudio\8.0\AutomationProperties")
        'Enumerate the categories:
        For Each categoryName In key.GetSubKeyNames()
            Dim subcategoryName As String
            selection.Insert(categoryName + vbLf)
            'Enumerate the subcategories:
            For Each subcategoryName In _
                key.OpenSubKey(categoryName).GetSubKeyNames()
                Dim properties As Properties
                Dim prop As [Property]
                selection.Insert("  " + subcategoryName + vbLf)
                Try
                    'Enumerate each property:
                    properties = DTE.Properties(categoryName, subcategoryName)
                    For Each prop In properties
                        selection.Insert("   " + prop.Name + vbLf)
                    Next
                Catch
                End Try
            Next
        Next
    End Sub

    Sub GetVBTabSizeProperty()
        Dim properties As Properties
        Dim prop As [Property]
        properties = DTE.Properties("TextEditor", "Basic")
        prop = properties.Item("TabSize")
    End Sub

    Sub GetVBTabSize()
        Dim properties As Properties
        properties = DTE.Properties("TextEditor", "Basic")
        MsgBox(properties.Item("TabSize").Value)
    End Sub

    Sub SetVBTabSize()
        Dim properties As Properties
        properties = DTE.Properties("TextEditor", "Basic")
        properties.Item("TabSize").Value = 4
    End Sub

    Sub CompareWindowsObjects()
        Dim window1 As Window
        Dim window2 As Window
        window1 = DTE.Windows.Item(Constants.vsWindowKindTaskList)
        window2 = DTE.Windows.Item(Constants.vsWindowKindTaskList)
        MsgBox(window1 Is window2)
    End Sub

    Sub ComparePropertyObjects()
        Dim props1 As Properties
        Dim props2 As Properties
        props1 = DTE.Properties("Environment", "General")
        props2 = DTE.Properties("Environment", "General")
        MsgBox(props1 Is props2)
    End Sub

    Sub RepeatedConstruct()
        Dim i As Long
        Dim props As Properties
        For i = 1 To Long.MaxValue
            props = DTE.Properties("Environment", "General")
        Next
    End Sub

    Sub OptimizedRepeatedConstruct()
        Dim i As Long
        Dim props As Properties
        Dim showStatusbar As Boolean
        props = DTE.Properties("Environment", "General")
        For i = 1 To Long.MaxValue
            showStatusbar = props.Item("ShowStatusBar").Value
        Next
    End Sub

    Sub ShowSamplePropertyValue()
        Dim props As Properties

        props = DTE.Properties("My Custom Category", "My Custom Subcategory")
        MsgBox(props.Item("SampleProperty").Value)
    End Sub

End Module


Public Class WinWrapper
    Implements System.Windows.Forms.IWin32Window

    Overridable ReadOnly Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
            Return iptr
        End Get
    End Property
End Class

Namespace InsideVSNET
    Namespace Utilities

        Class OutputWindowPaneEx
            Dim pane As OutputWindowPane
            Dim dte As EnvDTE.DTE

            Public Sub New(ByVal application As _DTE)
                dte = application
                InitPane("(Default)")
            End Sub

            Public Sub New(ByVal application As _DTE, ByVal windowName As String)
                dte = application
                InitPane(windowName)
            End Sub

            Private Sub InitPane(ByVal windowName As String)
                Dim win As Window = dte.Windows.Item(Constants.vsWindowKindOutput)
                Dim window As OutputWindow = CType(win.Object, OutputWindow)

                Try
                    pane = window.OutputWindowPanes.Item(windowName)
                Catch
                    pane = window.OutputWindowPanes.Add(windowName)
                End Try
            End Sub

            Public Sub WriteLine()
                pane.OutputString(vbLf)
            End Sub

            Public Sub WriteLine(ByVal format As String, ByVal args As Object())
                pane.OutputString(String.Format(format, args) + vbLf)
            End Sub

            Public Sub WriteLine(ByVal text As String)
                pane.OutputString(text + vbLf)
            End Sub

            Public Sub WriteLine(ByVal obj As Object)
                pane.OutputString(obj.ToString() + vbLf)
            End Sub

            Public Sub Write(ByVal format As String, ByVal args As Object())
                pane.OutputString(String.Format(format, args))
            End Sub

            Public Sub Write(ByVal text As String)
                pane.OutputString(Text)
            End Sub

            Public Sub Write(ByVal obj As Object)
                pane.OutputString(obj.ToString())
            End Sub

        End Class

    End Namespace

End Namespace