; Calaym - Media player GUI for sound systems
; Copyright (C) 2002-2014 Jan Vorel
;
; You can redistribute the official software packages under the terms of the
; User-friendly Distribution License Version 2.00, published by Jan Vorel
;
; You can use this software under the terms of the
; User-friendly Open-source 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.

EnableExplicit

Global AutoRefresh_DialogKBDProcOld.i
Global AutoRefresh_PopupMenu.i
Global AutoRefresh_Menu.i

Global AutoRefresh_xLastDir.s

Global Dim AutoRefres_xDir.s(#xAutoDirCountMaxX)
Global AutoRefresh_xDirCount.i

Global AutoRefresh_HWndDialog.i
Global AutoRefresh_HWndList.i
;Global AR_DragClass.IDropTarget
Global AutoRefresh_OldDragMode.i

Procedure AutoRefresh_SetDragDrop(Enabled)
  If AutoRefresh_HWndList
    If Enabled XOr AutoRefresh_OldDragMode
        DragAcceptFiles_(AutoRefresh_HWndList, Enabled)
      ;If Enabled
        ;Set AR_DragClass = New IDropTargetClass
        ;AR_DragClass.DragOver($F0F00F0F, AddressOf DropFiles, 0, DROPEFFECT_NONE)
        ;RegisterDragDrop(AutoRefresh_HWndList, ObjPtr(AR_DragClass))
        ;Else
        ;RevokeDragDrop(AutoRefresh_HWndList)
        ;DragAcceptFiles_(AutoRefresh_HWndList, #False)
        ;Set AR_DragClass = Nothing
        ;EndIf
      AutoRefresh_OldDragMode = Enabled
      EndIf
    EndIf
  EndProcedure

Procedure AutoRefresh_SetSize()
  Define.i xLong1
  Define.i xLong2
  Define.i xLong3

  If AutoRefresh_xDirCount = #xAutoDirCountMax
    xLong1 = #False
    xLong2 = 3
    Else
    xLong1 = #True
    xLong2 = 0
    EndIf
  AutoRefresh_SetDragDrop(xLong1)
  EnableMenuItem_(AutoRefresh_Menu, 2420, xLong2)
  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2420, xLong1)
  
  If AutoRefresh_xDirCount = 0
    xLong1 = #False
    xLong2 = 3
    Else
    xLong1 = #True
    xLong2 = 0
    EndIf
  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2421, xLong1)
  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2422, xLong1)

;  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2450, xLong1)
;  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2460, xLong1)

  EnableMenuItem_(AutoRefresh_Menu, 2421, xLong2)
  EnableMenuItem_(AutoRefresh_Menu, 2422, xLong2)

  SelectObject_(xSharedDC, hFontGSys)
  For xLong1 = 0 To AutoRefresh_xDirCount - 1
    xLong2 = GetTextOutWidth(xSharedDC, AutoRefres_xDir(xLong1))
    If xLong2 > xLong3
      xLong3 = xLong2
      EndIf
    Next
  If AutoRefresh_HWndList
    SendMessage_(AutoRefresh_HWndList, $194, #lWidth + xLong3, 0)
    EndIf
  EndProcedure

Procedure AutoRefresh_AddDirNow(xChar1.s)
  Define.i xBool1
  Define.i xLong1
  Define.i xLong2
  Define.i xLong3
  Define.s xChar2

  xChar1 = LCase(xChar1)
  xChar2 = xChar1 + "\"
  For xLong1 = 0 To AutoRefresh_xDirCount - 1
    If xChar1 = AutoRefres_xDir(xLong1)
      xBool1 = #True
      Break
      EndIf
    If xChar2 = Left(AutoRefres_xDir(xLong1), Len(xChar2))
      ;mod & clean others
      AutoRefres_xDir(xLong1) = xChar1
      
      SendMessage_(AutoRefresh_HWndList, #LB_DELETESTRING, xLong1, 0)
      SendMessage_(AutoRefresh_HWndList, #LB_INSERTSTRING, xLong1, @xChar1)
      SendMessage_(AutoRefresh_HWndList, #LB_SETSEL, 1, xLong1)
      xLong1 = xLong1 + 1
      For xLong2 = xLong1 To AutoRefresh_xDirCount - 1
        If xChar2 = Left(AutoRefres_xDir(xLong2), Len(xChar2))
          SendMessage_(AutoRefresh_HWndList, #LB_DELETESTRING, xLong1, 0)
          Else
          For xLong3 = xLong2 To AutoRefresh_xDirCount - 1
            AutoRefres_xDir(xLong1) = AutoRefres_xDir(xLong3)
            xLong1 + 1
            Next
          Break
          EndIf
        Next
      AutoRefresh_xDirCount = xLong1
      xBool1 = #True
      Break
      ElseIf Left(xChar1, Len(AutoRefres_xDir(xLong1)) + 1) = AutoRefres_xDir(xLong1) + "\"
      xBool1 = #True
      Break
      EndIf
    Next
  
  If Not xBool1
    xLong2 = AutoRefresh_xDirCount
    For xLong1 = AutoRefresh_xDirCount - 1 To 0 Step -1
      If xChar1 + Chr(1) < AutoRefres_xDir(xLong1) + Chr(1)
        xLong2 = xLong1
        EndIf
      Next
    For xLong1 = AutoRefresh_xDirCount To xLong2 + 1 Step -1
      AutoRefres_xDir(xLong1) = AutoRefres_xDir(xLong1 - 1)
      Next
    AutoRefres_xDir(xLong2) = xChar1
    
    SendMessage_(AutoRefresh_HWndList, #LB_INSERTSTRING, xLong2, @xChar1)
    SendMessage_(AutoRefresh_HWndList, #LB_SETSEL, 1, xLong2)
    
    AutoRefresh_xDirCount + 1
    EndIf
  EndProcedure

Procedure AutoRefresh_AddBrowsedDir()
  Define.s xChar1

  xChar1 = PathRequester(LoadResString(2420), AutoRefresh_xLastDir)
  If Right(xChar1, 1) = "\"
    xChar1 = Left(xChar1, Len(xChar1) - 1)
    EndIf
  If Len(xChar1)
    AutoRefresh_xLastDir = xChar1
    ;If Len(xChar1) = 3
    ;  xChar1 = Left(xChar1, 2)
    ;  EndIf
    If AllowedItem(xChar1, #True) = 0
      SendMessage_(AutoRefresh_HWndList, #LB_SETSEL, 0, -1)
      AutoRefresh_AddDirNow(xChar1)
      AutoRefresh_SetSize()
      EndIf
    EndIf
  EndProcedure

Procedure AutoRefresh_ClearSome()
  Define.i xLong1
  Define.i xLong2
  ;Dim xBool1 As Boolean
  
  For xLong1 = 0 To AutoRefresh_xDirCount - 1
    If SendMessage_(AutoRefresh_HWndList, #LB_GETSEL, xLong2, 0) = 0
      AutoRefres_xDir(xLong2) = AutoRefres_xDir(xLong1)
      xLong2 + 1
      Else
      SendMessage_(AutoRefresh_HWndList, #LB_DELETESTRING, xLong2, 0)
      ;xBool1 = #True
      EndIf
    Next
  If Not AutoRefresh_xDirCount = xLong2
    AutoRefresh_xDirCount = xLong2
    AutoRefresh_SetSize()
    EndIf
  EndProcedure

Procedure AutoRefresh_RaiseButton(ID.i)
  Define.i xLong1
  Define.i xLong2

  Select ID
    Case 2420
      If AutoRefresh_xDirCount < #xAutoDirCountMax
        AutoRefresh_AddBrowsedDir()
        EndIf
      SetFocus_(AutoRefresh_HWndList)
    Case 2421
      AutoRefresh_ClearSome()
      SetFocus_(AutoRefresh_HWndList)
    Case 2422
      AutoRefresh_xDirCount = 0
      If Not AutoRefresh_HWndList = 0
        SendMessage_(AutoRefresh_HWndList, $184, 0, 0)
        SetFocus_(AutoRefresh_HWndList)
        AutoRefresh_SetSize()
        EndIf
    Case 2450, 2451, 2460
      If ID = 2450 Or ID = 2460
        If IsDlgButtonChecked_(AutoRefresh_HWndDialog, ID)
          xLong1 = #True
          EndIf
        LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, ID + 2, xLong1)
        LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, ID + 1, xLong1)
        Else
        ID = ID & $fffe ; round down to 10... or here by clearing 2451 to 2450
        If IsDlgButtonChecked_(AutoRefresh_HWndDialog, 2450)
          xLong1 = #True
          EndIf
        EndIf
      If Not ID = 2460
        ID = GetDlgItem_(AutoRefresh_HWndDialog, 2462)
        If xLong1
          xLong1 = GetDlgItem_(AutoRefresh_HWndDialog, 2452)
          If xLong1 = 0
            xLong1 = xAutoRefreshTimer(0)
            Else
            xLong1 = SendMessage_(xLong1, $468, 0, 0)
            EndIf
          If Not ID = 0
            SendMessage_(ID, $465, 0, (#xAutoRefreshTimeMin * $10000 + xLong1))
            xLong2 = SendMessage_(ID, $468, 0, 0)
            If xLong2 > xLong1
              SendMessage_(ID, $467, 0, (#xAutoRefreshTimeMin * $10000 + xLong2))
              EndIf
            EndIf
          
          Else
          If Not ID = 0
            SendMessage_(ID, $465, 0, (#xAutoRefreshTimeMin * $10000 + #xAutoRefreshTimeMax))
            EndIf
          EndIf
        EndIf
    ;Case 2460
    ;  Debug.Print "Check2"
    Case 2490
      If Not xAutoDoing
        For xLong1 = 0 To 1
          ID = GetDlgItem_(AutoRefresh_HWndDialog, 2452 + xLong1 * 10)
          If Not ID = 0
            xAutoRefreshTimer(xLong1) = -SendMessage_(ID, $468, 0, 0)
            EndIf
          ID = GetDlgItem_(AutoRefresh_HWndDialog, 2450 + xLong1 * 10)
          If Not ID = 0
            If IsDlgButtonChecked_(AutoRefresh_HWndDialog, 2450 + xLong1 * 10)
              xAutoRefreshTimer(xLong1) = -xAutoRefreshTimer(xLong1)
              EndIf
            EndIf
          Next
        
        xAutoDirCount = 0
        For ID = 0 To AutoRefresh_xDirCount - 1
          xAutoDir(ID) = AutoRefres_xDir(ID)
          Next
        xAutoDirCount = AutoRefresh_xDirCount
        If xAutoDirCount
          xLong1 = #True
          Else
          xLong1 = #False
          EndIf
        MainForm_EnableApiMenu(411, xLong1)
        MainForm_EnableApiMenu(412, xLong1)
        MainForm_EnableApiMenu(413, xLong1)
        MainForm_EnableApiMenu(414, xLong1)
        AutoRefresh_SetDragDrop(#False)
        EndDialog_(AutoRefresh_HWndDialog, 2)
        EndIf
    Case 2491
      AutoRefresh_SetDragDrop(#False)
      EndDialog_(AutoRefresh_HWndDialog, 1)
    EndSelect
  EndProcedure

Procedure.i AutoRefresh_DropFiles(hDrop.i);, Message.i, wParam.i, lParam.i)
  Define.i xLong1
  Define.i xLong2
  Define.s xChar1
  Define.i xBool1
  
  xLong2 = DragQueryFileCount(hDrop) - 1
  For xLong1 = 0 To xLong2
    xChar1 = DragQueryFile(hDrop, xLong1)
    If Len(xChar1)
      If Len(xChar1) = 3
        xChar1 = Left(xChar1, 2)
        EndIf
      If AllowedItem(xChar1, #True) = 0
        If Not xBool1
          SendMessage_(AutoRefresh_HWndList, #LB_SETSEL, 0, -1)
          xBool1 = #True
          EndIf
        AutoRefresh_AddDirNow(xChar1)
        If AutoRefresh_xDirCount = #xAutoDirCountMax
          Break
          EndIf
        EndIf
      EndIf
    Next
  DragFinish_(hDrop)
  If xBool1
    AutoRefresh_SetSize()
    EndIf
  ProcedureReturn 0
  EndProcedure

Procedure.i AutoRefresh_DialogKBDProc(hWnd.i, Message.i, wParam.i, lParam.i)
  Select Message
    Case $100
      Select wParam
        Case 46
          AutoRefresh_ClearSome()
        Case 93
          If (lParam & $40000000) = 0
            AutoRefresh_RaiseButton(GetAPIPopupMenu(AutoRefresh_HWndDialog, AutoRefresh_Menu, AutoRefresh_HWndList))
            EndIf
        Case 9
          LoadResEx_Dialog_ManualTab(AutoRefresh_HWndDialog, hWnd)
          ProcedureReturn 0
        Case 27
          SendMessage_(AutoRefresh_HWndDialog, $471, 5, 0)
        EndSelect
    Case $87
      ProcedureReturn 4
    Case $205
      AutoRefresh_RaiseButton(TrackListBox(xStaticIconsHWnd(#xStaticIcons_ShowIR), AutoRefresh_HWndList, SignedHigherWord(lParam), #True, AutoRefresh_Menu, 1))
    EndSelect
  ProcedureReturn CallWindowProc_(AutoRefresh_DialogKBDProcOld, hWnd, Message, wParam, lParam)
  EndProcedure

Procedure AutoRefresh_Initialize()
  Define.i xLong1
  Define.i xLong2
  Define.i xLong3
  
  
  AutoRefresh_Menu = LoadResEx_Menu(2400)
  AutoRefresh_PopupMenu = MakePopupMenu(AutoRefresh_Menu)


  If xAutoRefreshTimer(0) > 0
    xLong1 = #True
    xLong3 = #BST_CHECKED
    Else
    LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2451, #False)
    xLong3 = #BST_UNCHECKED
    EndIf
  If xAutoRefreshTimer(1) > 0
    LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2452, xLong1)
    EndIf
  CheckDlgButton_(AutoRefresh_HWndDialog, 2450, xLong3)


  If xAutoRefreshTimer(0) > 0
    xLong2 = xAutoRefreshTimer(0)
    xLong3 = xAutoRefreshTimer(0)
    Else
    xLong2 = #xAutoRefreshTimeMax
    xLong3 = -xAutoRefreshTimer(0)
    EndIf
  
  xLong1 = GetDlgItem_(AutoRefresh_HWndDialog, 2452)
  If Not xLong1 = 0
    SendMessage_(xLong1, $465, 0, (#xAutoRefreshTimeMinAll * $10000 + #xAutoRefreshTimeMaxAll))
    SendMessage_(xLong1, $467, 0, xLong3)
    EndIf
  
  If xAutoRefreshTimer(1) > 0
    xLong3 = xAutoRefreshTimer(1)
    Else
    xLong3 = -xAutoRefreshTimer(1)
    EndIf
  xLong1 = GetDlgItem_(AutoRefresh_HWndDialog, 2462)
  If xLong1
    SendMessage_(xLong1, $465, 0, (#xAutoRefreshTimeMin * $10000 + xLong2))
    SendMessage_(xLong1, $467, 0, xLong3)
    EndIf

  If xAutoRefreshTimer(1) > 0
    xLong1 = #True
    Else
    xLong1 = #False
    EndIf
  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2461, xLong1)
  LoadResEx_Dialog_Enable(AutoRefresh_HWndDialog, 2462, xLong1)
  If xLong1
    xLong1 = #BST_CHECKED
    EndIf
  CheckDlgButton_(AutoRefresh_HWndDialog, 2460, xLong1)
  
  
  
  
  
  
  ;lDirList.Clear
  AutoRefresh_xDirCount = xAutoDirCount
  AutoRefresh_HWndList = GetDlgItem_(AutoRefresh_HWndDialog, 2400)
  If Not AutoRefresh_HWndList = 0
    AutoRefresh_DialogKBDProcOld = SetWindowLong_(AutoRefresh_HWndList, -4, @AutoRefresh_DialogKBDProc())
    If Not AutoRefresh_xDirCount = 0
      For xLong1 = 0 To AutoRefresh_xDirCount - 1
        AutoRefres_xDir(xLong1) = xAutoDir(xLong1)
        SendMessage_(AutoRefresh_HWndList, $180, 0, @AutoRefres_xDir(xLong1))
        Next
      SendMessage_(AutoRefresh_HWndList, #LB_SETSEL, 1, 0)
      EndIf
    EndIf
  
  AutoRefresh_SetSize()
  EndProcedure

Procedure.i AutoRefresh_MainProc(hWnd.i, Message.i, wParam.i, lParam.i)
  Select Message
    Case $111
       AutoRefresh_RaiseButton(wParam & $FFFF)
    Case $233
      ;DoDrops wParam
      AutoRefresh_DropFiles(wParam);, 0, 0, 0)
    Case $10
      AutoRefresh_SetDragDrop(#False)
      EndDialog_(hWnd, 1)
    Case $110
      AutoRefresh_HWndDialog = hWnd
      AutoRefresh_Initialize()
      ProcedureReturn 1
    EndSelect
  ProcedureReturn 0
  EndProcedure

Procedure AutoRefresh_ShowDialog()
  Define.i xLong1

  DialogTimer(#True)
  xLong1 = DialogBoxParam_(hInstance, 2400, MainHWnd, @AutoRefresh_MainProc(), 0)
  If AutoRefresh_Menu
    DestroyMenu_(AutoRefresh_Menu)
    If AutoRefresh_PopupMenu
      DestroyMenu_(AutoRefresh_PopupMenu)
      EndIf
    AutoRefresh_Menu = 0
    EndIf
  DialogTimer(#False)
  For AutoRefresh_xDirCount = 0 To #xAutoDirCountMaxX
    AutoRefres_xDir(AutoRefresh_xDirCount) = ""
    Next
  AutoRefresh_xDirCount = 0
  AutoRefresh_OldDragMode = #False
  If xLong1 = 2
    MainForm_xAutoRefreshNow(#False, #True, #False, #False, #True)
    EndIf
  EndProcedure

; IDE Options = PureBasic 5.21 LTS (Windows - x64)
; CursorPosition = 21
; FirstLine = 15
; Folding = --
; EnableUnicode
; EnableXP
; AddResource = res\exe64.rc