﻿;-- TIME_STAMP   2019-07-26 09:32:14

; created  by @BugFix
; modified by @Bitnugger

; setzt Status des Toolbar-Button <SAVE> abhängig von Erfordernis zu speichern oder nicht auf ENABLE/DISABLE
; Skript kompilieren
; mit Autostart-Eintrag starten
; läuft ständig im Hintergrund, kann mit '"Skript\Pfad" /Exit' beendet werden


#NoTrayIcon

#include <Misc.au3>
#include <WinAPISys.au3>
#include <GuiToolbar.au3>

Opt('MustDeclareVars', 1)

If Not @Compiled Then Exit 1

Global $g_hUSER32 = DllOpen('user32.dll')

; Index des gewünschten Button ermitteln
; Die Trennstriche zählen mit! Gezählt wird bei 0 beginnend.
; Bsp. Button "Save": Davor liegt "Trenner", "NEW", "OPEN" --> "SAVE" ist das 4.te Element - somit Index "3"
Global Const $g_iIndexSave = 3

If $CMDLINE[0] And $CMDLINE[1] = '/Exit' Then _SciTE_EnDisable_SaveBtn_Exit()

_Singleton('SciTE_EnDisable_SaveBtn') ; Enforce a design paradigm where only one instance of the script may be running


While Sleep(500)
    _CheckAndSetState()
WEnd
Exit



Func _CheckAndSetState()
    Local $hScite, $sTitle, $iBtnState, $iNewState, $hWndToolbar, $idCmd, $aResult

    $hScite = WinGetHandle('[ACTIVE]')
    $aResult = DllCall($g_hUSER32, "int", "GetClassNameW", "hwnd", $hScite, "wstr", "", "int", 4096)
    If Not @error And $aResult[2] = 'SciTEWindow' Then
        $hWndToolbar = ControlGetHandle($hScite, '', '[CLASS:ToolbarWindow32; INSTANCE:1]')
        $aResult = DllCall($g_hUSER32, "bool", "IsWindowVisible", "hwnd", $hWndToolbar)
        If Not @error And $aResult[0] Then
            $idCmd = _GUICtrlToolbar_IndexToCommand($hWndToolbar, $g_iIndexSave)
            $sTitle = WinGetTitle($hScite)
            If StringRegExp($sTitle, 'SciTE( \[\d+ (of|von) \d+\])?$') Then
                $iBtnState = _GUICtrlToolbar_GetButtonState($hWndToolbar, $idCmd)
                If Not StringInStr($sTitle, '*') And BitAND($iBtnState, $TBSTATE_ENABLED) Then
                    $iNewState = BitXOR($iBtnState, $TBSTATE_ENABLED)
                ElseIf StringInStr($sTitle, '*') And Not BitAND($iBtnState, $TBSTATE_ENABLED) Then
                    $iNewState = BitOR($iBtnState, $TBSTATE_ENABLED)
                Else
                    Return
                EndIf
                _GUICtrlToolbar_SetButtonState($hWndToolbar, $idCmd, $iNewState)
            EndIf
        EndIf
    EndIf
EndFunc

Func _SciTE_EnDisable_SaveBtn_Exit()
	Local $aWinList = WinList('[CLASS:SciTEWindow]'), $hWndToolbar, $idCmd, $iBtnState, $iNewState, $aProcessList = ProcessList(@ScriptName)
	For $i = 1 To $aProcessList[0][0] Step 1
		If $aProcessList[$i][1] <> @AutoItPID Then ProcessClose($aProcessList[$i][1])
	Next
	For $i = 1 To $aWinList[0][0] Step 1
		$hWndToolbar = ControlGetHandle($aWinList[$i][1], '', '[CLASS:ToolbarWindow32; INSTANCE:1]')
		$idCmd = _GUICtrlToolbar_IndexToCommand($hWndToolbar, $g_iIndexSave)
		$iBtnState = _GUICtrlToolbar_GetButtonState($hWndToolbar, $idCmd)
		$iNewState = BitOR($iBtnState, $TBSTATE_ENABLED)
		If $iNewState <> $iBtnState Then _GUICtrlToolbar_SetButtonState($hWndToolbar, $idCmd, $iNewState)
	Next
    DllClose($g_hUSER32)
    Exit
EndFunc
