Attribute VB_Name = "mod_VoiceControl"
Option Explicit

Public Const conHwndTopmost = -1
Public Const conHwndTopNot = -2
Public Const conSwpNoActivate = &H10
Public Const conSwpShowWindow = &H40
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2
Public Const GWL_EXSTYLE = (-20)
Public Const GWL_HWNDPARENT = (-8)
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const WS_BORDER = &H800000
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_VISIBLE = &H10000000


Public iniValue As Variant
Public PaddStart As Date
Public PaddEnd As Date
Public strApplication As String
Public strProgram As String
Public strProgramdir As String
Public strThemedir As String

Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Public Function Activate_Application() As Long
    Dim lngStyle As Long
    Dim Addcounter As Integer
    Dim tempcheck As String
    Dim currWnd As Long
    Dim Length As Long
    Dim listItem$
    Dim IsTask As Long
    Dim Counting As Long
    
    Activate_Application = -1
    IsTask = WS_VISIBLE Or WS_BORDER
    
    currWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)
    While currWnd <> 0
        If IsWindowVisible(currWnd) Then
            If GetParent(currWnd) = 0 Then
                lngStyle = GetWindowLong(currWnd, GWL_HWNDPARENT)
                If (lngStyle = 0) Or IsToolWindow(lngStyle) Then
                    If Not IsToolWindow(currWnd) Then
                        Length = GetWindowTextLength(currWnd)
                        listItem$ = Space$(Length + 1)
                        Length = GetWindowText(currWnd, listItem$, Length + 1)
                        If (Left(listItem$, Length) = strApplication) Then
                            Activate_Application = currWnd
                       End If
                    End If
                End If
            End If
        End If
        currWnd = GetWindow(currWnd, GW_HWNDNEXT)
    Wend
End Function

Public Function INI_GetKey(SectionName As String, KeyName As String, IniFileName As String) As Boolean
    Dim ReturnString As String
    Dim ReturnValue As Long
    
    On Local Error GoTo GetKey_Err
    ReturnString = String$(32000, 0)
    iniValue = ""
    ReturnValue = GetPrivateProfileString(SectionName & Chr(0), KeyName, "NotFound", ReturnString, Len(ReturnString), IniFileName & Chr(0))
    If Left(ReturnString, InStr(ReturnString, Chr$(0)) - 1) = "NotFound" Then
        INI_GetKey = False
    Else
        INI_GetKey = True
        iniValue = Left(ReturnString, InStr(ReturnString, Chr$(0)) - 1)
    End If

GetKey_End:
    Exit Function

GetKey_Err:
    MsgBox "Error Retrieving Key"
    INI_GetKey = False
    Resume GetKey_End
End Function

Public Function IsToolWindow(ByVal hwnd As Long) As Boolean
   ' Check for toolwindow style bit.
   IsToolWindow = ((GetWindowLong(hwnd, GWL_EXSTYLE) And WS_EX_TOOLWINDOW) = WS_EX_TOOLWINDOW)
End Function

Public Sub VoiceControl_Load()
    Dim lngLoop As Long
    Dim strFile As String
    Dim myCounter As Long
    Dim strID As String
    Dim strSection As String
    Dim myKomma As Long
    Dim strTemp As String
    Dim strLine As String
    
    frmMain.File1.Pattern = "*.skn"
    frmMain.File1.Path = strProgramdir & Left(strThemedir, Len(strThemedir) - 1)
    
    frmMain.lstBoxWords.Clear
    For lngLoop = 0 To frmMain.File1.ListCount - 1
        strFile = strProgramdir & strThemedir & frmMain.File1.List(lngLoop)
        myCounter = 0
        'frmMain.lstBox(1).AddItem frmMain.File1.List(lngLoop)
        While INI_GetKey("SECURITY", Format(myCounter + 1, "00"), strFile) = True
            strID = Left(iniValue, 2)
            strLine = Mid(iniValue, 4)
            myKomma = InStr(strLine, " ")
            strSection = Left(strLine, myKomma - 1)
            strTemp = Mid(strFile, 1, Len(strFile) - 3)
            
            INI_GetKey Mid(frmMain.File1.List(lngLoop), 1, Len(frmMain.File1.List(lngLoop)) - 4), strID, strProgramdir & strThemedir & "voicecontrol.ini"
            If iniValue = "" Then
                INI_GetKey Mid(frmMain.File1.List(lngLoop), 1, Len(frmMain.File1.List(lngLoop)) - 4), strID, strProgramdir & strThemedir & "voicecontrol.master"
            End If
            If (iniValue <> "") Then
                frmMain.lstBoxWords.AddItem iniValue
                INI_GetKey strSection, strID, strTemp & "ini"
                If iniValue = "" Then
                    INI_GetKey strSection, strID, strTemp & "skn"
                End If
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                myKomma = InStr(iniValue, ",")
                iniValue = Mid(iniValue, myKomma + 1)
                frmMain.lstBoxActions.AddItem Mid(frmMain.File1.List(lngLoop), 1, Len(frmMain.File1.List(lngLoop)) - 4) & " " & iniValue
            End If
            'frmMain.lstBoxActions.AddItem "  " & strLine
            'frmMain.lstBoxActions.ItemData(frmMain.lstBoxActions.NewIndex) = strID
            myCounter = myCounter + 1
        Wend
    Next
    strTemp = strProgramdir & "\voice-words.txt"
    Open strTemp For Output As #1
        strLine = "[Grammer]" & vbCrLf & "type=cfg" & vbCrLf & "[<start>]" & vbCrLf
        frmMain.lstBoxWords.ListIndex = -1
        For lngLoop = 0 To frmMain.lstBoxWords.ListCount
            If frmMain.lstBoxWords.List(lngLoop) <> "" Then
                strLine = strLine & "<start>=" & frmMain.lstBoxWords.List(lngLoop) & vbCrLf
            End If
        Next lngLoop
        Print #1, strLine
    Close #1
    frmMain.DirectSR1.Deactivate
    frmMain.DirectSR1.GrammarFromFile (strTemp)
    frmMain.DirectSR1.Activate
End Sub
