Attribute VB_Name = "modul_Calaym_AutoRefresh"
' 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 CanEnumComputerShares = False 'also frmPlayer

Private AutoRefreshDialogKBDProcOld As Long
Private xPopupMenu As Long
Private xMenu As Long

Private xLastDir As String

Private xDir(xAutoDirCountMaxX) As String
Private xDirCount As Long

Private HWndDialog As Long
Private HWndAutoList As Long
Private AR_DragClass As IDropTarget

Public Sub ShowAutoRefresh()
  Dim xLong1 As Long

  DialogTimer True
  xLong1 = DialogBoxIndirectParam(2400&, MainHWnd, AddressOf AutoRefreshProc, 0&)
  If Not xMenu = 0& Then
    DestroyMenu xMenu
    If Not xPopupMenu = 0& Then DestroyMenu xPopupMenu
    xMenu = 0&
    End If
  DialogTimer False
  Erase xDir()
  xDirCount = 0&
  If xLong1 = 2& Then
    MainForm_xAutoRefreshNow False, True, False, False, True
    End If
  End Sub

Private Function AutoRefreshProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H111&
       RaiseButton (wParam And &HFFFF&)
    'Case &H233&
    '  DoDrops wParam
    Case &H10&
      SetDragDrop False
      EndDialog hWnd, 1&
    Case &H110&
      HWndDialog = hWnd
      Initialize
      AutoRefreshProc = 1&
    End Select
  End Function

Private Sub Initialize()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  
  
  xMenu = LoadResEx_Menu(2400)
  xPopupMenu = LoadResEx_Menu_MakePopupMenu(xMenu)
  
  If xAutoRefreshTimer(0) > 0& Then
    xLong2 = xAutoRefreshTimer(0)
    xLong3 = xAutoRefreshTimer(0)
    Else
    xLong2 = xAutoRefreshTimeMax
    xLong3 = -xAutoRefreshTimer(0)
    End If
  
  xLong1 = GetDlgItem(HWndDialog, 2452&)
  If Not xLong1 = 0& Then
    Call SendMessage(xLong1, &H465&, 0&, (xAutoRefreshTimeMinAll * &H10000 + xAutoRefreshTimeMaxAll))
    Call SendMessage(xLong1, &H467&, 0&, xLong3)
    End If
  
  If xAutoRefreshTimer(0) > 0& Then
    Else
    LoadResEx_Dialog_Enable HWndDialog, 2451&, (xAutoRefreshTimer(0) > 0&)
    End If
  If xAutoRefreshTimer(1) > 0& Then
    LoadResEx_Dialog_Enable HWndDialog, 2452&, (xAutoRefreshTimer(0) > 0&)
    End If
  LoadResEx_Dialog_CheckBool HWndDialog, 2450&, (xAutoRefreshTimer(0) > 0&)
  
  If xAutoRefreshTimer(1) > 0& Then
    xLong3 = xAutoRefreshTimer(1)
    Else
    xLong3 = -xAutoRefreshTimer(1)
    End If
  xLong1 = GetDlgItem(HWndDialog, 2462&)
  If Not xLong1 = 0& Then
    Call SendMessage(xLong1, &H465&, 0&, (xAutoRefreshTimeMin * &H10000 + xLong2))
    Call SendMessage(xLong1, &H467&, 0&, xLong3)
    End If
  LoadResEx_Dialog_CheckBool HWndDialog, 2460&, (xAutoRefreshTimer(1) > 0&)
  LoadResEx_Dialog_Enable HWndDialog, 2461&, (xAutoRefreshTimer(1) > 0&)
  LoadResEx_Dialog_Enable HWndDialog, 2462&, (xAutoRefreshTimer(1) > 0&)
  
  
  'LoadResEx_Dialog_SpinnerLong(hwnd,2452&, ,false,)
  'Call ApplyTimer(0, xAutoRefreshTimeMinAll, xAutoRefreshTimeMaxAll, xAutoRefreshTimeDefAll)
  'Call ApplyTimer(1, xAutoRefreshTimeMin, xAutoRefreshTimeMax, xAutoRefreshTimeDef)
  
  
  
  
  
  'lDirList.Clear
  xDirCount = xAutoDirCount
  HWndAutoList = GetDlgItem(HWndDialog, 2400&)
  If Not HWndAutoList = 0& Then
    AutoRefreshDialogKBDProcOld = SetWindowLong(HWndAutoList, -4&, AddressOf AutoRefreshDialogKBDProc)
    If Not xDirCount = 0& Then
      For xLong1 = 0& To xDirCount - 1&
        xDir(xLong1) = xAutoDir(xLong1)
        Call SendMessage_String(HWndAutoList, &H180&, 0&, xDir(xLong1))
        Next
      Call SendMessage(HWndAutoList, &H185, 1&, 0&)
      End If
    End If
  
  SetSize
  End Sub

Private Sub SetSize()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long

  Call SelectObject(xSharedDC, hFontGSys)
  For xLong1 = 0& To xDirCount - 1&
    xLong2 = GetTextOutWidth(xSharedDC, xDir(xLong1))
    If xLong2 > xLong3 Then xLong3 = xLong2
    Next
  
  SetDragDrop (Not xDirCount = xAutoDirCountMax)
  
  
  LoadResEx_Dialog_Enable HWndDialog, 2420&, (Not xDirCount = xAutoDirCountMax)
  LoadResEx_Dialog_Enable HWndDialog, 2421&, (Not xDirCount = 0&)
  LoadResEx_Dialog_Enable HWndDialog, 2422&, (Not xDirCount = 0&)

  If xDirCount = xAutoDirCountMax Then
    Call EnableMenuItem(xMenu, 2420, 3&)
    Else
    Call EnableMenuItem(xMenu, 2420, 0&)
    End If
  If xDirCount = 0& Then
    Call EnableMenuItem(xMenu, 2421, 3&)
    Call EnableMenuItem(xMenu, 2422, 3&)
    Else
    Call EnableMenuItem(xMenu, 2421, 0&)
    Call EnableMenuItem(xMenu, 2422, 0&)
    End If

  If Not HWndAutoList = 0& Then
    Call SendMessage(HWndAutoList, &H194&, lWidth + xLong3, 0&)
    End If
  End Sub

Private Sub SetDragDrop(ByVal Enabled As Boolean)
  Static xOldMode As Boolean
  
  If Not (Enabled = xOldMode Or HWndAutoList = 0&) Then
    If Enabled Then
      Set AR_DragClass = New IDropTargetClass
      Call AR_DragClass.DragOver(&HF0F00F0F, AddressOf DropFiles, 0&, DROPEFFECT_NONE)
      Call RegisterDragDrop(HWndAutoList, ObjPtr(AR_DragClass))
      Else
      Call RevokeDragDrop(HWndAutoList)
      Set AR_DragClass = Nothing
      End If
    xOldMode = Enabled
    End If
  End Sub

'implements OLE

'  xNewValue(Index) = NewValue
'  If Index = 0 Then
'    ctVBox(1).ValueMax = NewValue
'    End If
'      Call SendMessageW(xLong1, &H465&, 0&, ByVal (MinVal * &H10000 + xLong2))
'      Call SendMessageW(xLong1, &H467&, 0&, ByVal xLong3)


'    If xNewValue(0) > 0 Then
'      ctVBox(1).ValueMax = xNewValue(0)
'      Else
'      ctVBox(1).ValueMax = xAutoRefreshTimeMax
'      End If

Private Sub AddDirNow(ByVal xChar1 As String)
  Dim xBool1 As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar2 As String

  xChar1 = LCase(xChar1)
  xChar2 = xChar1 + "\"
  For xLong1 = 0& To xDirCount - 1&
    If xChar1 = xDir(xLong1) Then
      xBool1 = True
      Exit For
      ElseIf xChar2 = Left(xDir(xLong1), Len(xChar2)) Then
      'mod & clean others
      xDir(xLong1) = xChar1
      
      Call SendMessage(HWndAutoList, &H182, xLong1, 0&)
      Call SendMessage_String(HWndAutoList, &H181, xLong1, xChar1)
      Call SendMessage(HWndAutoList, &H185, 1&, ByVal xLong1)
      xLong1 = xLong1 + 1&
      For xLong2 = xLong1 To xDirCount - 1&
        If xChar2 = Left(xDir(xLong2), Len(xChar2)) Then
          Call SendMessage(HWndAutoList, &H182, xLong1, 0&)
          Else
          For xLong3 = xLong2 To xDirCount - 1&
            xDir(xLong1) = xDir(xLong3)
            xLong1 = xLong1 + 1&
            Next
          Exit For
          End If
        Next
      xDirCount = xLong1
      xBool1 = True
      Exit For
      ElseIf Left(xChar1, Len(xDir(xLong1)) + 1&) = xDir(xLong1) + "\" Then
      xBool1 = True
      Exit For
      End If
    Next
  
  If Not xBool1 Then
    xLong2 = xDirCount
    For xLong1 = xDirCount - 1& To 0& Step -1&
      If xChar1 + vbNullChar < xDir(xLong1) + vbNullChar Then xLong2 = xLong1
      Next
    For xLong1 = xDirCount To xLong2 + 1& Step -1&
      xDir(xLong1) = xDir(xLong1 - 1&)
      Next
    xDir(xLong2) = xChar1
    
    Call SendMessage_String(HWndAutoList, &H181&, xLong2, xChar1)
    Call SendMessage(HWndAutoList, &H185, 1&, xLong2)
    
    xDirCount = xDirCount + 1&
    'If Not xDirCount < xAutoDirCountMax Then
    '  SetDragDrop False
    '  LoadResEx_Dialog_Enable HWndDialog, 2422&, False
    '  End If
    End If
  End Sub

Private Sub AddBrowsedDir()
  Dim xChar1 As String

  xChar1 = SHBrowseForFolder(HWndDialog, LoadResEx_String(2420&), xLastDir)
  If Not xChar1 = "" Then
    xLastDir = xChar1
    If Len(xChar1) = 3& Then xChar1 = Left(xChar1, 2&)
    If AllowedItem(xChar1, True) = 0& Then
      Call SendMessage(HWndAutoList, &H185&, 0&, ByVal -1&)
      AddDirNow xChar1
      SetSize
      End If
    End If
  End Sub

Private Sub ClearSome()
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Dim xBool1 As Boolean
  
  For xLong1 = 0& To xDirCount - 1&
    If SendMessage(HWndAutoList, &H187&, xLong2, 0&) = 0 Then
      xDir(xLong2) = xDir(xLong1)
      xLong2 = xLong2 + 1&
      Else
      Call SendMessage(HWndAutoList, &H182, xLong2, 0&)
      'xBool1 = True
      End If
    Next
  If Not xDirCount = xLong2 Then
    xDirCount = xLong2
    SetSize
    End If
  End Sub

Private Sub RaiseButton(ByVal ID As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long

  Select Case ID
    Case 2420&
      If xDirCount < xAutoDirCountMax Then
        AddBrowsedDir
        End If
      SetFocusAPI HWndAutoList
    Case 2421&
      ClearSome
      SetFocusAPI HWndAutoList
    Case 2422&
      xDirCount = 0&
      If Not HWndAutoList = 0& Then
        Call SendMessage(HWndAutoList, &H184&, 0&, 0&)
        'SetDragDrop  1&
        SetFocusAPI HWndAutoList
        SetSize
        End If
    Case 2450&, 2451&, 2460&
      If ID = 2450& Or ID = 2460& Then
        xLong1 = -CLng(Not IsDlgButtonChecked(HWndDialog, ID) = 0&)
        LoadResEx_Dialog_Enable HWndDialog, ID + 2&, xLong1
        LoadResEx_Dialog_Enable HWndDialog, ID + 1&, xLong1
        Else
        ID = Fix(ID / 10&) * 10&
        xLong1 = -CLng(Not IsDlgButtonChecked(HWndDialog, 2450&) = 0&)
        End If
      If Not ID = 2460& Then
        ID = GetDlgItem(HWndDialog, 2462&)
        If xLong1 = 1& Then
          xLong1 = GetDlgItem(HWndDialog, 2452&)
          If xLong1 = 0& Then
            xLong1 = xAutoRefreshTimer(0)
            Else
            xLong1 = SendMessage(xLong1, &H468&, 0&, 0&)
            End If
          If Not ID = 0& Then
            Call SendMessage(ID, &H465&, 0&, (xAutoRefreshTimeMin * &H10000 + xLong1))
            xLong2 = SendMessage(ID, &H468&, 0&, 0&)
            If xLong2 > xLong1 Then
              Call SendMessage(ID, &H467&, 0&, (xAutoRefreshTimeMin * &H10000 + xLong2))
              End If
            End If
          
          Else
          If Not ID = 0& Then
            Call SendMessage(ID, &H465&, 0&, (xAutoRefreshTimeMin * &H10000 + xAutoRefreshTimeMax))
            End If
          End If
        End If
    'Case 2460&
    '  Debug.Print "Check2"
    Case 2490&
      If Not xAutoDoing Then
        For xLong1 = 0& To 1&
          ID = GetDlgItem(HWndDialog, 2452& + xLong1 * 10&)
          If Not ID = 0& Then
            xAutoRefreshTimer(xLong1) = -SendMessage(ID, &H468&, 0&, 0&)
            End If
          ID = GetDlgItem(HWndDialog, 2450& + xLong1 * 10&)
          If Not ID = 0& Then
            If IsDlgButtonChecked(HWndDialog, 2450& + xLong1 * 10&) Then
              'If xLong1 = 0& Or xAutoRefreshTimer(0) > 0& Then
                xAutoRefreshTimer(xLong1) = -xAutoRefreshTimer(xLong1)
              '  End If
              End If
            End If
          Next
        
        xAutoDirCount = 0&
        For ID = 0& To xDirCount - 1&
          xAutoDir(ID) = xDir(ID)
          Next
        xAutoDirCount = xDirCount
        MainForm_EnableApiMenu 411&, (Not xAutoDirCount = 0&)
        MainForm_EnableApiMenu 412&, (Not xAutoDirCount = 0&)
        MainForm_EnableApiMenu 413&, (Not xAutoDirCount = 0&)
        MainForm_EnableApiMenu 414&, (Not xAutoDirCount = 0&)
        'ShowWindow HWndDialog, 0&
        'MainForm_xAutoRefreshNow False, True, False, False, True
        SetDragDrop False
        EndDialog HWndDialog, 2&
        End If
    Case 2491&
      SetDragDrop False
      EndDialog HWndDialog, 1&
    End Select
  End Sub


Private Function AutoRefreshDialogKBDProc(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&
          ClearSome
        Case 93&
          If (lParam And &H40000000) = 0& Then
            RaiseButton GetAPIPopupMenu(HWndDialog, xMenu, HWndAutoList)
            End If
        Case 9&
          LoadResEx_Dialog_ManualTab HWndDialog, hWnd
          Exit Function
        Case 27&
          SendMessage HWndDialog, &H471&, 5&, 0&
        End Select
    Case &H87&
      AutoRefreshDialogKBDProc = 4&
      Exit Function
    
    Case &H205&
      RaiseButton TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), HWndAutoList, ((lParam And &HFFFF0000) \ &H10000), True, xMenu, 1&)
    End Select
  If IsUnicode Then
    AutoRefreshDialogKBDProc = CallWindowProcW(AutoRefreshDialogKBDProcOld, hWnd, Message, wParam, lParam)
    Else
    AutoRefreshDialogKBDProc = CallWindowProcA(AutoRefreshDialogKBDProcOld, hWnd, Message, wParam, lParam)
    End If
  End Function

Private Function DropFiles(ByVal hDrop As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xBool1 As Boolean
  
  xLong1 = DragQueryFileCount(hDrop)
  For xLong1 = 0& To xLong1 - 1&
    If DragQueryFile(hDrop, xLong1, xChar1) Then
      If Len(xChar1) = 3& Then xChar1 = Left(xChar1, 2&)
      If AllowedItem(xChar1, True) = 0& Then
        If Not xBool1 Then
          Call SendMessage(HWndAutoList, &H185&, 0&, ByVal -1&)
          xBool1 = True
          End If
        AddDirNow xChar1
        If xDirCount = xAutoDirCountMax Then Exit For
        End If
      End If
    Next
  DragFinish hDrop
  If xBool1 Then SetSize
  End Function

