Attribute VB_Name = "modul_Calaym_SetupDialog"
' 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

#Const SelfPathElipse = True 'also mainform

Private Declare Function GetTextFaceA Lib "gdi32" (ByVal hdc As Long, ByVal nCount As Long, ByVal lpFacename As Long) As Long
Private Declare Function GetTextFaceW Lib "gdi32" (ByVal hdc As Long, ByVal nCount As Long, ByVal lpFacename As Long) As Long
Private hKeyFont As Long
Private hKeyFontB As Long
Private hKeyFontL As Long

Private WaitForAGlobalKey As Boolean
Private SetupWaitForKeyDialogProcExOld As Long
Private xPopupMenu As Long
Private xMenu As Long

Private HWndSetupKeyList As Long
Private HWndSetupKeyApply As Long
Private KeyRow1 As Long
Private KeyRow2 As Long
'Private Const KeyRow2Space As Long = 4&
'Private xKeyMod(4) As Long

'Private IsInKeyList As Boolean

'Private Type SystemTime
'  wYear As Integer
'  wMonth As Integer
'  wDayOfWeek As Integer
'  wDay As Integer
'  wHour As Integer
'  wMinute As Integer
'  wSecond As Integer
'  wMilliseconds As Integer
'  End Type

Private Type NMHDR
  HwndFrom As Long
  idfrom As Long
  code As Long
  End Type

Private SetupDialogKBDProcOld As Long
Private xUsedTypesTmp As String
'Private xUsedTypesResortTmp As Boolean
'Private FileboxHooked As Long
Private FileBoxCount As Long
Private FileBox(99&) As String
Private FileBoxCell(99&) As Long
'Private FileScrollWidthX As Long
'Private FileScrollWidthMaX As Long
'Private FileBoxScroll As Long
Private FileBoxRowsShownP1 As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long

Private HWndSetupDialog As Long
Private HWndSetupDialogKeyPage As Long

Private KeysChangedHigh As Boolean
Private KeysChangedLow As Boolean
Private Reg_RegF(2) As Long

Private xNewKey(KbdCountX) As Long


Public Sub ShowSetup()
  DialogTimer True
  ReadRegestry
  Call LoadResEx_PagedDialog(MainHWnd, 3100&, 3550&, 50&, AddressOf SetupDialogProc, 3000&)     ', , AddressOf SetupDialogProcInit)
  Erase xNewKey()
  
  KeysChangedHigh = False
  KeysChangedLow = False
  HWndSetupDialogSSPage = 0&
  HWndSetupDialogKeyPage = 0&
  HWndSetupDialog = 0&
  HWndSetupKeyList = 0&
  HWndSetupKeyApply = 0&
  
  If Not hKeyFontL = 0& Then
    Call SelectObject(xSharedDC, hFontGFix)
    Call DeleteObject(hKeyFontL)
    hKeyFontL = 0&
    End If
  If Not xMenu = 0& Then
    DestroyMenu xMenu
    If Not xPopupMenu = 0& Then DestroyMenu xPopupMenu
    xMenu = 0&
    End If
  DialogTimer False
  End Sub

Private Sub ApplyKbdListMenuOrButton(ByVal hList As Long, ByVal xID As Long)
  If Not hList = 0& Then
    Select Case xID
      Case 3405&
        TrySetupKey hList
      Case 3406&
        ClearSomeKeys
      End Select
    End If
  End Sub

Private Function SetupDialogProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xBool1 As Boolean

  Select Case Message
    Case &H2B& ' WM_DRAWITEM
      Call DrawPLItem(lParam)
      SetupDialogProc = 1&
      Exit Function
    Case &H111&
      Select Case (wParam And &HFFFF&)
        Case 3125&, 3530&, 3535&, 3570&, 3575&
          Call LoadResEx_Dialog_CheckLongManualHandle(hWnd, wParam, lParam)
        Case 3170&, 3171&
          xBool1 = Not (IsDlgButtonChecked(hWnd, 3170&) + IsDlgButtonChecked(hWnd, 3171&) = 0&)
          LoadResEx_Dialog_Enable hWnd, 3175&, xBool1
          LoadResEx_Dialog_Enable hWnd, 3176&, xBool1
        Case 3201&
          If Not HWndSetupDialogSSPage = 0& Then
            MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
            UpdateSnds HWndSetupDialogSSPage, 3200&
            End If
        Case 3240& To 3242&
          wParam = (wParam And &HFFFF&)
          xBool1 = Not (IsDlgButtonChecked(hWnd, wParam) = 0&)
          LoadResEx_Dialog_Enable hWnd, wParam + 10&, xBool1
          LoadResEx_Dialog_Enable hWnd, wParam + 20&, xBool1
        Case 3290&
          wParam = (wParam And &HFFFF&)
          xBool1 = Not (IsDlgButtonChecked(hWnd, wParam) = 0&)
          LoadResEx_Dialog_Enable hWnd, wParam + 1&, xBool1
        
        
        Case 3310& To 3399&
          TryFileClick hWnd, (wParam And &HFFFF&)
'          GoTo lbl1
        
        Case 3405&, 3406&
          Call ApplyKbdListMenuOrButton(HWndSetupKeyList, wParam And &HFFFF&)
        Case 3420&
          'lParam = GetParent(HWnd)
          'If Not lParam = 0& Then
          '  lParam = GetDlgItem(lParam, 3400&)
            If Not HWndSetupKeyList = 0& Then
              If CalaymMessageBox(HWndSetupDialog, LoadResEx_String(3401&), LoadResEx_String(3400&), vbOKCancel) = vbOK Then
                'Call EnableWindow(hwnd, 0&)
                Call GetControls(GetDefControls, xNewKey())
                'lstRes(3301).Clear
                Call SendMessage(HWndSetupKeyList, &H184&, 0&, 0&)
                For wParam = 0& To KbdCountX
                  Call SendMessage_String(HWndSetupKeyList, &H180&, 0&, wParam)
                '  xNewKey(xLong1) = xKey(xLong1)
                '  lstRes(3301).List(xLong1) = IntToExt(xLong1)
                  Next
                KeysChangedLow = True 'False
                KeysChangedHigh = True 'False
                'GetInitWidths
                UpdateKeyListWidth
                If Not HWndSetupKeyApply = 0& Then
                  EnableWindow HWndSetupKeyApply, 0&
                  ShowWindow HWndSetupKeyApply, 0&
                  ShowWindow HWndSetupKeyApply, 1&
                  End If
                SetApply HWndSetupDialogKeyPage
                End If
              End If
            'End If
        Case 3520&
'          Call LoadResEx_Dialog_CheckBool(hwnd, 3521, True, False)
          Select Case LoadResEx_Dialog_CheckLongManualHandle(hWnd, wParam, lParam)
            Case 1&
              Call LoadResEx_Dialog_CheckBool(hWnd, 3521, RegFolderMode(3), False)
              Call LoadResEx_Dialog_Enable(hWnd, 3521&, True)
            Case 2&
              Call LoadResEx_Dialog_CheckBool(hWnd, 3521, True, False)
              Call LoadResEx_Dialog_Enable(hWnd, 3521&, True)
            Case Else
              Call LoadResEx_Dialog_Enable(hWnd, 3521&, False)
            End Select
'        Case Else
'          GoTo lbl2
        End Select
'lbl1:
'      'message = GetParent(hwnd)
'lbl2:
      
      
      TrySetApply hWnd, lParam, wParam
      
    Case &H110&
      If HWndSetupDialog = 0& Then
        HWndSetupDialog = GetParent(hWnd)
        If xMenu = 0& Then
          xMenu = LoadResEx_Menu(3400)
          xPopupMenu = LoadResEx_Menu_MakePopupMenu(xMenu)
          End If
        End If
      FileBox_Pre hWnd
      SetupDialogProc = 1&
      HandleSettings hWnd, False
    Case &H4E&
      TryApply hWnd, lParam
    End Select
  End Function

Private Sub TryApply(ByVal HWndDialog As Long, ByVal ptrNMHDR As Long)
  Dim xType1 As NMHDR
  
  Call RtlMoveMemory(xType1, ByVal ptrNMHDR, 12&)
  If xType1.code = -211& Then
    HandleSettings HWndDialog, True
    Call SendMessage(HWndSetupDialog, &H46D&, HWndDialog, 0&)
    End If
  End Sub

Private Sub HandleSettings(ByVal HWndDialog As Long, ByVal ReadFromDialog As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xBool1 As Boolean
  Dim xResetSnd As Boolean
  Dim xResetSndMode As Boolean
  Dim xResetSndDialog As Long
  Dim xType1 As LOGFONT
  Dim xType2 As POINTAPI
  Dim xType3 As TEXTMETRIC

  xBool1 = LoadResEx_Dialog_CheckBool(HWndDialog, 3290&, CDDAUse, ReadFromDialog)
  If xBool1 Or LoadResEx_Dialog_CheckBool(HWndDialog, 3291&, CDDAJitter, ReadFromDialog) Then
    xResetSnd = True
    xResetSndMode = CBool(ctSSLayer_CanUseItem(2&))
    End If

  If ReadFromDialog Then
    If FetchSSLIDFromComboBoxAndCheckItHasChanged(HWndDialog, 3200&) Then
      xResetSnd = True
      xResetSndMode = False
      End If
    
    If xResetSnd Then
      xResetSndDialog = CreateDialogIndirectParam(3000&, 0&, AddressOf SetupApplyDialogProc, 0&)
      EnableWindow HWndSetupDialog, 0&
      End If
    
    
    
    
    'LoadResEx_Dialog_SpinnerLong HWndDialog, 3220&, xLong1, True
    
    
    For xLong1 = 1& To 3&
      If Not (GetDlgItem(HWndDialog, 3239& + xLong1) = 0& Or GetDlgItem(HWndDialog, 3259& + xLong1) = 0&) Then
        LoadResEx_Dialog_SpinnerLong HWndDialog, 3259& + xLong1, xLong2, True
        LoadResEx_Dialog_CheckBool HWndDialog, 3239& + xLong1, xBool1, True
        If xBool1 Then
          xTrackerSkipper(xLong1) = xLong2
          Else
          xTrackerSkipper(xLong1) = -xLong2
          End If
        End If
      Next
    
    
    xLong1 = GetDlgItem(HWndDialog, 3300&)
    If Not xLong1 = 0& Then
      If Not xUsedTypesTmp = xUsedTypes Then 'And xUsedTypesResortTmp = xUsedTypesResort Then
        xUsedTypes = xUsedTypesTmp
        xUsedTypesResort = True 'xUsedTypesResortTmp
        End If
      End If
    
    
    
    If KeysChangedLow Or KeysChangedHigh Then
      KeysChangedLow = False
      For xLong1 = 0& To KbdCountX
        xKey(xLong1) = xNewKey(xLong1)
        Next
      MainForm_UpdateMenus
      End If
    
    
    
    xLong1 = GetDlgItem(HWndDialog, 3500&)
    If Not xLong1 = 0& Then
      LoadResEx_Dialog_CheckBool HWndDialog, 3500&, xBool1, True
      If xBool1 Xor Reg_IsAutoRun Then
        If xBool1 Then
          If RegCreateKeyEx(&H80000001, "Software\Microsoft\Windows\CurrentVersion\Run", xLong1) Then
            Call RegSetValueEx_String(xLong1, LoadResEx_String(3500&), xRealBaseFile + "exe")
            Call RegCloseKey(xLong1)
            End If
          Else
          If RegOpenKeyEx(&H80000001, "Software\Microsoft\Windows\CurrentVersion\Run", 2&, xLong1) Then
            Call RegDeleteValue(xLong1, LoadResEx_String(3500&))
            Call RegCloseKey(xLong1)
            End If
          End If
        Reg_IsAutoRun = xBool1
        'TestRestoreReg False
        End If
      End If

    xLong1 = GetDlgItem(HWndDialog, 3520&)
    If Not xLong1 = 0& Then
      LoadResEx_Dialog_CheckLong HWndDialog, 3520&, xLong3, True
      LoadResEx_Dialog_CheckBool HWndDialog, 3521&, xBool1, True
      If xLong3 = 1& Then
        RegFolderMode(2) = True
        RegFolderMode(3) = xBool1
        Else
        RegFolderMode(2) = False
        End If
' playlistformatsimport
      If xLong3 = 0& Then
        RegisterFileTypes_RemoveRegistered "m3u", LoadResEx_String(3527&), LoadResEx_String(3529&)
        RegisterFileTypes_RemoveRegistered "m3u8", LoadResEx_String(3527&), LoadResEx_String(3529&)
        RegisterFileTypes_RemoveRegistered "pls", LoadResEx_String(3527&), LoadResEx_String(3529&)
        RegisterFileTypes_RemoveRegistered "xspf", LoadResEx_String(3527&), LoadResEx_String(3529&)
        RegisterFileTypes_RemoveRegistered "aap", LoadResEx_String(3527&), LoadResEx_String(3529&)
        RegisterFileTypes_RemoveRegistered "wpl", LoadResEx_String(3527&), LoadResEx_String(3529&)
        Else
        RegisterFileTypes_SetRegistered "m3u", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "M3U"), "", -1&, xBool1, True, LoadResEx_String(3529&)
        If IsUnicode Then
          RegisterFileTypes_SetRegistered "m3u8", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "M3U8"), "", -1&, xBool1, True, LoadResEx_String(3529&)
          RegisterFileTypes_SetRegistered "wpl", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "WPL"), "", -1&, xBool1, True, LoadResEx_String(3529&)
          End If
        RegisterFileTypes_SetRegistered "pls", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "PLS"), "", -1&, xBool1, True, LoadResEx_String(3529&)
        RegisterFileTypes_SetRegistered "aap", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "AAP"), "", -1&, xBool1, True, LoadResEx_String(3529&)
        RegisterFileTypes_SetRegistered "xspf", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "XSPF"), "", -1&, xBool1, True, LoadResEx_String(3529&)
        End If
      End If
    
     'GetIsAutoRun
     TestRestoreReg False
    
    
    
    
    '3530& / 3535&
    For xLong2 = 0& To 1&
      xLong1 = GetDlgItem(HWndDialog, 3530& + xLong2 * 5&)
      If Not xLong1 = 0& Then
        LoadResEx_Dialog_CheckLong HWndDialog, 3530& + xLong2 * 5&, xLong3, True
        RegFolderMode(xLong2) = (xLong3 = 1&)
        If xLong3 = 0& Then
          Call RegDeleteKey(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&) + "\command")
          Call RegDeleteKey(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&))
          ElseIf RegCreateKeyEx(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&) + "\command", xLong1) Then
          If xLong2 = 1& Then
            Call RegSetValueEx_String(xLong1, "", Chr(34) + xRealBaseFile + "exe" + Chr(34) + " /ENQUEUE " + Chr(34) + "%1" + Chr(34))
            Else
            Call RegSetValueEx_String(xLong1, "", Chr(34) + xRealBaseFile + "exe" + Chr(34) + " " + Chr(34) + "%1" + Chr(34))
            End If
          Call RegCloseKey(xLong1)
          If RegOpenKeyEx(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&), 2&, xLong1) Then
            Call RegSetValueEx_String(xLong1, "", LoadResEx_String(3533& + xLong2 * 5&))
            Call RegCloseKey(xLong1)
            End If
          End If
        End If
      Next
    Else
    
    
    
    
    
    
    
    
    LoadResEx_Dialog_ListFill HWndDialog, 3100&, 3100&, 3102&
    LoadResEx_Dialog_ListFill HWndDialog, 3124&, 3120&, 3123&
    
    
'    ReadFont 0&
    'ReadFont 1&
    'Call SetDlgItemTextA(HWndDialog, 3151&, GetFontCaption(0))
    'Call SetDlgItemTextA(HWndDialog, 3156&, GetFontCaption(1))
'    ShowUpdateFont HWndDialog, 0&
    'ShowUpdateFont HWndDialog, 1&
    
    LoadResEx_Dialog_Enable HWndDialog, 3160&, (Not ctSSLayer_CanUseItem(4&) = 0&)
    If Not LEDMode = 0& Then
      LoadResEx_Dialog_Enable HWndDialog, 3160&, False
      xLong1 = 3162&
      ElseIf xShowSpectrum Then
      xLong1 = 3160&
      Else
      xLong1 = 3161&
      End If
    Call SetDlgItemText(HWndDialog, 3160&, LoadResEx_String(xLong1))
    
    LoadResEx_Dialog_Enable HWndDialog, 3171&, (Not xShellMode)
    
    xLong1 = GetDlgItem(HWndDialog, 3175&)
    If Not xLong1 = 0& Then
      LoadResEx_Dialog_Enable HWndDialog, 3175&, (xTrayIconVisible Or xTrayIconEnabled)
      LoadResEx_Dialog_ListFill HWndDialog, 3175&, 3170&, 3176&
      End If
    xLong1 = GetDlgItem(HWndDialog, 3176&)
    If Not xLong1 = 0& Then
      Call SendMessage_String(xLong1, &H143&, 0&, LoadResEx_String(3170&))
      Call SendMessage_String(xLong1, &H143&, 0&, LoadResEx_String(3174&))
      Call SendMessage_String(xLong1, &H143&, 0&, LoadResEx_String(3175&))
      LoadResEx_Dialog_Enable HWndDialog, 3176&, (xTrayIconVisible Or xTrayIconEnabled)
      Call SendMessage(xLong1, &H155&, 1&, 0&)
      End If
    
    
    LoadResEx_Dialog_ListFill HWndDialog, 3180&, 3180&, 3183&
    
    If Not GetDlgItem(HWndDialog, 3200&) = 0& Then
      MaxDeviceCount = ctSSLayer_RefreshDeviceList(MainHWnd)
      HWndSetupDialogSSPage = HWndDialog
      UpdateSnds HWndDialog, 3200&
      End If

    LoadResEx_Dialog_Enable HWndDialog, 3201&, (ctSSLayer_CanUseItem(&HE&) = 1&)

    If Not IsMinWindowsXP Then
      Call LoadResEx_Dialog_Enable(HWndDialog, 3206&, False)
      End If

'WinHasUserSwitchFeature:
    xBool1 = Not (ctSSLayer_GetInfoString(&H10&) = "")
    LoadResEx_Dialog_Enable HWndDialog, 3240&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3250&, xBool1
    
    xBool1 = (ctSSLayer_CanUseItem(3&) = 2&) And Not (ctSSLayer_GetInfoString(&H10&) = "")
    LoadResEx_Dialog_Enable HWndDialog, 3270&, xBool1
    
    xBool1 = Not (ctSSLayer_GetInfoString(&H11&) = "" And ctSSLayer_GetInfoString(&H12&) = "")
    LoadResEx_Dialog_Enable HWndDialog, 3241&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3251&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3275&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3276&, xBool1
    
    xBool1 = Not (ctSSLayer_CanUseItem(&HA&) = 0&)
    LoadResEx_Dialog_Enable HWndDialog, 3242&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3252&, xBool1
    
    xBool1 = (ctSSLayer_CanUseItem(&H12&) = 2&)
    LoadResEx_Dialog_Enable HWndDialog, 3573&, xBool1
    
 '324x/325x -> HH:mm, 99.9 / TrackerSkipperMinValF = 0.1
  'If LoadResEx_Dialog_SpinnerLong(HWndDialog, 3250&, 'asdf + awere,  , ReadFromDialog, IntroMdSkipperMaxVal) Then
 '
 '   End If
    For xLong1 = 1& To 3&
      xBool1 = (xTrackerSkipper(xLong1) > 0&)
      xLong2 = xTrackerSkipper(xLong1)
      If Not xBool1 Then xLong2 = -xLong2
      LoadResEx_Dialog_CheckBool HWndDialog, 3239& + xLong1, xBool1, False
      LoadResEx_Dialog_Enable HWndDialog, 3249& + xLong1, xBool1
      LoadResEx_Dialog_Enable HWndDialog, 3259& + xLong1, xBool1
      'LoadResEx_Dialog_TimeLongA HWndDialog, 3250& - 1& + xLong1, xLong2, False
      LoadResEx_Dialog_SpinnerLong HWndDialog, 3259& + xLong1, xLong2, False, xAnySkipperMin, xAnySkipperMax

      'LoadResEx_Dialog_SpinnerLong HWndDialog, 3250&,xTrackerSkipper(xLong1),False,xlong2,
      'chkres(3439& + xLong1).Value = 1
      Next
    
    LoadResEx_Dialog_Enable HWndDialog, 3290&, (ctSSLayer_CanUseItem(0&) = 2&) And Not (ctSSLayer_CanUseItem(&HA&) = 0&)
    
    xBool1 = (ctSSLayer_CanUseItem(1&) = 2&) And CDDAUse And Not (ctSSLayer_CanUseItem(&HA&) = 0& Or ctSSLayer_CanUseItem(0&) = 0&)
    LoadResEx_Dialog_Enable HWndDialog, 3291&, xBool1
    
    'chkres(3490).Enabled = (ctSSLayer_CanUseItem(0&) = 2& And ctSSLayer_CanUseItem(10&) = 1&)
    'chkres(3491&).Enabled = (CDDAUse And (Not ctSSLayer_CanUseItem(0&) = 0&) And ctSSLayer_CanUseItem(1&) = 2& And ctSSLayer_CanUseItem(10&) = 1&)

    
    xLong1 = GetDlgItem(HWndDialog, 3300&)
    If Not xLong1 = 0& Then
      xUsedTypesTmp = xUsedTypes
      'xUsedTypesResortTmp = xUsedTypesResort
      FileBox_Upd HWndDialog
      End If

    
    'KeyListFont
    If HWndSetupKeyList = 0& Then
      HWndSetupKeyList = GetDlgItem(HWndDialog, 3400&)
      If Not HWndSetupKeyList = 0& Then
        'HWndSetupDialogSSPage = 0&
        HWndSetupDialogKeyPage = HWndDialog
        
        HWndSetupKeyApply = GetDlgItem(HWndDialog, 3420&)
        SetupDialogKBDProcOld = SetWindowLong(HWndSetupKeyList, -4&, AddressOf SetupDialogKBDProc)
        'SetupDialogKBDProcOld2 = SetWindowLong(HWndSetupDialog, -4&, AddressOf SetupDialogKBDProc2)
        
        hKeyFont = SendMessage(HWndSetupKeyList, &H31&, 0&, 0&)
        hKeyFontB = hKeyFont
        
        If Not SelectObject(xSharedDC, hKeyFont) = 0& Then
          If Not GetTextMetricsA(xSharedDC, xType3) = 0& Then
            If IsUnicode Then
              xLong1 = GetTextFaceW(xSharedDC, ptrTempSizeXW, ptrTemp)
              Else
              xLong1 = GetTextFaceA(xSharedDC, ptrTempSizeX, ptrTemp)
              End If
            xType1.FontName = PeekS(ptrTemp, xLong1)
            If Not Len(xType1.FontName) = 0& Then
              If Not xLong1 = 0& Then
                If xType3.tmWeight > 499& Then
                  xType1.lfWeight = 900&
                  ElseIf xType3.tmWeight < 300& Then
                  xType1.lfWeight = 600&
                  Else
                  xType1.lfWeight = xType3.tmWeight + 300&
                  End If
                xType1.lfHeight = xType3.tmHeight
                hKeyFontL = CreateFontIndirect(xType1)
                If Not hKeyFontL = 0& Then
                  hKeyFontB = hKeyFontL
                  Call SendMessage(HWndSetupKeyList, &H30&, hKeyFontB, 0&)
                  
                  xLong1 = Len(DefTxt)
                  Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(DefTxt), xLong1, xType2)
                  Call SendMessage(HWndSetupKeyList, &H1A0&, 0&, xType2.y)
                  End If
                End If
              End If
            End If
          End If
        
        xBool1 = False
        
        GetInitWidths
        Call GetControls(GetDefControls, xNewKey())
        For xLong1 = 0& To KbdCountX
          If Not (xNewKey(xLong1) = xKey(xLong1)) Then
            xBool1 = True
            xNewKey(xLong1) = xKey(xLong1)
            End If
          Call SendMessage_String(HWndSetupKeyList, &H180&, 0&, xLong1)
          Next
        
        UpdateKeyListWidth
        
        xLong1 = GetDlgItem(HWndDialog, 3410&)
        If Not xLong1 = 0& Then
          Call SetWindowText(xLong1, Replace(Replace(GetWindowText(xLong1), "$C", lkCtrl), "$A", lkAlt))
          End If
        If Not HWndSetupKeyApply = 0& Then
          EnableWindow HWndSetupKeyApply, -CLng(xBool1)
          ShowWindow HWndSetupKeyApply, 0&
          ShowWindow HWndSetupKeyApply, 1&
          End If
  
        
  '      For xLong1 = 0& To KbdCountX
  '        xNewKey(xLong1) = xKey(xLong1)
  '        Next
        
  '      If Not HWndSetupKeyList = 0& Then
  '        GetInitWidths
  '        UpdateKeyListWidth
  '        End If
        'If Not HWndSetupKeyApply = 0& Then
        '  EnableWindow HWndSetupKeyApply, 0&
        '  End If
        End If
      End If



    LoadResEx_Dialog_CheckBool HWndDialog, 3500&, Reg_IsAutoRun, False
    
    If RegFolderMode(2) Then
      LoadResEx_Dialog_CheckLong HWndDialog, 3520&, 1&, False
      LoadResEx_Dialog_Enable HWndDialog, 3521&, True
      LoadResEx_Dialog_CheckBool HWndDialog, 3521&, RegFolderMode(3), False
      Else
      If Reg_RegF(2) = 0& Then
        LoadResEx_Dialog_CheckLong HWndDialog, 3520&, 0&, False
        LoadResEx_Dialog_Enable HWndDialog, 3521&, False
        Else
        LoadResEx_Dialog_CheckLong HWndDialog, 3520&, 2&, False
        LoadResEx_Dialog_Enable HWndDialog, 3521&, True
        End If
      LoadResEx_Dialog_CheckBool HWndDialog, 3520&, Reg_RegF(2) = 2&, False
      End If
    If xSecureMode Then
      LoadResEx_Dialog_Enable HWndDialog, 3520&, False
      LoadResEx_Dialog_Enable HWndDialog, 3521&, False
      End If
    
    LoadResEx_Dialog_CheckLong HWndDialog, 3530&, Reg_RegF(0), False
    LoadResEx_Dialog_CheckLong HWndDialog, 3535&, Reg_RegF(1), False


    xBool1 = Not xSecureMode
    LoadResEx_Dialog_Enable HWndDialog, 3520&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3530&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3535&, xBool1
    LoadResEx_Dialog_Enable HWndDialog, 3567&, xBool1   '3540
    LoadResEx_Dialog_Enable HWndDialog, 3580&, xBool1 '3542

    LoadResEx_Dialog_ListFill HWndDialog, 3580&, 3580&, 3586&
  
  
    'xSkipMe = False
    End If
  
  
  
  
  
  
  If LoadResEx_Dialog_ListIndexCB(HWndDialog, 3100&, PlDClickAction, ReadFromDialog) Then
    If InterfaceMode = Interface_Mode_Search Then SearchForm_CheckBoxTxtChange
    End If

  If LoadResEx_Dialog_CheckBool(HWndDialog, 3120&, ShowFullPath, ReadFromDialog) Then
    MainForm_ReGetWidth
    MainForm_xBuildDirs
    End If
  xBool1 = LoadResEx_Dialog_CheckBool(HWndDialog, 3123&, ShowFullTitle, ReadFromDialog)
  
  
  If LoadResEx_Dialog_CheckBool(HWndDialog, 3121&, MP3GetFormat, ReadFromDialog) Then
    If MP3GetFormat Then
      MainForm_ReadLate "", True
      End If
    Call MP3GetFormatChanged(True)
    'ShowWindow xStaticIconsHWnd(-1&), -5& * CLng(MP3GetFormat)
    'MainForm_ResizePlayer True
    End If
  Call LoadResEx_Dialog_CheckBool(HWndDialog, 3122&, xShowElapsed, ReadFromDialog)
    'update display immediatelly?
    'End If
  'Call LoadResEx_Dialog_CheckBool(HWndDialog, 3125&, xTitlesWhenMinimized, ReadFromDialog)
  Call LoadResEx_Dialog_CheckLong(HWndDialog, 3125&, xTitlesWhenMinimized, ReadFromDialog)
  If LoadResEx_Dialog_ListIndexCB(HWndDialog, 3124&, xTitlesPos, ReadFromDialog) Then
    ReCreateIRFont
    ForceIRPosUpdate
'    If ShowIRWin Then ShowIR_Do True      'False
    ElseIf xBool1 Then
#If SelfPathElipse Then
    NeedIRPathElipse = ""
#End If

    If ShowIRWin Then
      ShowIR_Do True
      End If
    End If
  
  LoadResEx_Dialog_CheckBool HWndDialog, 3160&, xDeluxeSpectrum, ReadFromDialog
  StartUpWindowMode = StartUpWindowMode + 1&
  
  
  xBool1 = LoadResEx_Dialog_CheckBool(HWndDialog, 3170&, xTrayIconVisible, ReadFromDialog)
  If xBool1 Or LoadResEx_Dialog_CheckBool(HWndDialog, 3171&, xTrayIconEnabled, ReadFromDialog) Then
    MainForm_ShowTray False
    End If
  
  LoadResEx_Dialog_ListIndexCB HWndDialog, 3175&, xTrayIconLeft, ReadFromDialog
  LoadResEx_Dialog_ListIndexCB HWndDialog, 3176&, xTrayIconRight, ReadFromDialog
  
  LoadResEx_Dialog_ListIndexCB HWndDialog, 3180&, StartUpWindowMode, ReadFromDialog
  StartUpWindowMode = StartUpWindowMode - 1&

  LoadResEx_Dialog_CheckBool HWndDialog, 3185&, xAutoSwitchIRadioOEPl, ReadFromDialog




  If LoadResEx_Dialog_CheckBool(HWndDialog, 3205&, xAutoPlayback, ReadFromDialog) Then
    xReAutoPlay = xAutoPlayback
    End If
  LoadResEx_Dialog_CheckBool HWndDialog, 3206&, OutOfSessionPause, ReadFromDialog
  
  
  xCrossFadeTimeM = xCrossFadeTimeM + xFadeConsts1
  LoadResEx_Dialog_SpinnerLong HWndDialog, 3220&, xCrossFadeTimeM, ReadFromDialog, xFadeConsts1, xFadeConsts2
  xCrossFadeTimeM = xCrossFadeTimeM - xFadeConsts1
  
  
  LoadResEx_Dialog_SpinnerLong HWndDialog, 3225&, IntroModeTime, ReadFromDialog, IntroMdSkipperMinVal, IntroMdSkipperMaxVal
  LoadResEx_Dialog_CheckBool HWndDialog, 3230&, RandomPosOnTrackerSkipper, ReadFromDialog
  LoadResEx_Dialog_CheckBool HWndDialog, 3270&, CheckAccurate, ReadFromDialog
  If LoadResEx_Dialog_SpinnerLong(HWndDialog, 3276&, xModPanning, ReadFromDialog, 0&, 100&) Then
    UpdatePanning
    End If
  
  
  
  
  
  
  LoadResEx_Dialog_CheckBool HWndDialog, 3301&, IgnoreUnderscored, ReadFromDialog
  
  
  If LoadResEx_Dialog_CheckBool(HWndDialog, 3410&, GlobalKeys, ReadFromDialog) Then
    MainForm_SetGlobalKeys GlobalKeys
    ElseIf KeysChangedHigh And GlobalKeys Then
    MainForm_SetGlobalKeys False
    MainForm_SetGlobalKeys True
    End If
  KeysChangedHigh = False
  

  
  LoadResEx_Dialog_CheckBool HWndDialog, 3550&, xSorting, ReadFromDialog
  LoadResEx_Dialog_CheckBool HWndDialog, 3560&, UsePLSettings, ReadFromDialog
  LoadResEx_Dialog_CheckBool HWndDialog, 3565&, RemoveMissingOnPLLoad, ReadFromDialog
  If LoadResEx_Dialog_CheckBool(HWndDialog, 3567&, xDelMode, ReadFromDialog) Then
    MainForm_EnableApiMenu 133, xDelMode
    MainForm_EnableApiMenu 230, xDelMode
    End If
  LoadResEx_Dialog_CheckLong HWndDialog, 3570&, LockWinUpd, ReadFromDialog
  If LoadResEx_Dialog_CheckLong(HWndDialog, 3575&, SoundSchemePointerMode, ReadFromDialog) Then
    MainForm_ApplyProcessMode
    End If
  
  LoadResEx_Dialog_CheckBool HWndDialog, 3573&, xClassicBlockingOnLoad, ReadFromDialog
  
  PlayerPriority = PlayerPriority + 2&
  If LoadResEx_Dialog_ListIndexCB(HWndDialog, 3580&, PlayerPriority, ReadFromDialog) Then
    UpdatePriority PlayerPriority - 2&
    Else
    PlayerPriority = PlayerPriority - 2&
    End If
  
  
  


  'If Not ReadFromDialog Then
    'LoadResEx_Dialog_Enable HWndDialog
    
    
    
  '  End If
  
  If xResetSnd Then
    MainForm_OutputChange HWndSetupDialog, xResetSndMode, False
    
    EnableWindow HWndSetupDialog, 1&
    If Not xResetSndDialog = 0& Then
      'EnableWindow HWndSetupDialog, 1&
      'Else
      If GetForegroundWindow = xResetSndDialog Then
        SetForegroundWindow HWndSetupDialog
        End If
      'Call EndDialog(xResetSndDialog, 0&)
      Call DestroyWindow(xResetSndDialog)
      End If
    End If
  
  End Sub

Private Sub ReadRegestry()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xBool1 As Boolean
  'Dim xChar1 As String

  GetIsAutoRun
  
  ' playlistformatsimport
  If RegFolderMode(2) Then
    Reg_RegF(2) = 1& - CLng(RegFolderMode(2))
    ElseIf RegisterFileTypes_GetRegistered("m3u", LoadResEx_String(3527&), xBool1) Or (IsUnicode And RegisterFileTypes_GetRegistered("m3u8", LoadResEx_String(3527&), xBool1)) Then
    Reg_RegF(2) = 1& - CLng(xBool1)
    Else
    Reg_RegF(2) = 0&
    End If
  
  For xLong2 = 0& To 1&
    If RegOpenKeyEx(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&) + "\command", 1&, xLong1) Then
      Call RegCloseKey(xLong1)
      If RegFolderMode(xLong2) Then
        Reg_RegF(xLong2) = 1&
        Else
        Reg_RegF(xLong2) = 2&
        End If
      Else
      Reg_RegF(xLong2) = 0&
      End If
    Next
  End Sub

Private Function SetupApplyDialogProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  If Message = &H111& Then
    SetupApplyDialogProc = 1&
    End If
  End Function

Private Sub GetInitWidths()
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Dim xLong3 As Long
  'Dim xChar1 As String
  'Static xLongA1(1) As Long
  'Dim KeyRow1 As Long
  Const KeyRow1Space As Long = 14&
  Dim xDC As Long
  Dim hdc As Long
  Dim hFontOld As Long

  xDC = CreateCompatibleDC(0&)
  If xDC = 0& Then
    hdc = xSharedDC
    Else
    hdc = xDC
    End If
  
  hFontOld = SelectObject(hdc, hKeyFont)
  Call SelectObject(xSharedDC, hKeyFontB)

  KeyRow1 = 0&
  KeyRow2 = 0&
  For xLong1 = 0& To KbdCountX
    If ValidSetupKey(xLong1) Or xLong1 = KbdC_FLoadPLMenu Then
      xLong2 = IntToExtDepth(xLong1) + GetTextOutWidth(hdc, LoadResEx_String(3700& + xLong1))
      Else
      xLong2 = IntToExtDepth(xLong1) + GetTextOutWidth(xSharedDC, LoadResEx_String(3700& + xLong1))
      End If
    If xLong2 > KeyRow1 Then KeyRow1 = xLong2
    Next
  
  For xLong1 = 0& To 4&
    xLong2 = GetTextOutWidth(hdc, lkCombi(xLong1))
    If xLong2 > KeyRow2 Then KeyRow2 = xLong2
    Next
  
  KeyRow2 = KeyRow1 + KeyRow2 + KeyRow1Space
  
  If Not xDC = 0& Then
    Call SelectObject(xDC, hFontOld)
    Call DeleteDC(xDC)
    End If
  End Sub

Private Sub UpdateKeyListWidth()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  Dim xOldWidth3 As Long
  Dim xDC As Long
  Dim hdc As Long
  Dim hFontOld As Long

  xDC = CreateCompatibleDC(0&)
  If xDC = 0& Then
    hdc = xSharedDC
    Else
    hdc = xDC
    End If
  
  hFontOld = SelectObject(hdc, hKeyFont)
  Call SelectObject(xSharedDC, hKeyFontB)

  For xLong1 = 0& To KbdCountX
    If Not xNewKey(xLong1) = 0& Then
      xChar1 = GetKeyCaption(xNewKey(xLong1) And &HFF&)
      If ValidSetupKey(xLong1) Or xLong1 = KbdC_FLoadPLMenu Then
        xLong2 = GetTextOutWidth(xSharedDC, xChar1)
        Else
        xLong2 = GetTextOutWidth(hdc, xChar1)
        End If
      If xLong2 > xOldWidth3 Then xOldWidth3 = xLong2
      End If
    Next
  
  If Not xDC = 0& Then
    Call SelectObject(xDC, hFontOld)
    Call DeleteDC(xDC)
    End If
  
  xOldWidth3 = xOldWidth3 + KeyRow2 '+ KeyRow2
  Call SendMessage(HWndSetupKeyList, &H194&, xOldWidth3 + 4&, 0&)
  End Sub

Private Sub FileBox_Pre(ByVal HWndDialog As Long)
  Dim xLong1 As Long 'ctrlheight
  Dim xLong2 As Long 'tmp
  Dim xLong3 As Long 'tmp
  Dim xChar1 As String
  Dim xBool1 As Long
  Dim xType1 As RECT
  Dim xType2 As RECT
  Dim xFont As Long
  
  Dim FileBoxCHeight As Long
  Dim FileBoxVHeight As Long
  Dim HWndControl As Long
  
  FileBoxCount = 0&
  FileBoxVHeight = 0&
  Const FileScrollWidth As Long = 14& '16 orig chkbox '= eLabelF(0).Height
  Const FileTypeSpaceLeft = 16&
  Dim FileTypeWidth As Long
  xFont = SendMessage(HWndDialog, &H31&, 0&, 0&)

  
  HWndControl = GetDlgItem(HWndDialog, 3300&)
  If Not HWndControl = 0& Then
    Call GetClientRect(HWndControl, xType1)
    xType1.Right = xType1.Right - xType1.Left - FileTypeSpaceLeft - 8&
    FileBoxRowsShownP1 = Fix(xType1.Right / 160&)
    If FileBoxRowsShownP1 < 1& Then FileBoxRowsShownP1 = 1&
    FileTypeWidth = Fix(xType1.Right / FileBoxRowsShownP1)
    
    Call GetWindowRect(HWndDialog, xType2)
    Call GetWindowRect(HWndControl, xType1)
    xType1.Top = xType1.Top - xType2.Top + 4&
    xType1.Left = xType1.Left - xType2.Left + 4&
    
    FileBoxCHeight = xType1.Bottom - xType1.Top 'eFrameF(1).ScaleHeight
    
    For xLong1 = 0& To 1&
      xChar1 = Replace(ctSSLayer_GetInfoString(&H10& + xLong1), ";", vbNullChar)
      If xLong1 = 1& Then
        xChar1 = xChar1 & Replace(ctSSLayer_GetInfoString(&H12&), ";", vbNullChar)
        End If
      If Not xChar1 = "" Then
        xLong2 = CreateWindowEx(&H0&, W_STATIC, LoadResEx_String(3300& + xLong1), &H50000000, xType1.Left, xType1.Top + FileBoxVHeight, 336&, FileScrollWidth, HWndDialog)
        If Not xLong2 = 0& Then
          Call SetWindowLong(xLong2, -12&, 3305& + xLong1)
          Call SendMessage(xLong2, &H30&, xFont, 1&)
          End If
  
        FileBoxVHeight = FileBoxVHeight + FileScrollWidth
        Do
          xLong3 = InStr(xChar1, vbNullChar)
          If xLong3 = 0& Then
            Exit Do
            Else
            If xBool1 = 0& Then
              xLong2 = CreateWindowEx(&H0&, W_BUTTON, Left(xChar1, xLong3 - 1&), &H50010005, xType1.Left + FileTypeSpaceLeft, xType1.Top + FileBoxVHeight, FileTypeWidth, FileScrollWidth, HWndDialog)
              FileBoxVHeight = FileBoxVHeight + FileScrollWidth
              Else
              xLong2 = CreateWindowEx(&H0&, W_BUTTON, Left(xChar1, xLong3 - 1&), &H50010005, xType1.Left + FileTypeSpaceLeft + FileTypeWidth * xBool1, xType1.Top + FileBoxVHeight - FileScrollWidth, FileTypeWidth, FileScrollWidth, HWndDialog)
              End If
            If Not xLong2 = 0& Then
              Call SetWindowLong(xLong2, -12&, 3310& + FileBoxCount)
              Call SendMessage(xLong2, &H30&, xFont, 1&)
              End If
            If xBool1 = 0& Then
              End If
            FileBoxCell(FileBoxCount) = xBool1
            xBool1 = (xBool1 + 1&) Mod FileBoxRowsShownP1
            FileBox(FileBoxCount) = Left(xChar1, xLong3 - 1&)
            FileBoxCount = FileBoxCount + 1&
            xChar1 = Mid(xChar1, xLong3 + 1&)
            End If
          Loop
        FileBoxVHeight = FileBoxVHeight + FileScrollWidth '\ 2&
        xBool1 = 0&
        End If
      Next
    
    
    
    If ctSSLayer_CanUseItem(&HA&) = 1& Then
      xLong2 = CreateWindowEx(&H0&, W_STATIC, LoadResEx_String(3302&), &H50000000, xType1.Left, xType1.Top + FileBoxVHeight, 336&, FileScrollWidth, HWndDialog)
      If Not xLong2 = 0& Then
        Call SetWindowLong(xLong2, -12&, 3307&)
        Call SendMessage(xLong2, &H30&, xFont, 1&)
        End If
      
      
      FileBoxVHeight = FileBoxVHeight + FileScrollWidth
      xLong2 = CreateWindowEx(&H0&, W_BUTTON, "cda", &H50010005, xType1.Left + FileTypeSpaceLeft, xType1.Top + FileBoxVHeight, FileTypeWidth, FileScrollWidth, HWndDialog)
      If Not xLong2 = 0& Then
        Call SetWindowLong(xLong2, -12&, 3310& + FileBoxCount)
        Call SendMessage(xLong2, &H30&, xFont, 1&)
        End If
      
      FileBoxVHeight = FileBoxVHeight + FileScrollWidth
      FileBox(FileBoxCount) = "cda"
      FileBoxCount = FileBoxCount + 1&
      End If
    End If
  End Sub

Private Sub TryFileClick(ByVal HWndDialog As Long, ByVal ControlID As Long)
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  Dim xIndex As Long
  Dim xBool1 As Long
  
  xIndex = ControlID - 3310&
  xChar1 = FileBox(xIndex) & ","
  xChar3 = xUsedTypesTmp
  
  If IsDlgButtonChecked(HWndDialog, ControlID) = 0& Then
    xBool1 = 1&
    Else
    xBool1 = 0&
    End If
  
  Do
    xLong1 = InStr(xChar1, ",")
    If xLong1 = 0& Then
      Exit Do
      Else
      xChar2 = Left(xChar1, xLong1 - 1&)
      xChar3 = Replace(xChar3, vbNullChar & xChar2 & vbNullChar, vbNullChar)
      
      If xBool1 = 1& Then
        xChar3 = xChar3 & xChar2 & vbNullChar
        End If
      xChar1 = Mid(xChar1, xLong1 + 1&)
      End If
    Loop
  
  If Not xChar3 = vbNullChar Then
    xUsedTypesTmp = xChar3
    Call CheckDlgButton(HWndDialog, ControlID, xBool1)
    End If
  End Sub


Private Sub FileBox_Upd(ByVal HWndDialog)
  Dim xLong1 As Long
  Dim xLong4 As Long
  Dim xBool1 As Boolean
  Dim xBool2 As Boolean
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String

  'xSkipMe = True
  xChar3 = xUsedTypesTmp
  
  For xLong1 = 0& To FileBoxCount - 1&
    xChar1 = FileBox(xLong1) & ","
    Do
      xLong4 = InStr(xChar1, ",")
      If xLong4 = 0& Then
        Exit Do
        Else
        xChar2 = Left(xChar1, xLong4 - 1&)
        If InStr(xChar3, vbNullChar & xChar2 & vbNullChar) = 0& Then
          xBool1 = True
          Else
          xBool2 = True
          End If
        xChar1 = Mid(xChar1, xLong4 + 1&)
        End If
      Loop
    Call CheckDlgButton(HWndDialog, 3310& + xLong1, -CLng(xBool2) + CLng(xBool2) * CLng(xBool1))
    xBool1 = False
    xBool2 = False
    Next
  End Sub

Private Function SetupDialogKBDProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H100&
      Select Case wParam
        Case 46&
          ClearSomeKeys
        Case 93&
          If (lParam And &H40000000) = 0& Then
            Call ApplyKbdListMenuOrButton(hWnd, GetAPIPopupMenu(HWndSetupDialog, xMenu, hWnd))
            End If
        Case 13&
          If (lParam And &H40000000) = 0& Then
            TrySetupKey hWnd
            End If
          Exit Function
        Case 9&
          LoadResEx_Dialog_ManualTab HWndSetupDialog, hWnd
          Exit Function
        Case 27&
          SendMessage HWndSetupDialog, &H471&, 5&, 0&
        End Select
    Case &H101&, &H102&
    '  If wParam = 13& Then Exit Function
      Exit Function
    Case &H87&
      SetupDialogKBDProc = 4&
      Exit Function
    Case &H203&
      If IsUnicode Then
        SetupDialogKBDProc = CallWindowProcW(SetupDialogKBDProcOld, hWnd, Message, wParam, lParam)
        Else
        SetupDialogKBDProc = CallWindowProcA(SetupDialogKBDProcOld, hWnd, Message, wParam, lParam)
        End If
      TrySetupKey hWnd
      GoTo NoProc
    Case &H205&
      Call ApplyKbdListMenuOrButton(hWnd, TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), hWnd, ((lParam And &HFFFF0000) \ &H10000), True, xMenu, 1&))
#If ListFocusedLists Then
    Case 7&
      xCurFocusHWnd = hWnd
'      Debug.Print "Focused to", xCurFocusHWnd
    Case 8&
      xCurFocusHWnd = wParam
'      Debug.Print "(Un)Focused to", xCurFocusHWnd
#End If
    End Select
  If IsUnicode Then
    SetupDialogKBDProc = CallWindowProcW(SetupDialogKBDProcOld, hWnd, Message, wParam, lParam)
    Else
    SetupDialogKBDProc = CallWindowProcA(SetupDialogKBDProcOld, hWnd, Message, wParam, lParam)
    End If
NoProc:
  End Function

Private Sub ClearSomeKeys()
  Dim xLong1 As Long
  Dim xBool1 As Boolean

  For xLong1 = 0& To KbdCountX
    If Not xNewKey(xLong1) = 0& Then
      If Not SendMessage(HWndSetupKeyList, &H187&, xLong1, 0&) = 0 Then
        xNewKey(xLong1) = 0&
        LoadResEx_Dialog_ListItemSetData HWndSetupKeyList, xLong1, xLong1, True
        If xLong1 < KbdSplit Then
          KeysChangedLow = True
          Else
          KeysChangedHigh = True
          End If
        xBool1 = True
        End If
      End If
    Next
  If xBool1 Then
    If Not HWndSetupKeyApply = 0& Then
      EnableWindow HWndSetupKeyApply, 1&
      ShowWindow HWndSetupKeyApply, 0&
      ShowWindow HWndSetupKeyApply, 1&
      End If
    UpdateKeyListWidth
    SetApply HWndSetupDialogKeyPage
    End If
  End Sub

Private Sub SetKeyNow(ByVal xWaitKey As Long, ByVal Shift As Long, ByVal KeyCode As Long)
  'Dim xLong1 As Long
  'Dim xLong2 As Long
  
  'Shift = (CLng(Shift) And &H3&)
  'xLong2 = CLng(KeyCode)
  
  Select Case KeyCode
    Case 0& To 4&, 7& To 31&, 93&, 144& ' 38&, 40&, 93&
      'Debug.Print "Skipped: ", KeyCode
      KeyCode = 0&
    Case 5&, 6&, 32& To 40& ', &HE7&, &HE8&  '40&
      If Shift = 0& And xWaitKey < KbdSplit Then
        'Debug.Print "Ignored: ", KeyCode
        KeyCode = 0&
        End If
    Case 46&
      If xWaitKey > KbdSplit Then
        'Debug.Print "Ignored: ", KeyCode
        KeyCode = 0&
        End If
    End Select
  If UnassignedKey(KeyCode) Then
    If xWaitKey < KbdSplit Then
      KeyCode = 0&
      Else
      Shift = (Shift And 3&)
      End If
    ElseIf Not xWaitKey < KbdSplit Then
    Shift = (Shift And 1&)
    End If
  
'Debug.Print "KeyCode", KeyCode
  If Not KeyCode = 0& Then
    xNewKey(xWaitKey) = Shift * &H100& + KeyCode
    LoadResEx_Dialog_ListItemSetData HWndSetupKeyList, xWaitKey, xWaitKey, True
    SetApply HWndSetupDialogKeyPage

    If xWaitKey > KbdSplit Then
      KeysChangedHigh = True
      For Shift = KbdSplitN To KbdCountX
        If xNewKey(Shift) = xNewKey(xWaitKey) And Not Shift = xWaitKey Then
          xNewKey(Shift) = 0&
          LoadResEx_Dialog_ListItemSetData HWndSetupKeyList, Shift, Shift, False
          End If
        Next
      Else
      KeysChangedLow = True
      For Shift = 0& To KbdSplitP
        If xNewKey(Shift) = xNewKey(xWaitKey) And Not Shift = xWaitKey Then
          xNewKey(Shift) = 0&
          LoadResEx_Dialog_ListItemSetData HWndSetupKeyList, Shift, Shift, False 'True
          End If
        Next
      End If
    If Not HWndSetupKeyApply = 0& Then
      EnableWindow HWndSetupKeyApply, 1&
      ShowWindow HWndSetupKeyApply, 0&
      ShowWindow HWndSetupKeyApply, 1&
      End If
    UpdateKeyListWidth
    End If
  End Sub

Private Sub TrySetupKey(ByVal HWndList As Long)
  Dim xLong1 As Long
  
  If Not SendMessage(HWndList, &H190&, 0&, 0&) = 0& Then
    xLong1 = SendMessage(HWndList, &H19D&, 0&, 0&)
    If Not xLong1 = -1& Then
      If ValidSetupKey(xLong1) Then
        WaitForAGlobalKey = (xLong1 > KbdSplit)
        HWndList = DialogBoxIndirectParam(3700&, HWndSetupDialog, AddressOf SetupWaitForKeyDialogProc, 0&)
        If Not HWndList = 0& Then
          SetKeyNow xLong1, (HWndList And &H30000) \ &H10000, (HWndList And &HFF&)
          End If
        End If
      End If
    End If
  End Sub

Private Function SetupWaitForKeyDialogProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  
  Select Case Message
'    Case &H100& To &H108&
'      Exit Function
    Case &H111&
      If Not wParam = 0& Then
        'WaitKeyDone True
        EndDialog hWnd, 0&
        End If
    Case &H10&
      'WaitKeyDone True
      EndDialog hWnd, 0&
    Case &H110&
      If wParam = 0& Then
        wParam = GetDlgItem(hWnd, 3700&)
        If wParam = 0& Then
          EndDialog hWnd, 0&
          Else
          Call SetWindowLong(wParam, -4&, AddressOf SetupWaitForKeyDialogProcEx)
          Call SetFocusAPI(wParam)
          SetupWaitForKeyDialogProc = 1&
          End If
        Else
        SetupWaitForKeyDialogProcExOld = SetWindowLong(wParam, -4&, AddressOf SetupWaitForKeyDialogProcEx)
        SetupWaitForKeyDialogProc = 1&
        End If
    End Select
  End Function

Private Function SetupWaitForKeyDialogProcEx(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H101&
      Select Case wParam
        Case 13&, 16&, 17&
        Case 33& To 40&
          If WaitForAGlobalKey Then
            EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
            Exit Function
            End If
        Case 46&
          If Not WaitForAGlobalKey Then
            EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
            Exit Function
            End If
        Case Else
          'Debug.Print "Res:", wParam
          EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
          Exit Function
        End Select
    Case &H100&
      Select Case wParam
        Case 13&, 16&, 17&
          Exit Function
        Case 33& To 40&
          If WaitForAGlobalKey Then
            EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
            Exit Function
            End If
        Case 46&
          If Not WaitForAGlobalKey Then
            EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
            Exit Function
            End If
        Case Else
'          Debug.Print "Res2:", wParam
          EndDialog GetParent(hWnd), wParam + GetShiftCode * &H10000
          Exit Function
        End Select
    Case &H102&
    Case &H20B&, &HAB&
      If (wParam And &H60) Then
'        Debug.Print "mouseforeback...?"
        EndDialog GetParent(hWnd), (Hex(wParam And &H60) \ 20& + 4&) + ((wParam And &HC&) \ 4&) * &H1000&
        End If
    End Select
  If IsUnicode Then
    SetupWaitForKeyDialogProcEx = CallWindowProcW(SetupWaitForKeyDialogProcExOld, hWnd, Message, wParam, lParam)
    Else
    SetupWaitForKeyDialogProcEx = CallWindowProcA(SetupWaitForKeyDialogProcExOld, hWnd, Message, wParam, lParam)
    End If
  End Function

Private Sub SetApply(ByVal HwndFromDialog As Long)
  Call SendMessage(HWndSetupDialog, &H468&, HwndFromDialog, 0&)
  End Sub

Private Sub TrySetApply(ByVal HWndDialog As Long, ByVal HWndControl As Long, ByVal xEvent As Long)
  Select Case UCase(GetClassName(HWndControl))
    Case W_BUTTON
      If (GetWindowLong(HWndControl, -16&) And &HF&) > 1& Then
        SetApply HWndDialog
        End If
    Case "COMBOBOX"
      If (xEvent And &HFFFF0000) = &H10000 Then
        SetApply HWndDialog
        End If
    Case W_EDIT
      If (xEvent And &HFFFF0000) = &H3000000 Then
        SetApply HWndDialog
        End If
    End Select
  End Sub

Private Sub DrawPLItem(ByVal lParam As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xLong5 As Long
  Dim hFontOld As Long
  Dim xType1 As DRAWITEMSTRUCT
  Dim xType3 As RECT
  Call RtlMoveMemory(xType1, ByVal lParam, 44&)

  If Not xType1.hdc = 0& Then
    xLong1 = GetBkColor(xType1.hdc)
    xLong2 = GetTextColor(xType1.hdc)
    xLong3 = xType1.rcItem.Left
    xLong4 = xType1.rcItem.Right
    
    If (xType1.itemState And &H1&) Then
      If xType1.ItemID = -1& Then GoTo IDUnknown
#If ListFocusedLists Then
      If ValidSetupKey(xType1.ItemID) And xCurFocusHWnd = xType1.hwndItem Then
#Else
      If ValidSetupKey(xType1.ItemID) Then
#End If
'Debug.Print "UseFor:", xType1.itemID
'          hFontOld = SelectObject(xType1.hDC, hKeyFont)
        Call FillRect(xType1.hdc, xType1.rcItem, 14&)
        Call SetBkColor(xType1.hdc, SysColor13)
        Call SetTextColor(xType1.hdc, SysColor14)
        Else
IDUnknown:
        Call FillRect(xType1.hdc, xType1.rcItem, 16&)
        Call SetBkColor(xType1.hdc, SysColor15)
        Call SetTextColor(xType1.hdc, SysColor8)
        End If
      Else
      'If ValidSetupKey(xType1.itemID) Then
      If xType1.ItemID = KbdC_FLoadPLMenu Then
        Call SetTextColor(xType1.hdc, SysColor17)
        End If
      Call FillRect(xType1.hdc, xType1.rcItem, 6&)
      End If
      
    If Not xType1.ItemID = -1& Then
      xType1.rcItem.Left = IntToExtDepth(xType1.ItemID)
      If ValidSetupKey(xType1.ItemID) Or xType1.ItemID = KbdC_FLoadPLMenu Then
        hFontOld = SelectObject(xType1.hdc, hKeyFont)
        Call DrawText(xType1.hdc, LoadResEx_String(3700& + xType1.ItemID), xType1.rcItem, &H924&)
        'Call SelectObject(xType1.hDC, hFontOld)
        Else
        Call DrawText(xType1.hdc, LoadResEx_String(3700& + xType1.ItemID), xType1.rcItem, &H924&)
        If Not xType1.ItemID = KbdC_FLoadPLMenu Then
          xType3 = xType1.rcItem
          Call DrawText(xType1.hdc, LoadResEx_String(3700& + xType1.ItemID), xType3, &HD24&)
          xType3.Top = xType3.Bottom - 1&
          
          xType3.Left = xType3.Left - 3&
          xType3.Right = xType3.Right + 4&
          'If xType3.Left < xLong3 Then xType3.Left = xLong3
          'If xType3.Right > xType1.rcItem.Right Then xType3.Right = xType1.rcItem.Right
          
          'If (xType1.itemState And &H1&) Then
          '  Call FillRect(xType1.hdc, xType3, 19&)
          '  Else
            Call FillRect(xType1.hdc, xType3, 9&)
          '  End If
          End If
        End If
      
      If Not xNewKey(xType1.ItemID) = 0& Then
        If ValidSetupKey(xType1.ItemID) Then
          If xType1.ItemID < KbdSplitN Then
            xLong5 = ((xNewKey(xType1.ItemID) And &H300&) \ &H100&)
            If xLong5 = 0& Then GoTo NoPrefix
            ElseIf UnassignedKey(xNewKey(xType1.ItemID)) Then
            xLong5 = ((xNewKey(xType1.ItemID) And &H100&) \ &H100&)
            If xLong5 = 0& Then GoTo NoPrefix
            Else
            xLong5 = 4& + ((xNewKey(xType1.ItemID) And &H100&) \ &H100&)
            End If
          xType1.rcItem.Right = KeyRow2 '- xKeyMod(xLong5)
          Call DrawText(xType1.hdc, lkCombi(xLong5 - 1&), xType1.rcItem, &H926&)
NoPrefix:
          xType1.rcItem.Left = KeyRow2
          xType1.rcItem.Right = xLong4
          Call DrawText(xType1.hdc, GetKeyCaption(xNewKey(xType1.ItemID)), xType1.rcItem, &H924&)
          End If
        End If
        
      'If ValidSetupKey(xType1.itemID) Then
        Call SetTextColor(xType1.hdc, SysColor8)
        If (xType1.itemState And &H1&) = 0& Then
          Call SetBkColor(xType1.hdc, SysColor5)
          Else
          Call SetBkColor(xType1.hdc, SysColor15)
          End If
      '  Else
      '  Call SetTextColor(xType1.hdc, 18&)
      '  If (xType1.itemState And &H1&) Then
      '    Call SetBkColor(xType1.hdc, xLong1)
      '    Else
      '    Call SetBkColor(xType1.hdc, 15&)
      '    End If
      '  End If
      
      xType1.rcItem.Left = xLong3
      If Not hFontOld = 0& Then Call SelectObject(xType1.hdc, hFontOld)
      End If
    Call SetBkColor(xType1.hdc, xLong1)
    Call SetTextColor(xType1.hdc, xLong2)
    xType1.rcItem.Right = xLong4
    End If
  If (xType1.itemState And &H10&) Then
    'If IsUnicode Then
    '  Call DefWindowProcW(hwnd, Message, wParam, lParam)
    '  Else
    '  Call DefWindowProcA(hwnd, Message, wParam, lParam)
    '  End If
    Call DrawFocusRect(xType1.hdc, xType1.rcItem)
    End If
  End Sub

Private Function IntToExtDepth(ByVal Index As Long) As Long
  Select Case Index
    Case KbdC_FFileMenu, KbdC_CPCMenu, KbdC_PPLMenu, KbdC_OOptionsMenu, KbdC_TToolsMenu, KbdC_IInfoMenu, KbdSplit
      IntToExtDepth = 4&
    Case KbdC_FLoadPLDefault, _
         KbdC_CCSSelFile To KbdC_CCSExplorer, _
         KbdC_CVolUp To KbdC_CVolFull, _
         KbdC_OAutoRF To KbdC_OAutoRFSetup, KbdC_ODirToggle To KbdC_ODirDec, _
         KbdC_OTimerEdit To KbdC_OTimerClear, _
         KbdC_OPresetSave To KbdC_OPresetLoad
    '+KbdC_OInternetRadio,
      IntToExtDepth = 44&
    Case KbdC_GVolUp To KbdC_GVolFull
      IntToExtDepth = 44&
    'Case KbdSplitN To KbdCountX
    '  IntToExtDepth = 24&
    Case Else
      IntToExtDepth = 24&
    End Select
  End Function

