#include-once
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#region Header
; #INDEX# =======================================================================================================================
; Title .........: ThumbBar
; AutoIt Version : 3.3.4.0
; Language ......: English
; Description ...: Functions to assist in creating Thumbnail Toolbars.
; Author(s) .....: Beege
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_ThumbBar_AddButtons
;_ThumbBar_CreateButton
;_ThumbBar_SetImageList
;_ThumbBar_UpdateButton
;_ThumbBar_GetButtonID
;_ThumbBar_GetObj
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
;__GetTaskBarObj
;__SetDllStructData
;__ThumbExit
;__UpdateMask
;__WM_Command
;__SetWMCommand_Return
;__ErrFunc
; ===============================================================================================================================

#region User KeyWords and CallTips
#cs

    Keywords:
    au3.keywords.user.udfs= _thumbbar_addbuttons_thumbbar_createbutton _thumbbar_getbuttonid _thumbbar_getobj \
    _thumbbar_setimagelist _thumbbar_updatebutton

    CallTips:
    _ThumbBar_CreateButton($sToolTip = '', $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iFlags = -1, $iMask = -1)
    _ThumbBar_UpdateButton($iIndex, $iFlags = -1, $sToolTip = -1, $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iMask = -1)
    _ThumbBar_SetImageList($hGui, $hImageList)
    _ThumbBar_AddButtons($hGui)
    _ThumbBar_GetCtrlID($iIndex)

#ce
#endregion User KeyWords and CallTips

#include-once
#include <WindowsConstants.au3>
#include <AutoItObject.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#endregion Header

#region Global Variables and Constants

;THUMBBUTTONMASK
Global Const $THB_BITMAP = 0x00000001
Global Const $THB_ICON = 0x00000002
Global Const $THB_TOOLTIP = 0x00000004
Global Const $THB_FLAGS = 0x00000008
;THUMBBUTTONFLAGS
Global Const $THBF_ENABLED = 0x00000000
Global Const $THBF_DISABLED = 0x00000001
Global Const $THBF_DISMISSONCLICK = 0x00000002
Global Const $THBF_NOBACKGROUND = 0x00000004
Global Const $THBF_HIDDEN = 0x00000008
Global Const $THBF_NONINTERACTIVE = 0x00000010
;THUMBBUTTONEVENTS
Global Const $THBN_CLICKED = 0x1800

Global Enum $g_oTaskBar = 1, $g_DllStruct, $g_hGui, $g_vWMCommand_Ret
Global Enum $g_iID, $g_hIcon, $g_sToolTip, $g_sCallFunc, $g_iFlags, $g_iBitmap, $g_iMask, $Max
Global $aButtons[1][$Max] = [[0, 0]]

#cs

    $aButtons[0][0]                     = Button Count
    $aButtons[0][$g_oTaskBar]           = TaskbarList object
    $aButtons[0][$g_DllStruct]          = DllStruct Array contating THUMBSTRUCTURES
    $aButtons[0][$g_hGui]               = Handle to GUI
    $aButtons[0][$g_vWMCommand_Ret]     = WM_COMMAND Return Value

    $aButtons[$i][$g_iID]               = Button ID
    $aButtons[$i][$g_hIcon]             = Handle to button icon
    $aButtons[$i][$g_sToolTip]          = Text that is displayed when mouse hovers button
    $aButtons[$i][$g_sCallFunc]         = Function to call when button is pressed
    $aButtons[$i][$g_iFlags]            = A combination of THUMBBUTTONFLAGS values that control specific states and behaviors of the button.
    $aButtons[$i][$g_iBitmap]           = Zero-based index of the button image within the image list set through set through _ThumbBar_SetImageList.
    $aButtons[$i][$g_iMask]             = A combination of THUMBBUTTONMASK values that specify which members of this structure contain valid data

#ce

#endregion Global Variables and Constants

#region Initiations

Global $oError = ObjEvent("AutoIt.Error", "__ErrFunc")
$aButtons[0][$g_oTaskBar] = 0
$aButtons[0][$g_vWMCommand_Ret] = $GUI_RUNDEFMSG
_AutoItObject_StartUp()
GUIRegisterMsg($WM_COMMAND, '__WM_Command')
OnAutoItExitRegister('__ThumbExit')
_ThumbBar_GetObj()

#endregion Initiations

#region Public Functions

; ===================================================================
; _ThumbBar_AddButtons($hGui)
;
; Description - Applies created buttons to GUI Thumbnail
; Parameters:
;   $hGui - IN - Handle to GUI
; Returns:
;   Success - 1
;   Error - 1 No buttons have been created
; Remarks: This function can only be called once. Buttons can be hidden/restored by Update function, but not added multiple times.
;       Maximum of 7 buttons is allowed.
; ===================================================================
Func _ThumbBar_AddButtons($hGui)

    $aButtons[0][$g_hGui] = Number($hGui)

    If $aButtons[0][0] = 0 Then Return SetError(1, 0, 0);No Values have been created

    Local $i, $tagTHUMBBUTTON = "dword;UINT;UINT;handle;WCHAR[260];dword_ptr"
    For $i = 1 To $aButtons[0][0]
        $tagTHUMBBUTTON &= ';' & $tagTHUMBBUTTON
    Next

    $aButtons[0][$g_DllStruct] = DllStructCreate($tagTHUMBBUTTON)
    __SetDllStructData()

    Local $oTaskBar = $aButtons[0][$g_oTaskBar]
    $oTaskBar.ThumbBarAddButtons($aButtons[0][$g_hGui], $aButtons[0][0], Number(DllStructGetPtr($aButtons[0][$g_DllStruct])))

    Return 1

EndFunc   ;==>_ThumbBar_AddButtons

; ===================================================================
; _ThumbBar_CreateButton($sToolTip = '', $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iFlags = -1, $iMask = -1)
;
; Description - Creates a ThumbBar button
; Parameters:
;   $sToolTip - IN/OPTIONAL - Text of the button's tooltip displayed when the mouse pointer hovers over the button
;   $sIcon - IN/OPTIONAL - The handle or path of an icon to use as the button image
;   $iBitmap - IN/OPTIONAL - The zero-based index of the button image within the image list set through _ThumbBar_SetImageList.
;   $sFunctiontoCall - IN/OPTIONAL - Function to call when button is pressed
;   $iFlags - IN/OPTIONAL - A combination of THUMBBUTTONFLAGS values that control specific states and behaviors of the button. Default = $THBF_ENABLED
;   $iMask - IN/OPTIONAL - A combination of THUMBBUTTONMASK values that specify which members of this structure contain valid data.
; Returns:
;   Success - Button Index
;   Error - 1 File does not exist
;           2 Failed loading icon image.
;           3 Maximum Button Count Reached
; Remarks - Unless specified, iMask will automatically be set to the appropriate value.
;       Maximum of 7 buttons is allowed.
; ===================================================================
Func _ThumbBar_CreateButton($sToolTip = '', $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iFlags = -1, $iMask = -1)

    Static $iD = 500

    If $aButtons[0][0] = 7 Then Return SetError(3, 0, 0)

    If $sIcon <> -1 Then
        If Not IsPtr($sIcon) Then
            If Not FileExists($sIcon) Then Return SetError(1, 0, 0)
            $sIcon = _WinAPI_LoadImage(0, $sIcon, $IMAGE_ICON, 16, 16, $LR_LOADFROMFILE)
            If @error Then Return SetError(2, 0, 0)
        EndIf
    EndIf

    ReDim $aButtons[UBound($aButtons) + 1][UBound($aButtons, 2)]
    $aButtons[0][0] += 1
    $iD += 1

    If $sIcon = -1 Then $sIcon = 0
    If $iFlags = -1 Then $iFlags = $THBF_ENABLED

    $aButtons[$aButtons[0][0]][$g_iID] = $iD
    $aButtons[$aButtons[0][0]][$g_hIcon] = $sIcon
    $aButtons[$aButtons[0][0]][$g_sToolTip] = $sToolTip
    $aButtons[$aButtons[0][0]][$g_sCallFunc] = $sFunctiontoCall
    $aButtons[$aButtons[0][0]][$g_iFlags] = $iFlags
    $aButtons[$aButtons[0][0]][$g_iBitmap] = $iBitmap

    If $iMask <> -1 Then
        $aButtons[$aButtons[0][0]][$g_iMask] = $iMask
    Else
        __UpdateMask($aButtons[0][0])
    EndIf

    Return $aButtons[0][0]

EndFunc   ;==>_ThumbBar_CreateButton

; ===================================================================
; _ThumbBar_UpdateButton($iIndex, $iFlags = -1, $sToolTip = -1, $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iMask = -1)
;
; Description - Shows, enables, disables, or hides buttons in a thumbnail toolbar as required by the window's current state.
; Parameters:
;   $iIndex - IN - Index value returned from _ThumbBar_CreateButton().
;   $iFlags - IN/OPTIONAL - A combination of THUMBBUTTONFLAGS values that control specific states and behaviors of the button.
;   $sToolTip - IN/OPTIONAL - Text of the button's tooltip displayed when the mouse pointer hovers over the button
;   $sIcon - IN/OPTIONAL - IN/OPTIONAL - The handle or path of an icon to use as the button image
;   $iBitmap - IN/OPTIONAL - The zero-based index of the button image within the image list set through _ThumbBar_SetImageList.
;   $sFunctiontoCall - IN/OPTIONAL - Function to call when button is pressed
;   $iMask - IN/OPTIONAL - A combination of THUMBBUTTONMASK values that specify which members of this structure contain valid data.
; Returns:
;   Success - 1
;   Error   - 1 Invalid Index
; Remarks - Any Parameter that is -1 will not be changed.
; ===================================================================
Func _ThumbBar_UpdateButton($iIndex, $iFlags = -1, $sToolTip = -1, $sIcon = -1, $iBitmap = -1, $sFunctiontoCall = -1, $iMask = -1)

    If $iIndex < 1 Or $iIndex > $aButtons[0][0] Then Return SetError(1, 0, 0)

    If $sIcon <> -1 Then $aButtons[$iIndex][$g_hIcon] = $sIcon
    If $sFunctiontoCall <> -1 Then $aButtons[$iIndex][$g_sCallFunc] = $sFunctiontoCall
    If $iFlags <> -1 Then $aButtons[$iIndex][$g_iFlags] = $iFlags
    If $iBitmap <> -1 Then $aButtons[$iIndex][$g_iBitmap] = $iBitmap
    If $sToolTip <> -1 Then $aButtons[$iIndex][$g_sToolTip] = $sToolTip

    If $iMask <> -1 Then
        $aButtons[$iIndex][$g_iMask] = $iMask
    Else
        __UpdateMask($iIndex)
    EndIf

    __SetDllStructData()

    Local $oTaskBar = $aButtons[0][$g_oTaskBar]
    $oTaskBar.ThumbBarUpdateButtons($aButtons[0][$g_hGui], $aButtons[0][0], Number(DllStructGetPtr($aButtons[0][$g_DllStruct])))

    Return 1

EndFunc   ;==>_ThumbBar_UpdateButton

; ===================================================================
; _ThumbBar_SetImageList($hGui, $hImageList)
;
; Description - Specifies an image list that contains button images for a thumbnail buttons.
; Parameters:
;   $hGui - IN - Handle to GUI
;   $hImageList - IN - The handle of the image list that contains all button images to be used in the toolbar.
; Returns:
;   Success - 1
; Remarks - None
; ===================================================================
Func _ThumbBar_SetImageList($hGui, $hImageList)

    Local $oTaskBar = $aButtons[0][$g_oTaskBar]
    $oTaskBar.ThumbBarSetImageList(Number($hGui), Number($hImageList))

    Return 1

EndFunc   ;==>_ThumbBar_SetImageList

; ===================================================================
; _ThumbBar_GetObj()
;
; Description - Creates and Returns ITaskbarList3 Dispatch-Object
; Parameters:
;   None.
; Returns:
;   Success - Dispatch-Object
; Remarks - This object is automatically created on started on destoryed on exit.
; ===================================================================
Func _ThumbBar_GetObj()

    If $aButtons[0][$g_oTaskBar] = 0 Then $aButtons[0][$g_oTaskBar] = __GetTaskBarObj()
    Return $aButtons[0][$g_oTaskBar]

EndFunc   ;==>_ThumbBar_GetObj

; ===================================================================
; _ThumbBar_GetButtonID($iIndex)
;
; Description - Returns ID of Button. Use with WM_COMMAND.
; Parameters:
;   $iIndex - IN - Index returned from _ThumbBar_CreateButton().
; Returns:
;   Success - Id of Button.
;   Error - 1 Invalid Index
; Remarks - None
; ===================================================================
Func _ThumbBar_GetButtonID($iIndex)

    If $iIndex < 1 Or $iIndex > $aButtons[0][0] Then Return SetError(1, 0, 0)
    Return $aButtons[$iIndex][$g_iID]

EndFunc   ;==>_ThumbBar_GetButtonID

#endregion Public Functions

#region Internel Functions
Func __SetWMCommand_Return($sValue)
    ; set _WM_Command Return Value
    $aButtons[0][$g_vWMCommand_Ret] = $sValue
EndFunc   ;==>__SetWMCommand_Return

Func __SetDllStructData()

    Local $j = 1

    For $i = 1 To $aButtons[0][0]
        DllStructSetData($aButtons[0][$g_DllStruct], $j, $aButtons[$i][$g_iMask])
        DllStructSetData($aButtons[0][$g_DllStruct], $j + 1, $aButtons[$i][$g_iID])
        DllStructSetData($aButtons[0][$g_DllStruct], $j + 2, $aButtons[$i][$g_iBitmap])
        DllStructSetData($aButtons[0][$g_DllStruct], $j + 3, $aButtons[$i][$g_hIcon])
        DllStructSetData($aButtons[0][$g_DllStruct], $j + 4, $aButtons[$i][$g_sToolTip])
        DllStructSetData($aButtons[0][$g_DllStruct], $j + 5, $aButtons[$i][$g_iFlags])
        $j += 6
    Next

EndFunc   ;==>__SetDllStructData

Func __GetTaskBarObj()

    Local $CLSID_TaskBarlist = _AutoItObject_CLSIDFromString("{56FDF344-FD6D-11D0-958A-006097C9A090}")
    Local $IID_ITaskbarList3 = _AutoItObject_CLSIDFromString("{ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf}")
    Local $tagInterface = "QueryInterface long(ptr;ptr;ptr);" & _
            "AddRef ulong();" & _
            "Release ulong();" & _
            "HrInit long();" & _
            "AddTab long(hwnd);" & _
            "DeleteTab long(hwnd);" & _
            "ActivateTab long(hwnd);" & _
            "SetActiveAlt long(hwnd);" & _
            "MarkFullscreenWindow long(hwnd;int);" & _
            "SetProgressValue long(hwnd;uint64;uint64);" & _
            "SetProgressState long(hwnd;int);" & _
            "RegisterTab long(hwnd;hwnd);" & _
            "UnregisterTab long(hwnd);" & _
            "SetTabOrder long(hwnd;hwnd);" & _
            "SetTabActive long(hwnd;hwnd;dword);" & _
            "ThumbBarAddButtons long(hwnd;uint;ptr);" & _
            "ThumbBarUpdateButtons long(hwnd;uint;ptr);" & _
            "ThumbBarSetImageList long(hwnd;ptr);" & _
            "SetOverlayIcon long(hwnd;ptr;wstr);" & _
            "SetThumbnailTooltip long(hwnd;wstr);" & _
            "SetThumbnailClip long(hwnd;ptr);"

    Local $pTask
    _AutoItObject_CoCreateInstance(DllStructGetPtr($CLSID_TaskBarlist), 0, 1, DllStructGetPtr($IID_ITaskbarList3), $pTask)
    If @error Then Return SetError(1, 0, 0)

    Local $oTaskBar = _AutoItObject_WrapperCreate($pTask, $tagInterface)
    If Not IsObj($oTaskBar) Then Return SetError(2, 0, 0)

    $oTaskBar.HrInit()

    Return $oTaskBar

EndFunc   ;==>__GetTaskBarObj

Func __UpdateMask($iIndex)
    $aButtons[$iIndex][$g_iMask] = $THB_FLAGS
    If $aButtons[$iIndex][$g_hIcon] <> 0 Then $aButtons[$iIndex][$g_iMask] = BitOR($aButtons[$iIndex][$g_iMask], $THB_ICON)
    If $aButtons[$iIndex][$g_iBitmap] <> -1 Then $aButtons[$iIndex][$g_iMask] = BitOR($aButtons[$iIndex][$g_iMask], $THB_BITMAP)
    If $aButtons[$iIndex][$g_sToolTip] <> '' Then $aButtons[$iIndex][$g_iMask] = BitOR($aButtons[$iIndex][$g_iMask], $THB_TOOLTIP)
EndFunc   ;==>__UpdateMask

Func __WM_Command($hWnd, $msg, $wParam, $lParam)

    Local $iMsg = _WinAPI_HiWord($wParam)

    If $iMsg = $THBN_CLICKED Then
        Local $iID = _WinAPI_LoWord($wParam)
        For $i = 1 To $aButtons[0][0]
            If $iID = $aButtons[$i][$g_iID] Then
                If $aButtons[$i][$g_sCallFunc] = -1 Then ExitLoop
                Call($aButtons[$i][$g_sCallFunc])
            EndIf
        Next
    EndIf

    Return $aButtons[0][$g_vWMCommand_Ret];$GUI_RUNDEFMSG

EndFunc   ;==>__WM_Command

Func __ThumbExit()
    $aButtons[0][$g_oTaskBar] = 0
    _AutoItObject_Shutdown()
EndFunc   ;==>__ThumbExit

Func __ErrFunc()
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>__ErrFunc
#endregion Internel Functions
;