Attribute VB_Name = "modul_Calaym_MessageDialogs"
' Calaym - Media player GUI for sound systems
' Copyright (C) 2002-2012 Jan Vorel
'
' You can redistribute the official software packages under the terms of the
' User-friendly Open-source License Version 1.00, published by Jan Vorel
'
' You can use this software under the terms of the
' User-friendly Software License Version 1.00, published by Jan Vorel
'
' You should have received copies of the licenses along with this program. If
' not, contact the author.
'
' If you want to use or distribute this software under a different license, ask
' the author.
'
' The software is provided "as is" without warrenty of any kind. The Licensor
' cannot be held responsible for any damage caused by using, extracting or
' installing the software.

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

Public Sub ShowInfo_Callback(ByVal hWnd As Long, ByVal hIcon As Long)
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xType1 As RECT

  xType1.Left = 4&
  xType1.Right = 4&
  xType1.Top = 4&
  xType1.Bottom = 4&

  HyperlinkList_AddItem hWnd, GBN2 + LoadResEx_String(2200&), xType1, hIcon, 32&
  
  HyperlinkList_AddItem hWnd, LoadResEx_String(2210&), xType1

  xLong1 = ctSSLayer_CanUseItem(&H20&)
  If xLong1 > -1& And xLong1 < 90& Then
'Debug.Print xLong1
    If xLong1 < 10& Or xLong1 > 19& Then xLong1 = 9&
    xChar1 = Replace(SSRestrictions, "$I", Replace(LoadResEx_String(2100& + xLong1), "$S", ctSSLayer_GetInfoString(5&)))
    If Not (Len(LayerHP) = 0& Or Len(xChar1) = 0&) Then
      xChar1 = xChar1 + Replace(LoadResEx_String(2205&), "$H", LayerHP)
      End If
    End If
  If Len(xChar1) = 0& Then
    xChar1 = LoadResEx_String(2220&)
    End If
  'Call SetDlgItemText(HWndDialog, 2040&, Replace(GetDlgItemText(HWndDialog, 2040&), "$", LayerHP))
  'End If
  'xChar2 = ctSSLayer_GetInfoString(&HA&)
  'If Not Len(xChar2) = 0& Then
  '  xChar1 = xChar1 + xChar2
  '  End If
  HyperlinkList_AddItem hWnd, Replace(LoadResEx_String(2214&), "$S", vbCrLf + xChar1), xType1
  xChar1 = ctSSLayer_GetInfoString(&HA&)
  xLong1 = InStr(xChar1, vbCrLf)
  xType1.Left = 20&
  While xLong1 > 0&
    xChar2 = Trim(Left(xChar1, xLong1 - 1&))
    If Not Len(xChar2) = 0& Then
      Call HyperlinkList_AddItem(hWnd, xChar2, xType1)
      End If
    xChar1 = Mid(xChar1, xLong1 + 2&)
    xLong1 = InStr(xChar1, vbCrLf)
    Wend
  xChar2 = Trim(xChar1)
  If Not Len(xChar2) = 0& Then
    Call HyperlinkList_AddItem(hWnd, xChar1, xType1)
    End If
  xType1.Left = 4&

  If (LoadResEx_LanguageFile_hModule = 0& Or LoadResEx_LanguageFile_hModule = App.hInstance) Then
    xChar1 = LoadResEx_String(2221&)
    Else
    xChar1 = LoadResEx_StringEx(LoadResEx_LanguageFile_hModule, 2&)
    If Len(xChar1) = 0& Then xChar1 = LoadResEx_String(2220&)
    End If
  HyperlinkList_AddItem hWnd, Replace(LoadResEx_String(2215&), "$L", xChar1), xType1
  
  If (LoadResEx_ThemeFile_hModule = 0& Or LoadResEx_ThemeFile_hModule = App.hInstance) Then
    xChar1 = LoadResEx_String(2222&)
    Else
    xChar1 = LoadResEx_StringEx(LoadResEx_ThemeFile_hModule, 2&)
    If Len(xChar1) = 0& Then xChar1 = LoadResEx_String(2220&)
    End If
  'HyperlinkList_AddItem hWnd,  Replace(Replace(Replace(LoadResEx_String(2210&), "$S", vbCrLf + xChar1), "$L", xChar1), "$T", xchar1),xtype1
  HyperlinkList_AddItem hWnd, Replace(LoadResEx_String(2216&), "$T", xChar1), xType1
  
  xType1.Bottom = 4&
  HyperlinkList_AddItem hWnd, LoadResEx_String(2219&), xType1
  End Sub

Public Sub ShowInfo_OpenURL(ByVal HWndDialog As Long, ByVal ptrURL As Long)
  Dim xChar1 As String

  If UrlsAllowed Then
    xChar1 = PeekS(ptrURL)
    If Not Len(xChar1) = 0& Then
      Call ShellExecute(HWndDialog, "Open", xChar1, "", "", 5&)
      'Clipboard.Clear
      'Clipboard.SetText xChar1, Text
      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
  Call HyperLinkList_InfoDialog(2200&, AddressOf ShowInfo_Callback, MainHWnd, AddressOf ShowInfo_OpenURL, 2000&, 2200&, 2209&)
  DialogTimer False
  End Sub

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))
    If xUseChmHelp Then
      LoadResEx_Dialog_Enable HWndDialog, 2095&, FileExists(xRealBaseFile + "chm")
      Else
      LoadResEx_Dialog_Enable HWndDialog, 2095&, FileExists(xRealBaseFile + "hlp")
      End If
    'Msg = GetDlgItem(HWndDialog, 2095&)
    'If Not Msg = 0& Then
    '  If xUseChmHelp 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(LoadResEx_String(2100& + Msg), "$S", ctSSLayer_GetInfoString(5&))))
        End If
      Call SetDlgItemText(HWndDialog, 2040&, Replace(GetDlgItemText(HWndDialog, 2040&), "$", LayerHP))
'Debug.Print 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!
        If xUseChmHelp Then
          Call HtmlHelp(MainHWnd, xRealBaseFile + "chm", 0&, "soundsystem.htm")
          Else
          Call WinHelp(MainHWnd, xRealBaseFile + "hlp", &H101&, "Soundsystem")
          End If
      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

