Attribute VB_Name = "modul_Calaym_MessageDialogs"
' Copyright (C) 2006-2011 Jan Vorel
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

Public GenericMessageHandler_Param As Long
'Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public UnloadLayerOnDialog As Boolean
Private InfoDialogIcon As Long

Private Sub InfoRealInfo(ByVal hWnd As Long)
  Dim xLong1 As Long
  Dim xISnd As String
  Dim xILng As String
  Dim xIThm As String
  
  xLong1 = ctSSLayer_CanUseItem(&H20&)
  If xLong1 > -1& And xLong1 < 90& Then
    If xLong1 < 10& Or xLong1 > 19& Then xLong1 = 9&
    xISnd = Replace(SSRestrictions, "$I", Replace(Replace(LoadResEx_String(2100& + xLong1), "$S", ctSSLayer_GetInfoString(1&)), "$V", ctSSLayer_GetInfoString(&H30&)))
'    xISnd = Replace(LoadResEx_String(2205&), "$P", xISnd)
    If Not (Len(LayerHP) = 0& Or Len(xISnd) = 0&) Then
      xISnd = xISnd + vbCrLf + Replace(LoadResEx_String(2205&), "$H", LayerHP)
      End If
    End If
  If Len(xISnd) = 0& Then
    xISnd = LoadResEx_String(2220&)
    End If
  'Call SetDlgItemText(HWndDialog, 2040&, Replace(GetDlgItemText(HWndDialog, 2040&), "$", LayerHP))
  'End If
  
  If (LoadResEx_LanguageFile_hModule = 0& Or LoadResEx_LanguageFile_hModule = App.hInstance) Then
    xILng = LoadResEx_String(2221&)
    Else
    xILng = LoadResEx_StringEx(LoadResEx_LanguageFile_hModule, 2&)
    If Len(xILng) = 0& Then xILng = LoadResEx_String(2220&)
    End If
  
  If (LoadResEx_ThemeFile_hModule = 0& Or LoadResEx_ThemeFile_hModule = App.hInstance) Then
    xIThm = LoadResEx_String(2222&)
    Else
    xIThm = LoadResEx_StringEx(LoadResEx_ThemeFile_hModule, 2&)
    If Len(xIThm) = 0& Then xIThm = LoadResEx_String(2220&)
    End If
  
  HyperlinkList_AddItem hWnd, GBN2 + vbCr + LoadResEx_String(2200&), , InfoDialogIcon, 32&
  'HyperlinkList_AddItem hWnd, vbCrLf + Replace(Replace(Replace(LoadResEx_String(2210&), "$S", vbCrLf + xISnd), "$L", xILng), "$T", xIThm)
  HyperlinkList_AddItem hWnd, vbCrLf + LoadResEx_String(2210&)
  HyperlinkList_AddItem hWnd, vbCrLf + Replace(LoadResEx_String(2214&), "$S", vbCrLf + xISnd)
  HyperlinkList_AddItem hWnd, vbCrLf + Replace(LoadResEx_String(2215&), "$L", xILng)
  HyperlinkList_AddItem hWnd, vbCrLf + Replace(LoadResEx_String(2216&), "$T", xIThm)
  HyperlinkList_AddItem hWnd, vbCrLf + LoadResEx_String(2219&)
  HyperlinkList_AddingDone hWnd
  End Sub

Private Sub InfoDialogURLOpener(ByVal HWndDialog As Long, ByVal hWnd As Long)
  Dim xLong1 As Long
  Dim xChar1 As String

  If UrlsAllowed And Not hWnd = 0& Then
    xLong1 = SendMessageA(hWnd, &H19F&, 0&, ByVal 0&)
    If xLong1 >= 0& Then
      xChar1 = HyperlinkList_GetURLFromItem(hWnd, xLong1)
      If Not Len(xChar1) = 0& Then
        'If UrlsAllowed Then
          'Debug.Print xChar1
          Call ShellExecute(HWndDialog, "Open", xChar1, "", "", 5&)
          'Clipboard.Clear
          'Clipboard.SetText xChar1, Text
          'End If
        End If
      End If
    End If
  End Sub

Public Sub ShowInitInfo()
  GenericMessageHandler_Param = 2102&
  DialogTimer True
  Call LoadResEx_Dialog(MainHWnd, 2102&, 2090&, 2091&, AddressOf GenericMessageHandler)
  DialogTimer False
  End Sub

Public Sub ShowInfo()
  DialogTimer True
  #If InIDE Then
    Call DialogBoxIndirectParam(2200&, MainHWnd, AddressOf InfoDialogProc, 0&, LoadResEx_ThemeFile_hModule)
    #Else
    Call DialogBoxIndirectParam(2200&, MainHWnd, AddressOf InfoDialogProc, 0&)
    #End If
  DialogTimer False
  End Sub

Public Function InfoDialogProc(ByVal HWndDialog As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Msg
    Case &H2B& ' WM_DRAWITEM
      InfoDialogProc = HyperlinkList_WM_DRAWITEM(lParam)
      Exit Function



    Case &H110& 'WM_INITDIALOG
      Msg = GetDlgItem(HWndDialog, 2209&)
      If Not Msg = 0& Then
        Call SetFocusAPI(Msg)
        InfoDialogProc = 0&
        Else
        InfoDialogProc = 1&
        End If
      Msg = GetDlgItem(HWndDialog, 2200&)
      If Not Msg = 0& Then
        Call HookWindow(Msg, AddressOf HyperlinkList_Proc)
        InfoDialogIcon = LoadResEx_Icon(2000&)
'Debug.Print InfoDialogIcon
        Call InfoRealInfo(Msg)
        End If
      Exit Function
    Case &H111& 'WM_COMMAND
'Debug.Print Hex(wParam), lParam
      If lParam = 0& Then
        If wParam = 2& Then
          Call EndDialog(HWndDialog, 0&)
          End If
        ElseIf (GetWindowLong(lParam, -16&) And &HE&) = 0& Then
        If (wParam And &HFFFF&) = 2200& Then
          If (wParam And &HFFFF0000) = &H20000 Then
            Call InfoDialogURLOpener(HWndDialog, lParam)
            End If
          Else
          Call EndDialog(HWndDialog, 0&)
          End If
        End If
    Case &H10& 'WM_CLOSE
      Call EndDialog(HWndDialog, 0&)
    Case &H2& 'WM_DESTROY
      Msg = GetDlgItem(HWndDialog, 2200&)
      If Not Msg = 0& Then
        Call UnhookWindow(Msg)
        End If
      If Not InfoDialogIcon = 0& Then
        Call DestroyIcon(InfoDialogIcon)
        InfoDialogIcon = 0&
        End If
    
    
    
    Case &H2C& ' WM_MEASUREITEM
      InfoDialogProc = HyperlinkList_WM_MEASUREITEM(lParam)
      Exit Function
    Case &H2D& ' WM_DELETEITEM
      InfoDialogProc = HyperlinkList_WM_DELETEITEM(lParam)
      Exit Function
    End Select
  'InfoDialogProc = HookedWindowProc(hwnd, Msg, wParam, lParam)
  End Function



Public Function GenericMessageHandler(ByVal HWndDialog As Long, ByVal Msg As Long, ByVal ControlID As Long, ByVal ControlHWnd As Long) As Long
  'Dim xLong1 As Long
  'Static xWinIcon As Long
  'Dim xChar1 As String

  If Msg = &H110& Then
    'Call SetDlgItemTextA(HWndDialog, 2000&, LoadResEx_String(GenericMessageHandler_Param, True))
    LoadResEx_Dialog_Enable HWndDialog, 2095&, FileExists(xRealBaseFile + "hlp")
    'Msg = GetDlgItem(HWndDialog, 2095&)
    'If Not Msg = 0& Then
    '  If FileExists(xRealBaseFile+"hlp") Then
    '    Call EnableWindow(Msg, 1&)
    '    Else
    '    Call EnableWindow(Msg, 0&)
    '    End If
    '  End If
    'Msg = GetDlgItem(HWndDialog, 2010&)
    
    If GenericMessageHandler_Param > 2099 And GenericMessageHandler_Param < 2299& Or GenericMessageHandler_Param = 2090& Or GenericMessageHandler_Param = 2070& Or GenericMessageHandler_Param = 2071& Then
      Msg = ctSSLayer_CanUseItem(&H20&)
      If Msg > -1& And Msg < 90& Then
        If Msg < 10& Or Msg > 19& Then Msg = 9&
        Call SetDlgItemText(HWndDialog, 2080&, Replace(SSRestrictions, "$I", Replace(Replace(LoadResEx_String(2100& + Msg), "$S", ctSSLayer_GetInfoString(1&)), "$V", ctSSLayer_GetInfoString(&H30&))))
        End If
      Call SetDlgItemText(HWndDialog, 2040&, Replace(GetDlgItemText(HWndDialog, 2040&), "$", LayerHP))
      End If
    Select Case GenericMessageHandler_Param
      Case 2070&, 2071&
        Call SetDlgItemText(HWndDialog, 2000&, LoadResEx_String(GenericMessageHandler_Param))
        GenericMessageHandler_Param = 2090&
        AskSndResetButton = GetDlgItem(HWndDialog, 2090&)
        AskSndReset = GetDlgItem(HWndDialog, 2099&)
      Case 2050& To 2052&
        Call SetDlgItemText(HWndDialog, 2000&, Replace(LoadResEx_String(GenericMessageHandler_Param), "$", SimpleFile(xRealBaseFile + "lay")))
      Case 2100&, 2130& To 2132&
        Call SetDlgItemText(HWndDialog, 2000&, Replace(LoadResEx_String(GenericMessageHandler_Param), "$", ctSSLayer_GetInfoString(2&)))
      Case 2101
        If Not GlobalXInitRun Then
          LoadResEx_Dialog_Enable HWndDialog, 2091&, False
          'Msg = GetDlgItem(HWndDialog, 2091&)
          'If Not Msg = 0& Then Call EnableWindow(Msg, 0&)
          End If
        Call ctSSLayer_RefreshDeviceList(MainHWnd)
        ControlHWnd = ctSSLayer_GetDeviceFromSSLID(xOutputSSLID)
        If ControlHWnd = -1& Then ControlHWnd = 0&
        Call SetDlgItemText(HWndDialog, 2040&, GetDeviceName(ControlHWnd))
        Call SetDlgItemText(HWndDialog, 2000&, Replace(LoadResEx_String(GenericMessageHandler_Param), "$", ctSSLayer_GetInfoString(2&)))
      Case 2130& To 2132&
        Call SetDlgItemText(HWndDialog, 2000&, Replace(LoadResEx_String(GenericMessageHandler_Param), "$", ctSSLayer_GetInfoString(2&)))
      Case 2190& 'To 2199
        Call SetDlgItemText(HWndDialog, 2030&, CheckDlls(ctSSLayer_GetInfoString(&H31&)))
      Case 2102&
        If LCase(ctSSLayer_GetInfoString(4&)) = xNoVerWarnDLL Then
          LoadResEx_Dialog_Enable HWndDialog, 2260&, False
          LoadResEx_Dialog_CheckBool HWndDialog, 2260&, True, False
          'chkres(2260).Value = 1
          ElseIf ctSSLayer_CanUseItem(&H20&) = 0& Or Not xCmdSaveSet Then
          LoadResEx_Dialog_Enable HWndDialog, 2260&, False
          End If
      End Select
    
    If Not UrlsAllowed Then
      ControlHWnd = GetDlgItem(HWndDialog, 2050&) ' Visit me
      If Not ControlHWnd = 0& Then Call EnableWindow(ControlHWnd, 0&)
      ControlHWnd = GetDlgItem(HWndDialog, 2051&) ' Visit ms (for dx)
      If Not ControlHWnd = 0& Then Call EnableWindow(ControlHWnd, 0&)
      ControlHWnd = GetDlgItem(HWndDialog, 2060&) ' Visit sound system
      If Not ControlHWnd = 0& Then Call EnableWindow(ControlHWnd, 0&)
      End If
    If GenericMessageHandler_Param = 2090& Or GenericMessageHandler_Param = 2101& Then
      MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
      Call UpdateSnds(HWndDialog, 2040&)
      If Not HWndSetupDialogSSPage = 0& Then UpdateSnds HWndSetupDialogSSPage, 3200&
      If ctSSLayer_CanUseItem(&HE&) = 0& Then LoadResEx_Dialog_Enable HWndDialog, 2041&, False
      DialogTimer True
      End If
    If UnloadLayerOnDialog Then ctSSLayer_UnLoadVB: UnloadLayerOnDialog = False
    ElseIf Msg = &H12& Then
    If GenericMessageHandler_Param = 2090& Or GenericMessageHandler_Param = 2101& Then
      DialogTimer False
      Call FetchSSLIDFromComboBoxAndCheckItHasChanged(HWndDialog, 2040&)
      MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
      If Not HWndSetupDialogSSPage = 0& Then UpdateSnds HWndSetupDialogSSPage, 3200&
      If GenericMessageHandler_Param = 2090& Then
        If Not IsDlgButtonChecked(HWndDialog, 2045&) = 0& Then
          xNoSpektrumOnStartup = True
          CheckAccurate = False
          xDeluxeSpectrum = False
          If LockWinUpd = 0& Then LockWinUpd = 1&
          End If
        End If
      ElseIf GenericMessageHandler_Param = 2102& Then
      checkVerBox HWndDialog
      End If
    
    
    
    
    ElseIf Msg = &H111& Then
    Select Case (ControlID And &HFFFF&)
      'Case 2045&, 2260&
      '  Call CheckDlgButton(HWndDialog, ControlID, 1& - IsDlgButtonChecked(HWndDialog, ControlID))
      Case 2050& ' Visit me
        If UrlsAllowed Then
          Call ShellExecute(HWndDialog, "Open", "http://www.ctuser.net", "", "", 5&)
          End If
      Case 2041&
        MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
        Call UpdateSnds(HWndDialog, 2040&)
        If Not HWndSetupDialogSSPage = 0& Then UpdateSnds HWndSetupDialogSSPage, 3200&
      Case 2051& ' Visit ms (for dx)
        If UrlsAllowed Then
          Call ShellExecute(HWndDialog, "Open", LoadResEx_String(2120&), "", "", 5&)
          End If
      Case 2060& ' Visit sound system
        If UrlsAllowed Then
          Call ShellExecute(HWndDialog, "Open", LayerHP, "", "", 5&)
          End If
      Case 2095& ' Help!
        Call WinHelp(MainHWnd, xRealBaseFile + "hlp", 1&, 22&)
      Case Else
        'Debug.Print ControlID, Hex(ControlID)
        GenericMessageHandler = 1&
      End Select
    If GenericMessageHandler_Param = 2090& Then
      If ControlID = 2045& Or (ControlID And &HFFFF&) = 2040& And (ControlID And &HFFFF0000) = &H10000 Then
    '  'If ControlID < &H8000& Then
        If Not xAutoIgnoreTime = -1& Then
          xAutoIgnoreTime = -1&
          If Not AskSndReset = 0& Then
            Call SetWindowText(AskSndReset, "")
            End If
          End If
        End If
      If Not HWndSetupDialogSSPage = 0& Then
        Call FetchSSLIDFromComboBoxAndCheckItHasChanged(HWndDialog, 2040&)
        MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
        UpdateSnds HWndSetupDialogSSPage, 3200&
        End If
      End If
    End If
  End Function

Private Function CheckDlls(ByVal xChar1 As String) As String
  Dim xLong1 As Long
  Dim xChar2 As String
  
  xChar1 = LCase(Replace(xChar1, ",", ";")) + ";"
  Do
    xLong1 = InStr(xChar1, ";")
    If xLong1 = 0& Then
      Exit Do
      Else
      xChar2 = Trim(Left(xChar1, xLong1 - 1&))
      xChar1 = Mid(xChar1, xLong1 + 1&)
      If Not Len(xChar2) = 0& Then
        xLong1 = LoadLibrary(xChar2)
        If xLong1 = 0& Then
          CheckDlls = CheckDlls + ", " + xChar2
          Else
          FreeLibrary (xLong1)
          End If
        End If
      End If
    Loop
  CheckDlls = Mid(CheckDlls, 3&)
  End Function

Private Sub checkVerBox(ByVal HWndDialog As Long)
  Dim xBool1 As Boolean

  LoadResEx_Dialog_CheckBool HWndDialog, 2260&, xBool1, True
  If xBool1 Then xNoVerWarnDLL = LCase(ctSSLayer_GetInfoString(4&))
  End Sub

Public Function FetchSSLIDFromComboBoxAndCheckItHasChanged(ByVal HWndDialog As Long, ByVal ControlID As Long) As Boolean
  Dim xChar1 As String

  ControlID = GetDlgItem(HWndDialog, ControlID)
  If Not ControlID = 0& Then
    ControlID = SendMessage(ControlID, &H147&, 0&, 0&)
    If Not ControlID = -1& Then
      xChar1 = ctSSLayer_GetDeviceSSLID(ControlID)
      If Not xChar1 = xOutputSSLID Then
        xOutputSSLID = xChar1
        FetchSSLIDFromComboBoxAndCheckItHasChanged = True
        End If
      End If
    End If
  End Function
'If Not HWndSetupDialogSSPage = 0& Then UpdateSnds HWndSetupDialogSSPage,3200&


Public Sub UpdateSnds(ByVal HWndDialog As Long, ByVal ControlID As Long)
  ControlID = GetDlgItem(HWndDialog, ControlID)
  If Not ControlID = 0& Then
    Call SendMessage(ControlID, CB_RESETCONTENT, 0&, 0&)
    For HWndDialog = 0& To MaxDeviceCount - 1&
      Call SendMessage_String(ControlID, &H143&, 0&, GetDeviceName(HWndDialog))
      Next
    HWndDialog = ctSSLayer_GetDeviceFromSSLID(xOutputSSLID)
    If Not HWndDialog = -1& Then
      Call EnableWindow(ControlID, -CLng(MaxDeviceCount > 1&))
      Call SendMessage(ControlID, &H14E&, HWndDialog, 0&)
      End If
    Call SendMessage(ControlID, &H155&, 1&, 0&)
    End If
  'xSkipMe = False
  End Sub

