VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "SAToolbarTools"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"SAToolbarTool"
Attribute VB_Ext_KEY = "Member0" ,"SAToolbarTool"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

'local variable to hold collection
Private m_objMainTools As XtremeCommandBars.CommandBarControls  ' Only set for ATB.Tools collection
Private m_objContainer As XtremeCommandBars.CommandBar          ' Only set for actual toolbar tools collections

Private mCol As Collection              ' ONLY USED FOR MAIN TOOLS COLLECTION
Private m_strBandName As String

Public Property Get BandName() As String
    If m_strBandName <> SA_VB_EmptyString Then
        BandName = m_strBandName
    Else
        BandName = "MAINTOOLS" 'DEV_STRING
    End If
End Property

Friend Function AddMainTool(ByVal v_lngToolID As Long, ByVal v_strName As String, Optional ByVal v_strCaption As String = SA_VB_EmptyString, Optional ByVal v_enuToolType As SATOOLBAR_TOOLTYPE = SATB_TT_BUTTON) As SAToolbarTool
    If Not m_objMainTools Is Nothing Then
        ' This is a main tools collection
        Set AddMainTool = AddInner(v_lngToolID, v_strName, v_strCaption, v_enuToolType)
    Else
        Exit Function
    End If
End Function

Public Function Add(ByVal v_lngToolID As Long, ByVal v_strName As String, Optional ByVal v_blnHasSubBand As Boolean = False, Optional ByVal v_blnTearOff As Boolean = False, Optional v_blnSetDefaultBitmap As Boolean = True, Optional v_lngAtPos As Long = -1) As SAToolbarTool
    Dim enuType As SATOOLBAR_TOOLTYPE
    
    If v_blnHasSubBand Then
        If v_blnTearOff Then
            enuType = SATB_TT_TEAROFFPOPUP
        Else
        enuType = SATB_TT_BUTTONPOPUP
        End If
    Else
        enuType = SATB_TT_BUTTON
    End If
    Set Add = AddInner(v_lngToolID, v_strName, , enuType, v_lngAtPos, v_blnSetDefaultBitmap)
End Function

Private Function AddInner(ByVal v_lngToolID As Long, ByVal v_strName As String, Optional ByVal v_strCaption As String = SA_VB_EmptyString, Optional ByVal v_enuToolType As SATOOLBAR_TOOLTYPE = SATB_TT_BUTTON, Optional v_lngAtPos As Long = -1, Optional v_blnSetDefaultBitmap As Boolean = True) As SAToolbarTool
    'create a new object
    Dim objMainTool As SAToolbarTool
    Dim objNewMember As SAToolbarTool
    Dim objTempTool As SAToolbarTool
    Dim objNewTool As XtremeCommandBars.CommandBarControl
    Dim objComboTool As XtremeCommandBars.CommandBarComboBox
    Dim enuControlType As XtremeCommandBars.XTPControlType

    Set AddInner = Nothing

    Select Case v_enuToolType
        Case SATB_TT_BUTTONDROPDOWN:
            enuControlType = xtpControlSplitButtonPopup
        Case SATB_TT_COMBOBOX:
            enuControlType = xtpControlComboBox
        Case SATB_TT_EDIT:
            enuControlType = xtpControlEdit
        Case SATB_TT_STATIC:
            enuControlType = xtpControlLabel
        Case SATB_TT_BUTTONPOPUP:
            enuControlType = xtpControlButtonPopup
        Case SATB_TT_TEAROFFPOPUP
            enuControlType = xtpControlSplitButtonPopup
        Case Else
            enuControlType = xtpControlButton
    End Select

    If m_objMainTools Is Nothing Then
        ' This is not a main tool being added so
        ' ensure the tool is in the main tools collection
        On Error Resume Next
        Set objMainTool = fMainForm.ATB.Tools(v_strName)
        If objMainTool Is Nothing Then
            If CmdLineQAReporting Then
                If Not v_lngToolID = 9400 And Not v_lngToolID = 9401 And Not v_lngToolID = 9148 Then
                    '9148 was "Toolbar Options" which was 9400 in old codejock version, 9401 was "Close"
                    '
                    SAMessageBox "Toolbar Error - Missing tool from main tools collection - '" & v_strName & "' id=" & v_lngToolID & " caption='" & v_strCaption & "'"  'DEV_STRING
                End If
            End If
            Exit Function
        End If
        On Error GoTo 0
    End If
    
    If m_objMainTools Is Nothing Then
        ' This is a normal band not the main tools collection
        If v_lngAtPos = -1 Or v_lngAtPos >= m_objContainer.Controls.Count Then
            ' add to the end
            Set objNewTool = objMainTool.Tool.Copy(m_objContainer)
        Else
            ' insert at position
            Set objNewTool = objMainTool.Tool.Copy(m_objContainer, v_lngAtPos + 1)
        End If

        Set objNewMember = New SAToolbarTool
        objNewMember.Init objNewTool
        
        objNewMember.Style = objMainTool.Style  ' Ensure the style is carried across
        
        Set AddInner = objNewMember     'return the object created
        
        If CmdLineToolCheck Then
            ' Add the tool to the master toolbar shadow file
            Dim strBeforeToolCaption As String
            
            If Not (IsDrawingTool(objNewMember.toolID) Or IsSpecialTool(objNewMember.toolID)) Then
                If v_lngAtPos >= 0 And v_lngAtPos < m_objContainer.Controls.Count Then
                    strBeforeToolCaption = Me.Item(v_lngAtPos).Caption
                End If
                TOOLCHECK_AddTool Me, objNewMember, strBeforeToolCaption
            End If
        End If
    Else
        ' This is the main tools collection and not a normal band
        On Error Resume Next
        Set objNewMember = mCol.Item(v_strName)
        On Error GoTo 0
        If objNewMember Is Nothing Then
            If (enuControlType = xtpControlButtonPopup) Or (enuControlType = xtpControlSplitButtonPopup) Then
                ' Popup tools are also created by the bands collection - but have no parameter(name) set
                ' this code picks them up and uses them correctly - associating the tool with the band
                Set objNewTool = FindByID(v_lngToolID)
            End If
            If objNewTool Is Nothing Then
                Set objNewTool = m_objMainTools.Add(enuControlType, v_lngToolID, v_strCaption)
            Else
#If TOOLDEBUGGING Then
                LogMessageTools "SAToolBarTools.AddInner - Adding popup - found existing tool - " & objNewTool.Caption & " - " & objNewTool.Parameter 'DEV_STRING
#End If
            End If
            objNewTool.Parameter = v_strName
            
            Set objNewMember = New SAToolbarTool
            objNewMember.Init objNewTool, True
            If v_blnSetDefaultBitmap Then
                objNewMember.SetPicture
            End If

            mCol.Add objNewMember, v_strName
        End If
        
        Set AddInner = objNewMember     'return the object created/retrieved
    End If

    On Error GoTo 0
    Exit Function
ErrExit:
    If CmdLineQAReporting Then
        SAMessageBox "Toolbar Error - Unable to add tool - " & v_strName & " - " & v_strCaption 'DEV_STRING
    End If
End Function

Friend Function AddMasterToolFromActualToolbar(ByRef r_objTool As XtremeCommandBars.CommandBarControl) As SAToolbarTool
    ' THIS FUNCTION IS ONLY USED BY THE SATOOLBAR.LOAD - It recreates the master tools collection from a saved toolbar
    Dim objNewMember As New SAToolbarTool
    
    objNewMember.Init r_objTool, True
    mCol.Add objNewMember, objNewMember.Tool.Parameter
    Set AddMasterToolFromActualToolbar = objNewMember
End Function

Public Property Get Item(ByVal vntIndexKey As Variant) As SAToolbarTool
Attribute Item.VB_UserMemId = 0
    'used when referencing an element in the collection
    'vntIndexKey contains either the Index or Key to the collection,
    'this is why it is declared as a Variant
    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
    Dim lngIndex As Long
    Dim objTool As XtremeCommandBars.CommandBarControl
    Dim objMember As SAToolbarTool

    If m_objMainTools Is Nothing Then
        If IsNumeric(vntIndexKey) Then
            ' We have been passed an index - use this
            vntIndexKey = vntIndexKey + 1
            Set objTool = m_objContainer.Controls.Item(vntIndexKey)
        Else
            ' We have been passed a name not an index - get first instance
            For lngIndex = 1 To m_objContainer.Controls.Count
                If m_objContainer.Controls.Item(lngIndex).Parameter = vntIndexKey Then
                    Set objTool = m_objContainer.Controls.Item(lngIndex)
                    Exit For
                End If
            Next
        End If
        
        If Not objTool Is Nothing Then
            Set objMember = New SAToolbarTool
            objMember.Init objTool
            Set Item = objMember
        End If
    Else
        If IsNumeric(vntIndexKey) Then
            vntIndexKey = vntIndexKey + 1
        End If
                
        On Error Resume Next
        Set Item = mCol(vntIndexKey)
        On Error GoTo 0
    End If
End Property

Private Function FindByID(ByVal lngToolID As Long) As XtremeCommandBars.CommandBarControl
    Dim lngIndex As Long
    Dim objTool As XtremeCommandBars.CommandBarControl
    Dim objCol As Object
    
    If m_objMainTools Is Nothing Then
        Set objCol = m_objContainer.Controls
    Else
        Set objCol = m_objMainTools
    End If
    
    For lngIndex = 1 To objCol.Count
        If objCol.Item(lngIndex).Id = lngToolID Then
            Set objTool = objCol.Item(lngIndex)
            Exit For
        End If
    Next

    Set FindByID = objTool
End Function

Friend Function IndexOf(ByVal lngToolID As Long) As Long
    Dim lngIndex As Long
    Dim objCol As Object

    IndexOf = -1 ' failed to find
    
    If m_objMainTools Is Nothing Then
        Set objCol = m_objContainer.Controls
    Else
        Set objCol = m_objMainTools
    End If
    
    For lngIndex = 1 To objCol.Count
        If objCol.Item(lngIndex).Id = lngToolID Then
            IndexOf = lngIndex
            Exit For
        End If
    Next
End Function

Public Property Get Count() As Long
    'used when retrieving the number of elements in the
    'collection. Syntax: Debug.Print x.Count
    Dim objCol As Object

    If m_objMainTools Is Nothing Then
        If Not m_objContainer Is Nothing Then
            Set objCol = m_objContainer.Controls
        Else
            Count = 0
            Exit Property
        End If
    Else
        Set objCol = mCol
    End If
    Count = objCol.Count
End Property

Public Sub Remove(ByVal vntIndexKey As Variant)
    Dim lngIndex As Long
    Dim objTool As XtremeCommandBars.CommandBarControl
    Dim objMember As SAToolbarTool

    If m_objMainTools Is Nothing Then
        ' Remove the tool from the band
        If IsNumeric(vntIndexKey) Then
            ' We have been passed an index - use this
            vntIndexKey = vntIndexKey + 1
            Set objTool = m_objContainer.Controls.Item(vntIndexKey)
            objTool.Delete
        Else
            ' We have been passed a name not an index - remove first instance
            For lngIndex = 1 To m_objContainer.Controls.Count
                If m_objContainer.Controls.Item(lngIndex).Parameter = vntIndexKey Then
                    Set objTool = m_objContainer.Controls.Item(lngIndex)
                    objTool.Delete
                    Exit For
                End If
            Next
        End If
    Else
        ' Master tool to remove
        If IsNumeric(vntIndexKey) Then
            vntIndexKey = vntIndexKey + 1
        End If
        
        On Error Resume Next
        Set objMember = mCol(vntIndexKey)
        If Err.Number = 0 Then
#If TOOLDEBUGGING Then
    LogMessageTools "Removing master tool item - " & vntIndexKey 'DEV_STRING
#End If
            fMainForm.ctlImageMan.Icons.RemoveIcon objMember.Tool.Id    ' Remove the icon for this tool
            objMember.Tool.Delete   ' TODO TOOLBAR - this access violates when removing TB_ tools - why?
            
            mCol.Remove vntIndexKey
        End If
        On Error GoTo 0
    End If

End Sub

Private Sub Class_Initialize()
    Set m_objMainTools = Nothing
    Set m_objContainer = Nothing
End Sub

Private Sub Class_Terminate()
    Set m_objMainTools = Nothing
    Set m_objContainer = Nothing
    Set mCol = Nothing
End Sub

Public Sub RemoveAll()
    While Me.Count > 0
        Me.Remove 0
    Wend
End Sub

Public Sub Insert(ByVal v_lngIndex As Long, ByRef r_objSATool As SAToolbarTool)
    If Not m_objContainer Is Nothing Then
        AddInner r_objSATool.toolID, r_objSATool.name, , , v_lngIndex
    End If
End Sub

Friend Sub Init(ByVal v_strBandName As String, ByRef r_objToolsOrCommandBar As Object)
#If TOOLDEBUGGING Then
    Static blnAlreadyInit As Boolean
    
    If blnAlreadyInit And v_strBandName <> SA_VB_EmptyString Then
        LogMessageTools "??? Tools collection has already been initialized - " & v_strBandName 'DEV_STRING
    End If
#End If
    m_strBandName = v_strBandName
    
    If v_strBandName = SA_VB_EmptyString Then
        Set m_objMainTools = r_objToolsOrCommandBar
        Set mCol = New Collection
    Else
        Set m_objContainer = r_objToolsOrCommandBar
    End If
#If TOOLDEBUGGING Then
    blnAlreadyInit = True
#End If
End Sub

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property

