Jump to content

Recommended Posts

Posted (edited)

Bookmarking it here, .. will get useful at some point ..

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>

Local $sFilePath = @ScriptDir & "\Presets.txt", $checked, $aArray
;~ FileDelete($sFilePath)

Local $aFile[0][2]
If FileExists($sFilePath) Then
    _FileReadToArray($sFilePath, $aFile, $FRTA_NOCOUNT, "|")
Else

    Local $aFile[0][2]

    Local $sPreset = 'Preset_1|4,1,9,17,25' & @LF & _
            'Another1|4,2,10,18,26' & @LF & _
            '3|4,3,11,19,27' & @LF & _
            'Type_it_Here_And_Save|11,3,11,19,25,26,27,28,29,30,31,32' & @LF & _
            'Recommended|32,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32'

    Local $aVal = StringSplit($sPreset, @LF, 2)
    Local $aFile[UBound($aVal)][2]
    For $i = 0 To UBound($aVal) - 1
        $asplit = StringSplit($aVal[$i], "|")
        $aFile[$i][0] = $asplit[1]
        $aFile[$i][1] = $asplit[2]
    Next
EndIf

Local $hGUI = GUICreate("Example")
Local $Paste_Button = GUICtrlCreateButton("Paste", 5, 10, 45, 25)
Local $Clear_Button = GUICtrlCreateButton("Clear", 50, 10, 45, 25)
Local $Save_Button = GUICtrlCreateButton("Save", 340, 50, 45, 25)

Local $ArrayDisplay = GUICtrlCreateButton("Display", 340, 10, 45, 25)
Local $Delete_Button = GUICtrlCreateButton("Delete", 290, 10, 45, 25)

Local $idCombo = GUICtrlCreateCombo("", 100, 10, 180, 20), $iIndex

$sCombo = ""

For $i = 0 To UBound($aFile) - 1
    $sCombo &= $aFile[$i][0] & "|"
    Assign($aFile[$i][0], StringSplit($aFile[$i][1], ",", 2))
Next

GUICtrlSetData($idCombo, $sCombo)
_GUICtrlComboBox_SetCurSel($idCombo, 0)

Global $Eval = $idCombo, $istart = $Eval + 1, $iEnd = $Eval + 32


Local $aString[3], $randStr, $x = 30, $y = 20
For $i = 1 To 32
    If $i = 9 Or $i = 17 Or $i = 25 Then
        $x += 80
        $y = 20
    EndIf
    $y += 40
    $randStr = ""
    For $j = 1 To 5
        $aString[0] = Chr(Random(65, 90, 1))     ;A-Z
        $aString[1] = Chr(Random(97, 122, 1))    ;a-z
        $aString[2] = Random(0, 9, 1)            ;0-9
        $randStr &= $aString[Random(0, 2, 1)]
    Next

    Assign("check_" & $i, GUICtrlCreateCheckbox($randStr, $x, $y, 60, 20))
    DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle(-1), "wstr", 0, "wstr", 0)
    GUICtrlSetColor(-1, 0x6b964f)
Next

GUISetState(@SW_SHOW, $hGUI)

If GUICtrlRead($idCombo) Then
    $aArray = Eval(GUICtrlRead($idCombo))
    _Set($aArray)
EndIf

While 1
    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE
            ExitLoop

        Case $ArrayDisplay
            _ArraySort($aFile)
            _ArrayDisplay($aFile)

        Case $Paste_Button ; the Combo of the Last saved \ updated preset
            $aArray = StringSplit($checked, ",", 3)
            _Set($aArray)

        Case $Clear_Button
            _Set($aArray, False)

        Case $Delete_Button
            $i = _ArraySearch($aFile, GUICtrlRead($idCombo), Default, Default, 2)
            If Not @error Then
                _ArrayDelete($aFile, $i)
                _GUICtrlComboBox_DeleteString($idCombo, _GUICtrlComboBox_FindStringExact($idCombo, GUICtrlRead($idCombo)))
                If GUICtrlRead($idCombo) = "" Then
                    _GUICtrlComboBox_SetCurSel($idCombo, $i - 1)
                    If GUICtrlRead($idCombo) = "" Then _GUICtrlComboBox_SetCurSel($idCombo, _GUICtrlComboBox_GetTopIndex($idCombo))
                Else
                    _GUICtrlComboBox_SetEditText($idCombo, "")
                    ContinueLoop
                EndIf
            EndIf
            ContinueCase

        Case $idCombo
            $aArray = Eval(GUICtrlRead($idCombo))
            If Not IsArray($aArray) Then ContinueLoop
            _Set($aArray)

        Case $Save_Button
            If GUICtrlRead($idCombo) = "" Then
                MsgBox(262144, "Preset Name ?", "First Add a title for this Preset", 3)
                ContinueLoop
            EndIf

            $checked = ""
            For $i = 1 To 32
                If _IsChecked($Eval + $i) Then
                    $checked &= $i & ","
                EndIf
            Next

            If $checked = "" Then ContinueLoop
            $checked = StringTrimRight($checked, 1)
            $a = StringSplit($checked, ",")
            $checked = $a[0] & "," & $checked
            $sRead = StringReplace(GUICtrlRead($idCombo), " ", "_")

            $Test = 0
            $i = _ArraySearch($aFile, $checked, Default, Default, Default, Default, 1)
            If Not @error Then
                If $aFile[$i][0] <> $sRead Then
                    $Test += 1
                    $iIndex = _ArraySearch($aFile, $sRead)
                    If Not @error Then $Test += 1
                EndIf
            EndIf

            Switch $Test
                Case 0
                    $i = _ArraySearch($aFile, $sRead)
                    If $i = -1 Then
                        Local $aArray_Base[1][2] = [[$sRead, $checked]]
                        $i = _ArrayAdd($aFile, $aArray_Base, "|")
                        GUICtrlSetData($idCombo, $sRead)
                    Else
                        $aFile[$i][1] = $checked
                    EndIf

                    Assign($sRead, $a)
                    _Set($a)
                    ContinueLoop
                Case 1
                    If MsgBox(36, "", 'This combo already exists as preset "' & $aFile[$i][0] & '"' & @CRLF & 'Change and replace the preset name to "' & $sRead & '" ?') <> 6 Then
                        ContinueLoop
                    EndIf

                Case 2
                    If MsgBox(36, "", 'The Same combo of checks exist in the preset named "' & $aFile[$i][0] & '"' & @CRLF & " + " & @CRLF & _
                            'The TItle "' & $sRead & '" is used by another saved preset ..' & @CRLF & @CRLF & _
                            'Replace both presets with the current ?') <> 6 Then ContinueLoop
            EndSwitch

            $j = _GUICtrlComboBox_FindStringExact($idCombo, $aFile[$i][0])
            $aFile[$j][0] = $sRead
            Assign($aFile[$i][0], $a)

            If $Test = 2 Then _ArrayDelete($aFile, $iIndex)

            _ArraySort($aFile)
            _GUICtrlComboBox_ResetContent($idCombo)

            $sCombo = ""
            For $j = 0 To UBound($aFile) - 1
                $sCombo &= $aFile[$j][0] & "|"
            Next

            GUICtrlSetData($idCombo, $sCombo)
            _GUICtrlComboBox_SelectString($idCombo, $sRead)

            _Set($a)
    EndSwitch
WEnd

_FileWriteFromArray($sFilePath, $aFile, 0, Default, "|")
If @error Then
    If MsgBox(262144 + 36, "Exiting", "Delete " & $sFilePath & " ?") = 6 Then FileDelete($sFilePath)
EndIf

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

Func _Set(ByRef $arr, $bCheck = True)

    For $j = $istart To $iEnd
        GUICtrlSetColor($j, 0x6b964f)
        GUICtrlSetState($j, 4)
    Next

    If $bCheck = False Then Return

    For $j = 1 To $arr[0]
        GUICtrlSetColor($Eval + $arr[$j], 0xe70f46)
        GUICtrlSetState($Eval + $arr[$j], 1)
    Next
EndFunc   ;==>_Set

 

Edited by Deye
Changed and Compacted

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...