Attribute VB_Name = "modul_Calaym_TimerEditor"
' Copyright (C) 2002-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 Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hWnd As Long, ByVal fAccept As Long)

Private xPopupMenu(2) As Long
Private xEventCount As Long
Private xEventID(xTimerEvMaxSubEventsX) As Long
Private xEventFile(xTimerEvMaxSubEventsX) As String
Private xEvAlias(KbdSplitP) As Long
Private HWndList(1) As Long

Private TimerEditKBDProcOld As Long 'TimerEditKBDProc)
Private TimerEditKBDProcOldLite As Long 'TimerEditKBDProcLite
Private TimerEditTxtProcOld As Long 'TimerEditTxtProc)

Public Sub ShowTimerEditor()
  Dim xLong1 As Long
  'Dim xChar1 As String
  
  xTimerEditing = True
  'xChar1 = LoadResEx_Data("#2700", "#5", True)
  'If Not Len(xChar1) = 0& Then
    DialogTimer True
    
    'xLong1 = DialogBoxIndirectParam(2700&, MainHWnd, AddressOf TimerEditProc, 0&)
    Call DialogBoxIndirectParam(2700&, MainHWnd, AddressOf TimerEditProc, 0&)
    
    xTimerEditingHWnd = 0&
    For xLong1 = 0& To 2&
      If xTimerPLMenu(xLong1) Then
        DestroyMenu xTimerPLMenu(xLong1)
        If xPopupMenu(xLong1) Then
          DestroyMenu xPopupMenu(xLong1)
          End If
        xTimerPLMenu(xLong1) = 0&
        End If
      Next
    Erase xEventID()
    Erase xEventFile()
    xEventCount = 0&
    DialogTimer False
'    End If
  xTimerEditing = False
  End Sub

Private Function TimerEditProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H100&
      If wParam = 112& Then
        RunTimerHelp
        End If
    Case &H111&
       RaiseButton wParam
    Case &H233&
      DropFiles wParam
    Case &H10&
      SetDragDrop False
      EndDialog hWnd, 1&
    Case &H110&
      xTimerEditingHWnd = hWnd
      Initialize
      TimerEditProc = 1&
    End Select
  End Function

Private Sub Initialize()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  'Dim xLong5 As Long
  Dim xChar1 As String
  'Dim xType1 As POINTAPI
  
  LoadResEx_Dialog_Enable xTimerEditingHWnd, 2741&, False
  LoadResEx_Dialog_Enable xTimerEditingHWnd, 2742&, False
  LoadResEx_Dialog_Enable xTimerEditingHWnd, 2743&, False
  'LoadResEx_Dialog_Enable xTimerEditingHWnd, 2760&, False
  LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, False
  
  If xUseChmHelp Then
    Call LoadResEx_Dialog_Enable(xTimerEditingHWnd, 2780&, Not (xSecureMode Or vKioskMode) And FileExists(xRealBaseFile + "chm"))
    Else
    Call LoadResEx_Dialog_Enable(xTimerEditingHWnd, 2780&, Not (xSecureMode Or vKioskMode) And FileExists(xRealBaseFile + "hlp"))
    End If
  
  
  
  For xLong1 = 0& To 2&
    xTimerPLMenu(xLong1) = LoadResEx_Menu(2700& + xLong1 * 10&)
    xPopupMenu(xLong1) = LoadResEx_Menu_MakePopupMenu(xTimerPLMenu(xLong1))
    xTimerPLMenuLoaded(xLong1) = 0&
    Call EnableMenuItem(xTimerPLMenu(xLong1), 2750& + xLong1 * 10&, 3&)
    'Call EnableMenuItem(xTimerPLMenu(xLong1), 2780&, xLong5)
    Next
  
  If xSecureMode Then
    EnableMenuItem xTimerPLMenu(0&), 2700&, 3&
    EnableMenuItem xTimerPLMenu(1&), 2710&, 3&
    EnableMenuItem xTimerPLMenu(2&), 2720&, 3&
    LoadResEx_Dialog_Enable xTimerEditingHWnd, 2780&, False
    End If
  
  Call MainForm_XMenuRefresher
  
  
  HWndList(0) = GetDlgItem(xTimerEditingHWnd, 2730&)
  HWndList(1) = GetDlgItem(xTimerEditingHWnd, 2750&)
  
  If Not HWndList(0) = 0& Then
    Call SelectObject(xSharedDC, hFontGSys)
    For xLong1 = 0& To KbdSplitP
        Select Case xLong1
          Case KbdC_FAddPList, KbdC_FSavePLas, _
               KbdC_CRepToggle, KbdC_CCSSelFile, KbdC_CCSRemFile, KbdC_CCSDelFile, KbdC_CCSExplorer, _
               KbdC_PPlayNext, KbdC_PFind, KbdC_PFindNext, KbdC_PFindQuick, _
               KbdC_PInvSel, KbdC_PRemSongs, KbdC_PRem, KbdC_PCopyPLEnd, KbdC_PCrop, KbdC_PDelFiles, KbdC_PExplorer, _
               KbdC_OEndModeTggle, KbdC_CMoreSetts, KbdC_OAutoRFSetup, KbdC_PGotoDir, KbdC_ODirToggle, KbdC_ODirInc, KbdC_ODirDec, KbdC_OTimerEdit, KbdC_OTimerExport, KbdC_OVolCtrl, KbdC_OPresetSave, _
               KbdC_IHelp, KbdC_IAbout
          Case KbdC_FFileMenu, KbdC_CPCMenu, KbdC_PPLMenu, KbdC_OOptionsMenu, KbdC_TToolsMenu
            xChar1 = LoadResEx_String(3700& + xLong1)
            GoSub AddKey2
          Case KbdC_FQuitSD
            xEvAlias(xLong2) = KbdC_FQuitSD
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_FShutDown
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_FHibernate
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_FStandby
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_FMonitoff
            GoSub AddKey
          Case KbdC_CPlay
            xEvAlias(xLong2) = KbdCTm_CPlay
            GoSub AddKey
          Case KbdC_CPause
            xEvAlias(xLong2) = KbdCTm_CPause
            GoSub AddKey
          Case KbdC_CStop
            xEvAlias(xLong2) = KbdCTm_CStop
            GoSub AddKey
          Case KbdC_PFindCurrent
            xEvAlias(xLong2) = KbdCTm_PFindCurrent
            GoSub AddKey
          
          
          Case KbdC_CIntroMode
            xEvAlias(xLong2) = KbdCTm_CIntroModeOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_CIntroModeOff
            GoSub AddKey
          
          Case KbdC_OFading
            xEvAlias(xLong2) = KbdCTm_OFadingOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_OFadingOff
            GoSub AddKey
          Case KbdC_OSpektrum
            xEvAlias(xLong2) = KbdCTm_OSpektrumOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_OSpektrumOff
            GoSub AddKey
          Case KbdC_OFileInfos
            xEvAlias(xLong2) = KbdCTm_OFileInfosOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_OFileInfosOff
            GoSub AddKey
          Case KbdC_OFollowSong
            xEvAlias(xLong2) = KbdCTm_OFollowSongOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_OFollowSongOff
            GoSub AddKey
          Case KbdC_ORemMissing
            xEvAlias(xLong2) = KbdCTm_ORemMissingOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_ORemMissingOff
            GoSub AddKey
          Case KbdC_ORemSkipped
            xEvAlias(xLong2) = KbdCTm_ORemSkippedOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_ORemSkippedOff
            GoSub AddKey
          Case KbdC_ORemPlayed
            xEvAlias(xLong2) = KbdCTm_ORemPlayedOn
            GoSub AddKey
            xEvAlias(xLong2) = KbdCTm_ORemPlayedOff
            GoSub AddKey
          Case KbdC_ONoEndMode
            xEvAlias(xLong2) = KbdCTm_ONoEndMode
            GoSub AddKey
          Case KbdC_OXFade
            xEvAlias(xLong2) = KbdCTm_OXFadeOn
            GoSub AddKey
          Case KbdC_OMDMode
            xEvAlias(xLong2) = KbdCTm_OMDModeOff
            GoSub AddKey
          Case KbdC_OStopEach
            xEvAlias(xLong2) = KbdCTm_OStopEachOn
            GoSub AddKey
          'Case KbdC_OEndModeTggle
          '  xEvAlias(xLong2) = KbdCTm_OEndModeOff
          '  GoSub AddKey
          Case KbdC_OInternetRadio ' needs special handling... soon...
          'Case KbdC_OInternetRadioRe '...
          'Case KbdC_OInternetRadioHP
            'OpenIRUrl
          Case Else
            If ValidSetupKey(xLong1) Then
              xEvAlias(xLong2) = xLong1
              GoSub AddKey
              End If
          End Select
      Next
    
    If Not xLong3 = 0& Then
      xLong1 = GetDlgItem(xTimerEditingHWnd, 2730&)
      If Not xLong1 = 0& Then Call SendMessage(xLong1, &H194&, xLong3 + 4&, 0&)
      End If
    TimerEditKBDProcOldLite = SetWindowLong(HWndList(0), -4&, AddressOf TimerEditKBDProcLite)
    End If
  If Not HWndList(1) = 0& Then
    TimerEditKBDProcOld = SetWindowLong(HWndList(1), -4&, AddressOf TimerEditKBDProc)
    End If

  xLong1 = GetDlgItem(xTimerEditingHWnd, 2705&)
  If Not xLong1 = 0& Then
    TimerEditTxtProcOld = SetWindowLong(xLong1, -4&, AddressOf TimerEditTxtProc)
    End If



  If xTimerEvMode = -1& Then
    'xLong1 = (Timer \ 60& + 30&) Mod 1440&
    'eTime.Value = Right("00" & CStr(xLong1 \ 60&), 2&) & ":" & Right("00" & CStr(xLong1 Mod 60&), 2&)
    'SetDragDrop True
    LoadResEx_Dialog_TimeLongA xTimerEditingHWnd, 2700&, -1&
    Else
    'xLong1 = xTimerEvTime(xTimerEvMode) ' \ 60&
    'eTime.Value = Right("00" & CStr(xLong1), 2&) & ":" & Right("00" & CStr(xTimerEvTime(xTimerEvMode) - xLong1 * 60&), 2&)
'Debug.Print "WriteDebugTime2", xTimerEvTime(xTimerEvMode)
    LoadResEx_Dialog_TimeLongA xTimerEditingHWnd, 2700&, xTimerEvTime(xTimerEvMode)
    WriteSettings xTimerEv(xTimerEvMode)
    End If
  SetDragDrop True
'  eTime.Value = Right("00" & CStr(xLong1 \ 60&), 2&) & ":" & Right("00" & CStr(xLong1 Mod 60&), 2&)
  
  Exit Sub
  
AddKey:
  xChar1 = "    " & LoadResEx_String(3700& + xEvAlias(xLong2))
AddKey2:
  xLong4 = GetTextOutWidth(xSharedDC, xChar1)
  If xLong4 > xLong3 Then xLong3 = xLong4
  Call SendMessage_String(HWndList(0), &H180, 0&, xChar1)
  xLong2 = xLong2 + 1&
  Return
  End Sub

Private Sub RaiseButton(ByVal ButtonID As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String

  Select Case (ButtonID And &HFFFF&)
    Case 2750&
      If (ButtonID And &HFFFF0000) = &H10000 Then
        ListSelChangeUnknown
        End If
    Case 2740& 'add
      AddNow 2&
    Case 2741& 'rem
      If GetSelected(1&, xLong1) Then
        For xLong2 = xLong1 To xEventCount - 2&
          xEventID(xLong2) = xEventID(xLong2 + 1&)
          xEventFile(xLong2) = xEventFile(xLong2 + 1&)
          Next
        xEventCount = xEventCount - 1&
        If Not HWndList(1) = 0& Then
          Call SendMessage(HWndList(1), &H182, xLong1, 0&)
          End If
        UpdateWidths
        If xEventCount = 0& Then
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2741&, False
          SetDlgItemText xTimerEditingHWnd, 2760&, ""
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, False
          Else
          If xEventCount = 1& Then
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2742&, False
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2743&, False
            End If
          If xLong1 < xEventCount Then
            'lstRes(2750).ListIndex = xLong1
            LoadResEx_Dialog_ListIndexMS xTimerEditingHWnd, 2750&, xLong1
            Else
            LoadResEx_Dialog_ListIndexMS xTimerEditingHWnd, 2750&, xLong1 - 1&
            End If
          If xEventCount = xTimerEvMaxSubEventsX Then
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2740&, True
            SetDragDrop True
            End If
          End If
        End If
    Case 2742& 'up
      If GetSelected(1&, xLong1) Then
        If Not xLong1 = 0& Or xEventCount = 1& Then
          Call ToggleEvents(xLong1, xLong1 - 1&)
          End If
        End If
    Case 2743& 'down
      If GetSelected(1&, xLong1) Then
        If Not xLong1 = xEventCount - 1& Then
          Call ToggleEvents(xLong1, xLong1 + 1&)
          End If
        End If
    Case 2761& 'browse!
      If GetSelected(1&, xLong1) Then
        Call UpdateByMenu(xLong1, 1&)
        End If
    Case 2780&
      RunTimerHelp
    Case 2790&
      #If TimeTestDebug Then
        MainForm_ctConsoleA_AddLog "TimeDebug: Trying " & eTime.Value
        #End If
      'xLong1 = CorrectDate(eTime.Value)
      LoadResEx_Dialog_TimeLongA xTimerEditingHWnd, 2700&, xLong1, True
      'xChar1 = Trim(txtRes(2706).Text)
      xChar1 = GetDlgItemText(xTimerEditingHWnd, 2705&)
      If xEventCount = 0& Then
        Call CalaymMessageBox(xTimerEditingHWnd, LoadResEx_String(2743), LoadResEx_String(2740), vbOKOnly + vbInformation)
        ElseIf Not InStr(xChar1, ":") = 0& Then
        Call CalaymMessageBox(xTimerEditingHWnd, LoadResEx_String(2741), LoadResEx_String(2740), vbOKOnly + vbInformation)
        Else
        'If chkres(2710).Value = 1 Then xLong1 = (Timer \ 60& + xLong1) Mod 1440&
        xLong2 = Timer \ 60&
        If (xLong1 = xLong2 Or _
          (xLong1 + 1&) Mod 1440& = xLong2 Or _
          (xLong1 + 1439&) Mod 1440& = xLong2 Or _
          (xLong1 + 1438&) Mod 1440& = xLong2) Then
          Call CalaymMessageBox(xTimerEditingHWnd, LoadResEx_String(2742), LoadResEx_String(2740), vbOKOnly + vbInformation)
          Else
          For xLong2 = 0& To xTimerEvCount - 1&
            If xTimerEvTime(xLong2) = xLong1 And Not xLong2 = xTimerEvMode Then
              Call CalaymMessageBox(xTimerEditingHWnd, LoadResEx_String(2744), LoadResEx_String(2740), vbOKOnly + vbInformation)
              Exit Sub
              End If
            Next
          For xLong2 = 0& To xEventCount - 1&
            If xLong2 = 0& Then
              xChar1 = xChar1 & "=" & CStr(xEventID(xLong2))
              Else
              xChar1 = xChar1 & TimerSepp & CStr(xEventID(xLong2))
              End If
            If HasSubFile(xEventID(xLong2)) Then
              xChar1 = xChar1 & "," & xEventFile(xLong2)
              End If
            Next
          #If TimeTestDebug Then
            MainForm_ctConsoleA_AddLog "TimeDebug: New/Changed timer: " & CStr(xLong1)
            #End If
          xLong1 = xTimerChange(xTimerEvMode, xLong1, xChar1)
          If xTimerEvMode = xTimerEvIR And Not xTimerEvIR = -1& Then xTimerEvIR = xLong1
          #If TimeTestDebug Then
            MainForm_ctConsoleA_AddLog "TimeDebug: Listitem: (#" & CStr(xLong1) & ") " & xTimerDisplay(xLong1)
            #End If
          Call SendMessage_String(Interface_Control(Interface_Control_Timer_List), &H181&, xLong1, xTimerDisplay(xLong1, 1&))
          Call SetListIndexMS(Interface_Control(Interface_Control_Timer_List), xLong1)
          TimerListForm_ListUpdate
          SetDragDrop False
          EndDialog xTimerEditingHWnd, 0&
          'Unload Me
          End If
        End If
    Case 2791&
      SetDragDrop False
      EndDialog xTimerEditingHWnd, 0&
    End Select
  End Sub

Private Sub ListSelChangeUnknown()
  Dim xLong1 As Long
    
  If GetSelected(1&, xLong1) Then
    ListSelChange xLong1
    End If
  End Sub

Private Sub ListSelChange(ByVal Index As Long)
  If HasSubFile(xEventID(Index)) Then
    LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, True
    SetDlgItemText xTimerEditingHWnd, 2760&, xEventFile(Index)
    Else
    LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, False
    SetDlgItemText xTimerEditingHWnd, 2760&, ""
    End If
  End Sub

Private Function GetSelected(ByVal Index As Long, ByRef SelIndex As Long) As Boolean
  If Index = 0& Or Not xEventCount = 0& Then
'    If Not lstRes(Index).SelCount = 0 Then
      'SelIndex = lstRes(Index).ListIndex
    If Not HWndList(Index) = 0& Then
      If Not SendMessage(HWndList(Index), &H190&, 0&, 0&) = 0& Then
        SelIndex = SendMessage(HWndList(Index), &H19D&, 0&, 0&)
        If Not SelIndex = -1& Then
          GetSelected = True
          End If
        End If
      End If
    End If
  End Function

Private Sub UpdateByMenu(ByVal xIndex As Long, ByVal ByButton As Long)
  Dim xChar1 As String
  
  xChar1 = GetBrowsedItem(xEventID(xIndex), ByButton, xEventFile(xIndex))
  If Not Len(xChar1) = 0& Then
    SetDlgItemText xTimerEditingHWnd, 2760&, xChar1
    'txtRes(2760).Text = xChar1
    xEventFile(xIndex) = xChar1
'    lstRes(2750).List(xIndex) =
    If Not HWndList(1) = 0& Then
      LoadResEx_Dialog_ListItemSet HWndList(1), xIndex, LoadResEx_String(3700& + xEventID(xIndex)) & vbTab & xChar1, True
      UpdateWidths
      End If
    End If
  End Sub

Private Sub WriteSettings(ByVal TimerEvSetts As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar2 As String

  xLong1 = InStr(TimerEvSetts, "=")
  If Not xLong1 = 0& Then
    Call SetDlgItemText(xTimerEditingHWnd, 2705&, Trim(Left(TimerEvSetts, xLong1 - 1&)))
    End If
  TimerEvSetts = TimerEvSetts & TimerSepp
  
  Do
    xLong2 = InStr(xLong1 + 1&, TimerEvSetts, TimerSepp)
    If xLong2 = 0& Then
      Exit Do
      Else
      xChar2 = Mid(TimerEvSetts, xLong1 + 1&, xLong2 - xLong1 - 1&)
      xLong3 = InStr(xChar2, ",")
      If xLong3 = 0& Then
        xEventID(xEventCount) = Val(xChar2)
        If Not HWndList(1) = 0& Then
          Call SendMessage_String(HWndList(1), &H180, 0&, LoadResEx_String(3700& + xEventID(xEventCount)))
          End If
        xEventCount = xEventCount + 1&
        Else
        xEventID(xEventCount) = Val(Left(xChar2, xLong3 - 1&))
        xEventFile(xEventCount) = Trim(Mid(xChar2, xLong3 + 1&))
        If Not HWndList(1) = 0& Then
          Call SendMessage_String(HWndList(1), &H180, 0&, LoadResEx_String(3700& + xEventID(xEventCount)) & vbTab & xEventFile(xEventCount))
          End If
        xEventCount = xEventCount + 1&
        End If
      xLong1 = xLong2
      End If
    Loop
  If xEventCount = 0& Then
    SetDragDrop True
    Else
    If xEventCount = xTimerEvMaxSubEvents Then
      LoadResEx_Dialog_Enable xTimerEditingHWnd, 2740&, False
      SetDragDrop False
      End If
    If xEventCount > 1& Then
      LoadResEx_Dialog_Enable xTimerEditingHWnd, 2742&, True
      LoadResEx_Dialog_Enable xTimerEditingHWnd, 2743&, True
      End If
    LoadResEx_Dialog_Enable xTimerEditingHWnd, 2741&, True
    UpdateWidths
    End If
  End Sub

Private Sub UpdateWidths()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  'Dim xType1 As POINTAPI
  Dim xChar1 As String
  Static xLongA1(1) As Long

  Call SelectObject(xSharedDC, hFontGSys)
  For xLong1 = 0& To xEventCount - 1&
    xChar1 = LoadResEx_String(3700& + xEventID(xLong1))
    xLong4 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong4 > xLong2 Then xLong2 = xLong4
    'xTimerDisplayExtraLen = xLong4 + xTimerDisplaySpace1
    If HasSubFile(xEventID(xLong1)) Then
      xLong4 = GetTextOutWidth(xSharedDC, xEventFile(xLong1))
      If xLong4 > xLong3 Then xLong3 = xLong4
      End If
    Next
  If xLong3 = 0& Then
    If Not HWndList(1) = 0& Then
      Call SendMessage(HWndList(1), &H192&, 0&, 0&) 'xLongA1(0))
      If xLong2 = 0& Then
        Call SendMessage(HWndList(1), &H194&, 0&, 0&)
        Else
        Call SendMessage(HWndList(1), &H194&, xLong2 + 4&, 0&)
        End If
      End If
    Else
    xLongA1(0) = 0&
    xLongA1(1) = (xLong2 + 16&) * xGlobalDialogUnitsGFix
    If Not HWndList(1) = 0& Then
      Call SendMessage(HWndList(1), &H192&, 2&, VarPtr(xLongA1(0)))
      Call SendMessage(HWndList(1), &H194&, xLong2 + xLong3 + 16& + 4&, 0&)
      End If
    'Else
    End If
  End Sub

Private Function GetBrowsedItem(ByVal ForID As Long, ByVal ByButton As Long, ByVal GivenDir As String) As String
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xBool1 As Boolean
  Dim xChar1 As String
  
  Select Case ForID
    Case KbdC_FAddPList, KbdC_FLoadPL
      xLong2 = 0&
      xBool1 = True
    Case KbdC_FSavePLas
      xLong2 = 0&
    Case KbdC_OTimerImport
      xLong2 = 2&
      xBool1 = True
    Case KbdC_OTimerExport
      xLong2 = 2&
    Case KbdC_OPresetSave
      xLong2 = 1&
    Case KbdC_OPresetLoad
      xLong2 = 1&
      xBool1 = True
    Case KbdC_FAddDir, KbdC_FAddFileS
      xLong2 = 3&
    Case Else
      xLong2 = -1&
    End Select
  If xLong2 = 3& Then
    If ForID = KbdC_FAddFileS Then
      If Not xSecureMode Then
        If xUsedTypesResort Then MainForm_ResortTypes xUsedTypes
        xChar1 = _
                 Replace(LoadResEx_String(1201&), "$", Replace(Mid(xUsedTypes, 2&, Len(xUsedTypes) - 2&), vbNullChar, "/")) & vbNullChar & _
                 "*." & Replace(Mid(xUsedTypes, 2&, Len(xUsedTypes) - 2&), vbNullChar, ";*.") & vbNullChar & _
                 Replace(LoadResEx_String(1202&), "$", Replace(Mid(xSupportedFiles, 2&, Len(xSupportedFiles) - 2&), vbNullChar, "/")) & vbNullChar & _
                 "*." & Replace(Mid(xSupportedFiles, 2&, Len(xSupportedFiles) - 2&), vbNullChar, ";*.") & vbNullChar & _
                 vbNullChar
        xChar1 = CalaymLoadSingleFile(xChar1, LoadResEx_String(1200&), xLastDirectory, xTimerEditingHWnd, False)
        If Not Len(xChar1) = 0& Then
          xLastDirectory = SimpleDir(xChar1)
          If AllowedItem(xChar1, True) = 1& Then
            GetBrowsedItem = xChar1
            End If
          End If
        End If
      Else 'If Not xSecureMode Then
      xChar1 = CommonDLG_OpenPath(xTimerEditingHWnd, LoadResEx_String(1210&), GivenDir)
      If Not Len(xChar1) = 0& Then
        xLastDir = xChar1
        If Len(xChar1) = 3 Then xChar1 = Left(xChar1, 2&)
        If AllowedItem(xChar1, True) = 0& Or Len(xChar1) = 2& Then
          GetBrowsedItem = xChar1
          End If
        End If
      End If
    Else
    If Not xLong2 = -1& Then
      Select Case ByButton
        Case 5&
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), HWndList(1))
        Case 4&
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), 0&)
        Case 3&
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), 0&)
        Case 2&
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), GetDlgItem(xTimerEditingHWnd, 2740&))
        Case 1&
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), GetDlgItem(xTimerEditingHWnd, 2761&))
        Case Else
          xLong3 = GetAPIPopupMenu(xTimerEditingHWnd, xTimerPLMenu(xLong2), HWndList(0))
        End Select
      Select Case xLong3
        Case 2700&
          If Not xSecureMode Then
 ' playlistformatsimport
            xChar1 = CalaymLoadSingleFile(GetPLTypes(False), LoadResEx_String(1220&), m3uList, xTimerEditingHWnd, False)
            If Not Len(xChar1) = 0& Then
              m3uList = SimpleDir(xChar1)
              If AllowedItem(xChar1, xBool1) = 255& Then
                GetBrowsedItem = xChar1
                End If
              End If
            End If
        Case 2720&
          If Not xSecureMode Then
            xChar1 = CalaymLoadSingleFile(LoadResEx_String(1605&) + vbNullChar + "*.cpt", LoadResEx_String(1600&), TimerDir, xTimerEditingHWnd, False)
            If Not Len(xChar1) = 0& Then
              TimerDir = SimpleDir(xChar1)
              If AllowedItem(xChar1, xBool1) = 254& Then
                GetBrowsedItem = xChar1
                End If
              End If
            End If
        Case 2710&
          If Not xSecureMode Then
            xChar1 = CalaymLoadSingleFile(LoadResEx_String(1655&) & vbNullChar & "*.cps" & vbNullChar & LoadResEx_String(1656&) & vbNullChar & "*.ini", LoadResEx_String(1650&), PresetFile, xTimerEditingHWnd, False)
            If Not Len(xChar1) = 0& Then
              PresetFile = SimpleDir(xChar1)
              If AllowedItem(xChar1, xBool1) = 253& Then
                GetBrowsedItem = xChar1
                End If
              End If
            End If
        Case 3000& + xLong2 * 100& To 3099& + xLong2 * 100&
          GetBrowsedItem = vPLXMain + vPLX(vPLXMax * xLong2 + xLong3 - (3000& + xLong2 * 100&))
        End Select
      End If
    End If
  End Function

Private Sub AddNow(ByVal ByButton As Long)
  Dim xLong1 As Long
  Dim xChar1 As String
  
  If Not xEventCount = xTimerEvMaxSubEvents Then
    If GetSelected(0&, xLong1) Then
      If ValidSetupKey(xEvAlias(xLong1)) Then
        If HasSubFile(xEvAlias(xLong1)) Then
          xChar1 = GetBrowsedItem(xEvAlias(xLong1), ByButton, xLastDir)
          If Len(xChar1) = 0& Then
            Exit Sub
            Else
            xEventFile(xEventCount) = xChar1
            If Not HWndList(1) = 0& Then
              Call SendMessage_String(HWndList(1), &H180, 0&, LoadResEx_String(3700& + xEvAlias(xLong1)) & vbTab & xEventFile(xEventCount))
              End If
            SetDlgItemText xTimerEditingHWnd, 2760&, xEventFile(xEventCount)
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, False
            End If
          Else
          If Not HWndList(1) = 0& Then
            Call SendMessage_String(HWndList(1), &H180, 0&, LoadResEx_String(3700& + xEvAlias(xLong1)))
            End If
          SetDlgItemText xTimerEditingHWnd, 2760&, ""
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2761&, False
          End If
          
        xEventID(xEventCount) = xEvAlias(xLong1)
        LoadResEx_Dialog_ListIndexMS xTimerEditingHWnd, 2750&, xEventCount
        ListSelChange xEventCount

        xEventCount = xEventCount + 1&
        UpdateWidths
        
        If xEventCount = xTimerEvMaxSubEvents Then
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2740&, False
          SetDragDrop False
          ElseIf xEventCount = 1& Then
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2741&, True
          ElseIf xEventCount = 2& Then
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2742&, True
          LoadResEx_Dialog_Enable xTimerEditingHWnd, 2743&, True
          End If
        End If
      End If
    End If
  End Sub

Private Sub ToggleEvents(ByVal OrigIndex As Long, ByVal NewIndex As Long)
  Dim xChar1 As String
  Dim xLong1 As Long
  
  xChar1 = xEventFile(OrigIndex)
  xEventFile(OrigIndex) = xEventFile(NewIndex)
  xEventFile(NewIndex) = xChar1
  
  xLong1 = xEventID(OrigIndex)
  xEventID(OrigIndex) = xEventID(NewIndex)
  xEventID(NewIndex) = xLong1
  
  'xChar1 = lstRes(2750).List(OrigIndex)
  'lstRes(2750).List(OrigIndex) = lstRes(2750).List(NewIndex)
  'lstRes(2750).List(NewIndex) = xChar1
  
  'xChar1 = lstRes(2750).List(OrigIndex)
  If Not HWndList(1) = 0& Then
    xChar1 = LoadResEx_Dialog_ListItemGet(HWndList(1), OrigIndex)
  
    LoadResEx_Dialog_ListItemSet HWndList(1), OrigIndex, LoadResEx_Dialog_ListItemGet(HWndList(1), NewIndex), False
'  Call SendMessage(HWndList(1), &H182, OrigIndex, 0&)
'  Call SendMessage_String(HWndList(1), &H181, OrigIndex, xChar1)
    LoadResEx_Dialog_ListItemSet HWndList(1), NewIndex, xChar1, False
  
  'lstRes(2750).List(OrigIndex) = lstRes(2750).List(NewIndex)
  'lstRes(2750).List(NewIndex) = xChar1
  
    LoadResEx_Dialog_ListIndexMS xTimerEditingHWnd, 2750&, NewIndex
    End If
  End Sub

  Private Function LoadResEx_Dialog_TimeLongA(ByVal HWndDialog As Long, ByVal ControlID As Long, ByRef Value As Long, Optional ByVal ReadFromDialog As Boolean = False) As Boolean
    Dim xChar1 As String
    'Dim xTypeA1(1) As SystemTime
    'Dim xType1 As SystemTime
  
    ControlID = GetDlgItem(HWndDialog, ControlID)
    If Not ControlID = 0& Then
      With sSystemTime '(0)
        If ReadFromDialog Then
          Call SendMessage(ControlID, &H1001&, 0&, VarPtr(sSystemTime))
          ControlID = (.wHour * 60& + .wMinute) '* 1000&
          If Not ControlID = Value Then
            LoadResEx_Dialog_TimeLongA = True
            Value = ControlID
            End If
          Else
          xChar1 = "HH':'mm"
          If IsUnicode Then
            Call SendMessage_String(ControlID, &H1032&, 0&, xChar1)
            Else
            Call SendMessage_String(ControlID, &H1005&, 0&, xChar1)
            End If
          'Call SendMessage(ControlID, &H1004&, 3&, VarPtr(xTypeA1(0)))
          If Value = -1& Then
            Call SendMessage(ControlID, &H1002&, 2&, VarPtr(sSystemTime))
            Else
            .wYear = 2000&
            .wMonth = 1
            .wDay = 1
            .wHour = Value \ 60&
            .wMinute = (Value) Mod 60&
            Call SendMessage(ControlID, &H1002&, 0&, VarPtr(sSystemTime))
            End If
          End If
        End With
      End If
    End Function

Private Sub SetDragDrop(ByVal Enabled As Boolean)
  Static xOldMode As Boolean
  
  If Not (Enabled = xOldMode Or HWndList(1) = 0&) Then
    Call DragAcceptFiles(HWndList(1), -CLng(Enabled))
    xOldMode = Enabled
    End If
  End Sub

Private Function TimerEditKBDProc(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&
          RaiseButton 2741&
        Case 93&
          If (lParam And &H40000000) = 0& Then
            TrySpecialKey
            End If
        Case 112&
          RunTimerHelp
        Case 9&
          LoadResEx_Dialog_ManualTab xTimerEditingHWnd, hWnd
          Exit Function
        Case 27&
          WantsQuit
        End Select
    Case &H87&
      TimerEditKBDProc = 4&
      Exit Function
    Case &H203& 'WM_LBUTTONDBLCLK
      If IsUnicode Then
        TimerEditKBDProc = CallWindowProcW(TimerEditKBDProcOld, hWnd, Message, wParam, lParam)
        Else
        TimerEditKBDProc = CallWindowProcA(TimerEditKBDProcOld, hWnd, Message, wParam, lParam)
        End If
      RaiseButton 2741&
      GoTo NoProc
    Case &H205& 'Mouseup
      EditListItem1 lParam
    End Select
  If IsUnicode Then
    TimerEditKBDProc = CallWindowProcW(TimerEditKBDProcOld, hWnd, Message, wParam, lParam)
    Else
    TimerEditKBDProc = CallWindowProcA(TimerEditKBDProcOld, hWnd, Message, wParam, lParam)
    End If
NoProc:
  End Function

Private Sub EditListItem1(ByVal lParam As Long)
  Call TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), HWndList(1), ((lParam And &HFFFF0000) \ &H10000), False, 0&, 0&)
  If GetSelected(1&, lParam) Then
    If HasSubFile(xEventID(lParam)) Then
      UpdateByMenu lParam, 4&
      End If
    End If
  End Sub

Private Sub TrySpecialKey()
  Dim xLong1 As Long

  If GetSelected(1&, xLong1) Then
    If HasSubFile(xEventID(xLong1)) Then
      UpdateByMenu xLong1, 5&
      End If
    End If
  End Sub

Private Function TimerEditKBDProcLite(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 13&
          AddNow 0&
        Case 93&
          If (lParam And &H40000000) = 0& Then
            'RaiseButton TrackListBox(HWndAutoList, 0&, True, xMenu, 1&)
'            RaiseButton GetAPIPopupMenu(xTimerEditingHWnd, xMenu, HWndAutoList)
            End If
        Case 112&
          RunTimerHelp
        Case 9&
          LoadResEx_Dialog_ManualTab xTimerEditingHWnd, hWnd
          Exit Function
        Case 27&
          WantsQuit
        End Select
    Case &H87&
      TimerEditKBDProcLite = 4&
      Exit Function
    Case &H203& 'WM_LBUTTONDBLCLK
      If IsUnicode Then
        TimerEditKBDProcLite = CallWindowProcW(TimerEditKBDProcOldLite, hWnd, Message, wParam, lParam)
        Else
        TimerEditKBDProcLite = CallWindowProcA(TimerEditKBDProcOldLite, hWnd, Message, wParam, lParam)
        End If
      AddNow 3&
      GoTo NoProc
    Case &H205& 'mouseup
      Call TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), HWndList(0), ((lParam And &HFFFF0000) \ &H10000), False, 0&, 0&)
    End Select
  If IsUnicode Then
    TimerEditKBDProcLite = CallWindowProcW(TimerEditKBDProcOldLite, hWnd, Message, wParam, lParam)
    Else
    TimerEditKBDProcLite = CallWindowProcA(TimerEditKBDProcOldLite, hWnd, Message, wParam, lParam)
    End If
NoProc:
  End Function

Private Function TimerEditTxtProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H100& To &H102&
      Select Case wParam
        Case 61&
          wParam = 58& '59&
          'Exit Function
        End Select
    End Select
  If IsUnicode Then
    TimerEditTxtProc = CallWindowProcW(TimerEditTxtProcOld, hWnd, Message, wParam, lParam)
    Else
    TimerEditTxtProc = CallWindowProcA(TimerEditTxtProcOld, hWnd, Message, wParam, lParam)
    End If
  End Function

Private Sub WantsQuit()
  If xEventCount = 0& Then
    SendMessage xTimerEditingHWnd, &H471&, 5&, 0&
    End If
  End Sub

Private Sub DropFiles(ByVal hDrop As Long) ', ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  'Debug.Print "DropFiles", hDrop, "timeredit"
  
  Dim xChar1 As String
  If Not xEventCount = xTimerEvMaxSubEvents Then
    If DragQueryFileCount(hDrop) = 1& Then
      If DragQueryFile(hDrop, 0&, xChar1) Then ' Data.Files(1)
        xEventFile(xEventCount) = xChar1
        Select Case AllowedItem(xEventFile(xEventCount), True)
          Case 255& 'playlist
            xEventID(xEventCount) = KbdC_FLoadPL
          Case 254& 'timer
            xEventID(xEventCount) = KbdC_OTimerImport
          Case 253& 'settings/preset
            xEventID(xEventCount) = KbdC_OPresetLoad
          Case 0& 'dir
            xEventID(xEventCount) = KbdC_FAddDir
          Case 1& ' supp-file
            xEventID(xEventCount) = KbdC_FAddFileS
          Case Else
            xEventID(xEventCount) = -1&
          End Select
        If Not xEventID(xEventCount) = -1& Then
          'lstRes(2750).AddItem LoadResEx_String(3700& + xEventID(xEventCount)) & vbTab & xEventFile(xEventCount)
          Call SendMessage_String(HWndList(1), &H180, 0&, LoadResEx_String(3700& + xEventID(xEventCount)) & vbTab & xEventFile(xEventCount))
          'txtRes(2760).Text = xEventFile(xEventCount)
          SetDlgItemText xTimerEditingHWnd, 2760&, xEventFile(xEventCount)
          'lstRes(2750).ListIndex = xEventCount
          LoadResEx_Dialog_ListIndexMS xTimerEditingHWnd, 2750&, xEventCount
          xEventCount = xEventCount + 1&
          
          UpdateWidths
          
          If xEventCount = xTimerEvMaxSubEvents Then
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2740&, False
            SetDragDrop False
            ElseIf xEventCount = 1& Then
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2741&, True
            ElseIf xEventCount = 2& Then
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2742&, True
            LoadResEx_Dialog_Enable xTimerEditingHWnd, 2743&, True
            End If
          End If
        End If
      End If
    End If
  DragFinish hDrop
  End Sub

Private Sub RunTimerHelp()
  If Not (xSecureMode Or vKioskMode) Then
    If xUseChmHelp Then
      If FileExists(xRealBaseFile + "chm") Then
        Call HtmlHelp(MainHWnd, xRealBaseFile + "chm", 0&, "timers.htm")
        End If
      ElseIf FileExists(xRealBaseFile + "hlp") Then
      Call WinHelp(MainHWnd, xRealBaseFile + "hlp", &H101&, "Timers")
      End If
    End If
  End Sub
