Attribute VB_Name = "modul_Calaym_Player"
' /base=d:\dev\software\projects\Calaym\configs\Calaym /multiinst /keeplogs /portable

' 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 private 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 private License for more details.

' You should have received a copy of the GNU General private 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

#Const WPL_AnsiTest = False
#If MainMenuCloseButton Then
  Private Declare Function AppendMenuA Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, lpNewItem As Any) As Long
  Private Declare Function AppendMenuW Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, lpNewItem As Long) As Long
  Private Type MEASUREITEMSTRUCT
    CtlType As Long
    CtlID As Long
    ItemID As Long
    itemWidth As Long
    itemHeight As Long
    itemData As Long
    End Type
  #End If



Private DefaultPLExtension  As String
Public xUseChmHelp As Boolean
#Const LoudestSongMethode = True
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As Long, nSize As Long) As Long
'Private Declare Function SetProcessDEPPolicy Lib "kernel32" (ByVal dwFlags As Long) As Long

'Private Const LB_ADDSTRING As Long = &H180&
Private Const LB_INSERTSTRING As Long = &H181&
Private Const LB_DELETESTRING As Long = &H182&
'Private Const LB_SELITEMRANGEEX as Long = &H183&
Private Const LB_RESETCONTENT As Long = &H184&
Private Const LB_SETSEL As Long = &H185&
'Private Const LB_SETCURSEL As Long = &H186&
Private Const LB_GETSEL As Long = &H187&
'Private Const LB_GETCURSEL As Long = &H188&
'Private Const LB_GETTEXT As Long = &H189&
'Private Const LB_GETTEXTLEN As Long = &H18A&
Private Const LB_GETCOUNT As Long = &H18B&
'Private Const LB_SELECTSTRING As Long = &H18C&
'Private Const LB_DIR As Long = &H18D&
Private Const LB_GETTOPINDEX As Long = &H18E&
'Private Const LB_FINDSTRING As Long = &H18F&
'Private Const LB_GETSELCOUNT As Long = &H190&
'Private Const LB_GETSELITEMS As Long = &H191&
'Private Const LB_SETTABSTOPS As Long = &H192&
'Private Const LB_GETHORIZONTALEXTENT As Long = &H193&
'Private Const LB_SETHORIZONTALEXTENT As Long = &H194&
'Private Const LB_SETCOLUMNWIDTH As Long = &H195&
'Private Const LB_ADDFILE As Long = &H196&
'Private Const LB_SETTOPINDEX As Long = &H197&
'Private Const LB_GETITEMRECT As Long = &H198&
Private Const LB_GETITEMDATA As Long = &H199&
Private Const LB_SETITEMDATA As Long = &H19A&
'Private Const LB_SELITEMRANGE As Long = &H19B&
'Private Const LB_SETANCHORINDEX As Long = &H19C&
'Private Const LB_GETANCHORINDEX As Long = &H19D&
Private Const LB_SETCARETINDEX As Long = &H19E&
Private Const LB_GETCARETINDEX As Long = &H19F&
'Private Const LB_SETITEMHEIGHT As Long = &H1A0&
Private Const LB_GETITEMHEIGHT As Long = &H1A1&
'Private Const LB_FINDSTRINGEXACT As Long = &H1A2&
'Private Const LB_SETLOCALE As Long = &H1A5&
'Private Const LB_GETLOCALE As Long = &H1A6&
'Private Const LB_SETCOUNT As Long = &H1A7&
'Private Const LB_MSGMAX As Long = &H1A8&
#If 0 Then
Private Const LB_ITEMFROMPOINT As Long = &H1A9&
#End If

Public Const CB_RESETCONTENT As Long = &H14B&
Private xAutoUSBQuit As Boolean
Private Const MouseWheelDeltaDiv As Long = 120& / 16&
Private MouseWheelDeltaVol As Long

#Const NoDroppingIfOverloaded = False ' you can still load playlists, timers etc!

'xMWindowIDsX
'ttips
'animiconpos

'Private Const GWL_USERDATA As Long = (-21&)

Private CanIRadio As Boolean
Public QuitMode As Long
Public QuitModePending As Long
Public WantsShutdownDialog As Boolean
'Public vShutDownDTimer As Single
Public hwndShutDownDialog As Long

Private FormatToolTip1 As String
Private FormatToolTip2 As String
Private CurSongToolTip1 As String
Private CurSongToolTip2 As String

#Const SelfPathElipse = True 'also frmsetup

#Const XPDeviceRemoveDetect = True
#Const XPDeviceRemoveDetectPST = False

#If XPDeviceRemoveDetect And XPDeviceRemoveDetectPST Then
  Private PendingDeviceRegB As Boolean
  Private PendingDeviceRegT As Long
  #End If


Public SSRestrictions As String
Private Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Boolean
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hWnd As Long) As Long

Public xLoadingStream As Long
Private xLoadURL As String
Private Const Interface_Mode_None As Long = 0&
Public Const Interface_Mode_Search As Long = 1&
Public Const Interface_Mode_Timer As Long = 2&
Private Const Interface_Mode_Radio As Long = 3&
Public InterfaceMode As Long

Private SearchFormDraggingIs As Boolean
Private SearchFormDraggingWParam As Long
Private SearchFormDraggingLParam As Long
Private SearchFormDraggingWMsgPlus As Long

'Private Const xSearchResultDigitsOver As String = "  + "
'Private Const xSearchResultDigitsNull As String = "    "

'Private HWndCapLabel As Long
'Private HWndTxt As Long
'Private HWndDrop As Long
'Private HWndSearchResultList As Long
Private TimerListForm_xMenu As Long
Private TimerListForm_xMenu2 As Long
Private TimerListForm_xPopupMenu2 As Long
Private TimerListUpdateHWnd As Long

Private TimerListForm_xLocked As Boolean
Private xTimerDisplayExtraLen As Long
Private xTimerDisplayExtraLenT As Long
Private Const xTimerDisplaySpace1 As Long = 16&
Private Const xTimerDisplaySpace2 As Long = 16&
Public xTimerEditingHWnd As Long

Private Const Interface_Control_Search_SpecialCheck As Long = 2&
Private Const Interface_Control_Search_Check1 As Long = 0&
Private Const Interface_Control_Search_Check2 As Long = 1&
Private Const Interface_Control_Search_Play As Long = 2&
Private Const Interface_Control_Search_Select As Long = 3&
Private Const Interface_Control_Search_Return As Long = 4&
Private Const Interface_Control_Search_Txt As Long = 5&
Private Const Interface_Control_Search_CapLabel As Long = 6&
Private Const Interface_Control_Search_LastFontApply As Long = 6&
Private Const Interface_Control_Search_Results As Long = 7&
Private Const Interface_Control_Search_Image As Long = 8&

'Private Const Interface_Control_Search_Last = Interface_Control_Search_Image '8
Private Const Interface_Control_Search_LastHook = Interface_Control_Search_Txt '5
Private Const Interface_Control_Search_LastAccel = Interface_Control_Search_Txt '5

Private Const Interface_Control_Timer_Add As Long = 0&
Private Const Interface_Control_Timer_Edit As Long = 1&
Private Const Interface_Control_Timer_Remove As Long = 2&
Private Const Interface_Control_Timer_RemoveAll As Long = 3&
Private Const Interface_Control_Timer_Close As Long = 4&
Public Const Interface_Control_Timer_List As Long = 5&
Private Const Interface_Control_Timer_CapLabel = 6&
Private Const Interface_Control_Timer_Image As Long = 7&

'Private Const Interface_Control_Timer_Last = Interface_Control_Timer_Image '7
Private Const Interface_Control_Timer_LastHook = Interface_Control_Timer_Close '4
'Private Const Interface_Control_Timer_LastAccel = Interface_Control_Timer_List '5
Private Const Interface_Control_Timer_LastFont = Interface_Control_Timer_CapLabel

Private Const Interface_Control_Radio_Go As Long = 0&
Private Const Interface_Control_Radio_Play As Long = 1&
Private Const Interface_Control_Radio_OpenHP As Long = 2&
Private Const Interface_Control_Radio_AutoCloseCheck As Long = 3&
Private Const Interface_Control_Radio_Add As Long = 4&
Private Const Interface_Control_Radio_Edit As Long = 5&
Private Const Interface_Control_Radio_Remove As Long = 6&
Private Const Interface_Control_Radio_Return As Long = 7&
Private Const Interface_Control_Radio_Txt As Long = 8&
Private Const Interface_Control_Radio_CapLabel As Long = 9&
Private Const Interface_Control_Radio_LastFontApply As Long = 9&
Private Const Interface_Control_Radio_List As Long = 10&
Private Const Interface_Control_Radio_Image As Long = 11&

Private Const Interface_Control_Radio_Last = Interface_Control_Radio_Image '11
Private Const Interface_Control_Radio_LastHook = Interface_Control_Radio_Txt '8
Private Const Interface_Control_Radio_LastAccel = Interface_Control_Radio_Txt '8




Private Const Interface_Control_Last = Interface_Control_Radio_Last
'Private Const Interface_Control_HookLast = Interface_Control_Radio_LastHook
Private Const Interface_Control_AccelLast = Interface_Control_Radio_LastAccel

Public Interface_Control(Interface_Control_Last) As Long
Private Interface_Control_Accel(Interface_Control_AccelLast) As Long







Public Type TEXTMETRIC
  tmHeight As Long
  tmAscent As Long
  tmDescent As Long
  tmInternalLeading As Long
  tmExternalLeading As Long
  tmAveCharWidth As Long
  tmMaxCharWidth As Long
  tmWeight As Long
  tmOverhang As Long
  tmDigitizedAspectX As Long
  tmDigitizedAspectY As Long
  tmFirstLastDefaultBreakChars As Long
  tmItalicA As Byte
  tmUnderlinedA As Byte
  tmStruckOutA As Byte
  tmPitchAndFamilyA As Byte
  tmItalicW_CharsetA As Byte
  'tmUnderlinedW As Byte
  'tmStruckOutW As Byte
  'tmPitchAndFamilyW As Byte
  'tmCharSetW As Byte
  End Type
Public Declare Function GetTextMetricsA Lib "gdi32" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
'Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal nwCount As Long, lpBits As Any) As Long
'Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
'Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFOHEADER, ByVal wUsage As Long) As Long
Private Declare Function DrawTextA Lib "user32" (ByVal hdc As Long, ByVal lpStr As Long, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

Public lkCtrl As String
Public lkAlt As String
Public lkShift As String
Public lkCombi(4) As String

Public UrlsAllowed As Boolean
Public DragOut As Boolean

Public LayerHP As String
Private Type DEV_BROADCAST_VOLUME
  dbcv_size As Long
  dbcv_devicetype As Long
  dbcv_reserved As Long
  dbcv_unitmask As Long
'  dbcv_flags As Long
  End Type
Private IsDevicePlugging As Boolean
Private HasDevicePlugged As Boolean
Private HasDevicePlugStoppedSong As Boolean
'Public DeviceAddAutoPlay As Boolean
Private xPStateIconPlugging As Boolean

Private Const FirstNonFloppy As Long = 2&

Public Const DefTxt As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

Private Const TTSpaceLeft As Long = 5&
'Private Const TTSpaceLeftD As Long = TTSpaceLeft * 2&
Private Const TTSpaceTop As Long = 3&
Private Const TTSpaceTopD As Long = TTSpaceTop * 2&
Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetTextColor Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long

#Const UseIRPlugins = False
#Const ShowRegKeyStuff = 0
#Const FlatCastCF17 = 0
#Const PersonalStuffABC = 0 ' Should be false! Some custom routines for personal usage, ignore them :-)

#Const BoldDirs = 98

Public PLODHeight As Long
Private PLODIconTopShift As Long
Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Boolean
Public Type DRAWITEMSTRUCT
  CtlType As Long
  CtlID As Long
  ItemID As Long
  itemAction As Long
  itemState As Long
  hwndItem As Long
  hdc As Long
  rcItem As RECT
  itemData As Long
  End Type

#If ListFocusedLists Then
  Public xCurFocusHWnd As Long
  #End If
Private Declare Function FindTextA Lib "comdlg32.dll" (pFindreplace As FINDREPLACE) As Long
Private Declare Function FindTextW Lib "comdlg32.dll" (pFindreplace As FINDREPLACE) As Long

' 0/1/2 = play stop pauses

'hdcs / epic:
' 2 = pl anysize
' 3 = pl/def small; req timedisp

' 5 req anim -> tags!!!
' 6 req defaultfont bitmapgen

' fadinganim
' deleting
'refreshing
'quit
'startup


'HaveToDo :
'NoSmooth
'PopupMenu mM!!
'Const WM_DEVICECHANGE As Long = &H219
'Const WM_DEVMODECHANGE As Long = &H1B
'Const WM_DISPLAYCHANGE As Long = &H7E

'6 - 1
'5+1
' max 6xxxx
' -> 7000-9000 -> pre-strings...
'  900 = tag reading etc
'  900-19 = reset... / unknown stream/tracker/midi/cda
'        0=reset, 4=strea, 8=tracker, 12=midi, 16=cda
'   1x-7x = streams <-> tracker
' 1000 maincaps (playing...) / icons
' 1100 = tooltips / caps...
'   0x -> dirlist, prevlist
'   1x -> buttons itself (prev - next) 7x -> each 4pcs? -> 0 - 28+3, -> 10-49
'   5x -> vol
'   6x -> repeatmode
'   7x -> position, spectrum
'   8x -> timer
'   9x -> addinfo/tags tooltip
' 1200 File dialogs
'   00 = addfiles
'   10 = adddir
'   20 = load/savepl
' 1300 PlayControl dialogs/msg
' 1400 playList dialogs/msg
'   00 = finddialog
' 1500 options dialogs/msg
' 1600 tools dialogs/msg
'   00 = timer lsdialog (5x)
'   10 = timerAsks
'   50 = presetlsdialog
' 1700 help dialogs/msg
'   1780 about / dragmenu

' 1800 - 1900 startup error msgs (/traymenus)
'2 0/1 xx -> Startup
'2 2   xx -> Info
'2 3   xx -> search
'2 4   xx -> arsetup
'2 5   xx FREEEEEEEEEEE
'2 6   xx -> timer
'2 7   xx -> timer edit
'2 8   xx -> ShutDown
'2 9   xx -> SPDisplayIcon
'3  x  xx -> setup

'4  x  xx -> Timer / Edit (0/1 timer 2 edit win 3/4 strings
'4 6/7 xx -> SearchWindow
'4 8   xx -> SPDisplayIcon

' 4000 = internet radio?

#Const ProcCtl = False
#If ProcCtl Then
  Private Const w_Progress As String = "msctls_progress32"
  #End If

#Const PlayLoosingLog = False
Private Type Msg
  hWnd As Long
  Message As Long
  wParam As Long
  lParam As Long
  time As Long
  pt As POINTAPI
  End Type

Private IsMessageing As Boolean
Private Declare Function GetDoubleClickTime Lib "user32" () As Long

Private Declare Function GetMessageA Lib "user32" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Private Declare Function GetMessageW Lib "user32" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Private Declare Function IsDialogMessageA Lib "user32" (ByVal hDlg As Long, lpMsg As Msg) As Long
Private Declare Function IsDialogMessageW Lib "user32" (ByVal hDlg As Long, lpMsg As Msg) As Long
Private Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
Private Declare Function DispatchMessageA Lib "user32" (lpMsg As Msg) As Long
Private Declare Function DispatchMessageW Lib "user32" (lpMsg As Msg) As Long
Private Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)

Private EnqueueFlag As Boolean
Public OutOfSessionPause As Boolean
Private OutOfSessionLeft As Boolean
Private OutOfSessionHasPaused As Boolean
Private OutOfSession_MySessionID As Long
Public NotOutOfSessionOld As Boolean
Private Declare Function ProcessIdToSessionId Lib "kernel32" (ByVal dwProcessId As Long, pSessionID As Any) As Long
Private Declare Function WTSGetActiveConsoleSessionId Lib "kernel32.dll" () As Long



'Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
'Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long

Private NeedRestoreReg As Boolean
Private LastStreamTT As String

Private GMCD_URL As String
Private GMCD_Mode As Long
Private GMCD_Pending As Boolean

Private SenderURL As String
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

'Private LastSuccessStream As String -> PLCurFullURL
Public LockWinUpd As Long
Private Declare Function LockWindowUpdate Lib "User32.dll" (ByVal hwndLock As Long) As Long

Public xOnEmptyPLStartup As Long
Private curTagText As String

#Const TrayXFadeIcon = True
'Private Const TrackerSkipperMinValM As Long = -TrackerSkipperMinVal
Private Const m3uIString As String = "#Calaym100m3u"
Private Const ResFileIDString As String = "calaym101lng"
Private Const ThemeFileIDString As String = "calaym100athm"
Private Const xTimerIDString = "[CalaymTimerSettings100]"

Public RegFolderMode(3) As Boolean

#Const NewRemove = False
#Const NewKeyMode = True 'also mod & teardown
#Const CanEnumComputerShares = False 'also frmautorefresh!

#Const FailSaveTest = False ' skips save settings on quitting so failsave occurs on next start
'#Const UseFileLogging = 1 'Set 1 to enable log file handling; set in compiler options!
    ' affects modCalaym.bas, CallBackAddLog
    #Const UseFileLoggingIDE = True
    #Const UseFileLoggingKernel = True
    #Const DetailLog = False
    ' Always english text !!!
#Const PresetBinded = True
' Enable / Disable at your own risk; Some features may not work
#Const AutoHideDropDown = True ' Check current active window to automatically hide dropdown-lists


#If UseFileLogging Then
  Private PLClearCounter As Long
  #End If


' !!!!!!!!!!!! FMOD depending currently only !!!!!!!!!!!!1
#Const TryPreventReInit = False ' Designed to automatically reset output device if playback fails with device busy
    ' (e.g. starting some games cause playback to 'hang', and when skipping nothing can be played)
    ' not quite finished
#Const TrayIconViaTimerCheck = 2

'Private CalaymDragSource As Long
'Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetLastActivePopup Lib "user32" (ByVal hwndOwnder As Long) As Long

Public Const xAutoDirCountMax As Long = 256&
Public Const xAutoDirCountMaxX As Long = xAutoDirCountMax - 1&
Public xAutoDir(xAutoDirCountMaxX) As String
Public xAutoDirCount As Long
Public xAutoRefreshTimer(1) As Long

Private xAutoDirHWnd(xAutoDirCountMaxX) As Long
Private xAutoDirHWndCount As Long

Private Declare Function RegisterWindowMessageA Lib "user32" (ByVal lpString As String) As Long
Private Declare Sub RtlZeroMemory Lib "kernel32" (ptrMem As Any, ByVal Length As Long)
Private RegisteredFindMsgID As Long
Private CurrentFindHWnd As Long
Private CurrentFindHMem As Long
Private CurrentFindInvert As Long
Private CurrentFindMatchCase As Long

Private CurSongPatterns As Long

Private Declare Function DrawFrameControl Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) As Boolean
Public ScrollComboWidth As Long

'Private Type OSVERSIONINFO
'  dwOSVersionInfoSize As Long
'  dwMajorVersion As Long
'  dwMinorVersion As Long
'  dwBuildNumber As Long
'  dwPlatformId As Long
'  szCSDVersion As String * 128
'  End Type

Private FormatIcon(4) As String

Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long


Private Const ShowIRTicksFull As Long = 5000&

#If UseFileLogging Then
  #If UseFileLoggingKernel Then
    Private Declare Sub OutputDebugStringA Lib "kernel32" (ByVal lpOutputString As String)
    Private Declare Sub OutputDebugStringW Lib "kernel32" (ByVal lpOutputString As Long)
    #End If
  #End If

            
#If PresetBinded Then
  Private xNoPresets As Boolean
  'Private vPLXPs(vPLXMaxX) As String
  'Private vPLXPsCount As Long
  'Private PresetFile As String
  #End If

Private xNoTimers As Boolean
Private Declare Function GetDialogBaseUnits Lib "user32" () As Long

Private NewSongID As Long
Private OldSongID As Long



#If CanEnumComputerShares Then
  Private Type sNETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As Long
    lpRemoteName As Long
    lpComment As Long
    lpProvider As Long
    End Type
  Private Const sNETRESOURCE_Len As Long = 32&
  
  Private Declare Function WNetOpenEnumA Lib "mpr.dll" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
  Private Declare Function WNetOpenEnumW Lib "mpr.dll" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
  Private Declare Function WNetEnumResourceA Lib "mpr.dll" (ByVal hEnum As Long, lpcCount As Long, ByVal lpBuffer As Long, ByRef lpBufferSize As Long) As Long
  Private Declare Function WNetEnumResourceW Lib "mpr.dll" (ByVal hEnum As Long, lpcCount As Long, ByVal lpBuffer As Long, ByRef lpBufferSize As Long) As Long
  Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
  #End If

Private xLastPathTimeSpace As Long
Private xLastPathCurMaxWidth As Long


Private LogFile As String
Private LogFileLog As String
Private LogFileMulti As Boolean
#If UseFileLogging Then
  Private LogFileDoLog As Boolean
  #End If
Private LogFileDo As Boolean

'Private xTimerLoaded As Long
  ' &h100 = led               0
  '(&h010 = startup / eTimer  2)
  ' ^h001 = GUI def / eTimer  1)


Public xSearchCount As Long
Private Const xSearchCountMax As Long = 2048&
Private Const xSearchCountMaxX As Long = xSearchCountMax - 1&
Public Const xSearchCountOver As Long = xSearchCountMax + 1&
'Public Const xSearchResultDigitsCount As Long = 4&

Private xSearch(xSearchCountMaxX) As Long
Private xSearchSortAlias(xSearchCountMaxX) As Long
Private xSearchSortCap(xSearchCountMaxX) As String
Private xSearchSortDir(xSearchCountMaxX) As Long
Public xSearchError As String


Public xRadioCount As Long
Private Const xRadioCountMax As Long = 2048&
Private Const xRadioCountMaxX As Long = xRadioCountMax - 1&
Private xRadioCap(xRadioCountMaxX) As String
Public xRadioURL(xRadioCountMaxX) As String
Private xRadioHP(xRadioCountMaxX) As String
Private xRadioLastEnteredURL As String
Private xRadioEditingHWnd As Long
Private xRadioEditingEdit As Long

Private xRadioMenu As Long
Private xRadioMenu2 As Long
Private xRadioPopupMenu2 As Long

Private Type COPYDATASTRUCT
  dwdata As Long
  cbData As Long
  lpData As Long
  End Type


Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const STARTF_USESIZE As Long = &H2
Private Const STARTF_USEPOSITION As Long = &H4





Private TrackerLoop As Boolean
Private xVolTTText As String
Private xModeTTText As String
Private xNoPLMenu As Boolean
Private xNoQuitMenu As Boolean
Private xNoSetup As Boolean
Private xNoAutoSetup As Boolean
Private xNoAutoRefreshByUser As Boolean
Private xNoAutoRefresh As Boolean
Private xNoSpectrumForce As Boolean
Private xModCurPattern As Long
'Private xModCurPatternDone As Long
Private xModPatterns As String
'Private eSPPList As Long
'Private xLastDir As String
Private Const InfoProcIntervalKb As Long = 100&
Private LastPosTime As Long
Private Const LastPosSkipTime As Long = 1000&
Private Const LastPosSkipTimeNetStream = 8000&
Private xProcID As Long





'Private CrossFadeModeXX = 0
' 0 = ~  1.5 secs superfade
' 1 = ~  6.5 secs
' 2 = ~ 11.5 secs
' 3 = ~ 16.5 secs
Private xCrossFadeTimeMC As Long

Private Const CrossFadeWaitTime As Long = 1000& ' Wait time if seeking fails; should be <= WaitSeek
Private Const CrossFadeWaitSeek As Long = 1000& ' Seek time for new song
Private Const CrossFadeWaitSkipPattern As Long = 1& ' seek pattern (mod)

'private xWaitKey As Long
'private xCrossFadeTime As Long
Public xCrossFadeTimeM As Long

Public Const xFadeConsts1 As Long = 2&
Public Const xFadeConsts2 As Long = 20&
'private Const xFadeConsts2x As Long = xFadeConsts2 - xFadeConsts1
Private Const xFadeConstsX As Long = xFadeConsts2 - xFadeConsts1 '- 1&

'Private Const CrossFadeWaitEndTime As Long = 600&  '500&
Private CrossFadeTimeSingle(xFadeConstsX) As Long ' Pure fade time each song
Private CrossFadeTimeDelay(xFadeConstsX) As Long ' Delay (or time not really fading each song)

    Private CrossFadeMinTime(xFadeConstsX) As Long ' requ length cursong or newsong
      Private CrossFadeNewMinMinTime(xFadeConstsX) As Long                'requ length  newsong on userends
    Private CrossFadeMaxPos(xFadeConstsX) As Long           ' pos1 (TimerEvent checking)
    Private CrossFadeMinPos(xFadeConstsX) As Long           ' pos2 (TimerEvent checking)
    Private CrossFadeMinRemainTime(xFadeConstsX) As Long                  'requ remaining time
    
    Private CrossFadeMinPatterns(xFadeConstsX) As Long 'requ pattern cursong or newsong
      Private CrossFadeNewMinMinPatterns(xFadeConstsX) As Long        'requ pattern newsong on userends
    Private CrossFadeMaxPattern(xFadeConstsX) As Long 'pos1 (TimerEvent checking)
    Private CrossFadeMinPattern(xFadeConstsX) As Long 'pos2 (TimerEvent checking)
    Private CrossFadeMinRemainPatterns(xFadeConstsX) As Long 'requ remaining pattern
    

Private Const CannotPlayAbortTime As Long = 3000&
'Private CurMusic As Long
Private xTrayIconOld As Long
Private CDDriveLetters As String

'32736


Private xWarnOverload As Boolean
Private xOldWarnOverload As Boolean

Private Const xLastPathMax As Long = 1024&
Private Const xLastPathMaxX As Long = xLastPathMax - 1&
Private xLastPath(xLastPathMaxX) As String
Private xLastPathCap(xLastPathMaxX) As String
Private xLastPathTime(xLastPathMaxX) As String
Private xLastPathLen(xLastPathMaxX) As Long
Private xLastPathCount As Long

#Const Test256 = False
#If Test256 Then
  'Private Const PLCountMax As Long = 256& ' 32736& '32736&
  'Private Const PLCountWarnNT As Long = 250&
  'Private Const PLCountWarn95 As Long = 250&
  #Else
  'Windows 95/Windows 98/Windows Millennium Edition (Windows Me) : The wParam parameter is limited to 16-bit values. This means list boxes cannot contain more than 32,767 items. Although the number of items is restricted, the total size in bytes of the items in a list box is limited only by available memory.
  Private Const PLCountMax95 As Long = 32700& '32736&
  ''Private Const PLCountMaxNT As Long = &H7F0000 ' 8323072
  'Private Const PLCountMaxNT As Long = 8000000 ' 8323072
  'Private Const PLCountWarn95 As Long = 30000&
  'Private Const PLCountWarnNT As Long = 7500000
  #End If
Private PLCountMax As Long
Private PLCountMaxX As Long
Private PLCountMaxX2 As Long
Private PLCountWarn As Long

Private Const RandomizerPattern As Long = 16&
Private RndPLPatternCount As Long
Private RndPLPattern() As Byte
Private RndPatternAlias(RandomizerPattern - 1&) As Byte


Private xOLEisDragging As Boolean
'Private xPStateIconPlaying As Boolean
'Private xPStateIconPaused As Boolean
Private xPStateIconRefreshing As Boolean
Public xPStateIconDeleting As Boolean
Private xLastSkipDirection As Long
Private xXFadeCurMusic As Long
Public xSongIsMidi As Long
'Private xSongWasMidi As Long
Private xSongWillBeMidi As Long
'Private xXFadeCDDriveLetterByte As Byte
Private xXFadeOldTime As Long

'Private xCDDriveLetterByte As Byte
Public xOldIconState As Long


Private xTimerTimeTimerTime As Long
Private xEndSongTimerEventTime As Long
Private xAutoRefreshTimerEventTime(1) As Long

#If TrayIconViaTimerCheck Then
  Private xTrayIconCrashUpdateTimer As Long
  #If TrayIconViaTimerCheck = 1 Then
    Private Const xTrayIconCrashUpdateTimerDelay As Long = 300000 '300 secs = 5 mins
    #Else
    Private Const xTrayIconCrashUpdateTimerDelay As Long = 30000 '300 secs = 5 mins
    #End If
  #End If
Private xAutoPlaybackLastRetryTime As Long
'Private xOldDropRepeat As Long

'Private Const MAX_PATH = 260&
'Private Const INVALID_HANDLE_VALUE = -1&

Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal ID As Long) As Long
Private Declare Function RegisterServiceProcess Lib "kernel32" (ByVal dwProcessId As Long, ByVal dwType As Long) As Long


Private Const ConstByte_0 As Byte = 0
Private Const ConstByte_1 As Byte = 1
Private Const ConstByte_2 As Byte = 2
Private Const ConstByte_3 As Byte = 3
Private Const ConstByte_4 As Byte = 4
Private Const ConstByte_5 As Byte = 5
Private Const ConstByte_6 As Byte = 6
Private Const ConstByte_7 As Byte = 7
Private Const ConstByte_8 As Byte = 8
Private Const ConstByte_9 As Byte = 9
Private Const ConstByte_A As Byte = 10
Private Const ConstByte_B As Byte = 11
Private Const ConstByte_C As Byte = 12
Private Const ConstByte_D As Byte = 13
Private Const ConstByte_E As Byte = 14
Private Const ConstByte_F As Byte = 15



Private xIsVoluming As Boolean
Private PLCurSongLastPos As Long
Private PLCurSongPos As Long
Private PLCurSongLength As Long

Private Declare Function AdjustWindowRectEx Lib "user32" (lpRect As RECT, ByVal dsStyle As Long, ByVal bMenu As Long, ByVal dwEsStyle As Long) As Long

Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
'Private Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long

Private xFollowCurSongLast As Long
Private xFollowCurSongLastDiff As Long 'Time that has to be elapsed since new song started due FollowCurrentSong to delete current song (to ensure the new song is to be deleted and not the old one that finished a few ms before)
  Private Const xFollowCurSongLastDiffDef As Long = 1500& + 225& 'FadingTimerEx As Long = 225&

Private xCurPlistFilename As String
Private xCurPlistFilenameSuggestNew As Boolean
Private PLCurSongDisplay As String
Private PLCurSongDisplaySpace As Long
Private PLCurSongDisplaySpacePerc As Long
Private PLCurSongDisplaySpacePos As Long
Private PLCurSongDisplaySpacePosOrg As Long
Private PLCurSongDisplaySpaceStr As String
Private PLCurFullURL As String
Private PLCurSong As String
Private xFindNextString As String
Private NSCursor As Long
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long

'Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Private lPlayList_Height As Long

Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long

Private PLNext As Long
Private PlNextSong As String
Private PlNextSongCap As String


Private Declare Function SetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As Any, ByVal wUsage As Long) As Long

Public xNoRemove As Boolean

Private DirLblCaption As String
Private ReAutoPlayDR As Boolean
Private ReAutoPlayDRTime As Long

'private xCurSmallFont As Long

Private Type BITMAPINFOHEADER
  biSize As Long
  biWidth As Long
  biHeight As Long
  biPlanes As Integer
  biBitCount As Integer
  biCompression As Long
  biSizeImage As Long
  biXPelsPerMeter As Long
  biYPelsPerMeter As Long
  biClrUsed As Long
  biClrImportant As Long
  End Type

Private xGUIClickDelayTimer As Long

Public MainFormIsSplitterMoving As Boolean
Private MainFormDraggingIs As Boolean
Private MainFormDraggingWParam As Long
Private MainFormDraggingLParam As Long
Private MainFormDraggingWMsgPlus As Long
'Private Declare Function LBItemFromPt Lib "comctl32" (ByVal hLB As Long, ByVal x As Long, ByVal y As Long, ByVal bAutoScroll As Boolean) As Long

Private Const xDirListMin As Long = 1&
Private Const xDirListMax As Long = 8&
Private Const xDirListMaxX As Long = -8&
Private Const xDirListMaxDiv As Long = 10&

Public xDirList As Long

Private ePic1ScaleWidth As Long
Private ePic1TopX As Long
Private ePic1TopX2 As Long
Private Const ePicXWidthX2 As Long = 2& * 32&
'Private Const eImage0Width As Long = 16&
Private Const eImage1Right As Long = 244&
'private eButton0Width As Long
Private ePic0Height As Long
Private Const DirAndCurSongBarHeight As Long = 20&
  Private Const DirAndCurSongBarRealOverlap As Long = 1&
  Private Const DirAndCurSongBarRealHeightOverlap As Long = DirAndCurSongBarHeight + DirAndCurSongBarRealOverlap
  Private Const DirAndCurSongBarHeightOverlap As Long = 2&
  Private Const CurSongPosRealTop As Long = DirAndCurSongBarHeight - DirAndCurSongBarHeightOverlap
  Private Const CurSongPosRealHeight As Long = 11&
  Private Const CurSongAndPosHeight As Long = CurSongPosRealTop + CurSongPosRealHeight

Private Const eSPTop As Long = 4&
Private Const eSPTop2 As Long = eSPTop * 2&
Private Const eSPTop4 As Long = eSPTop * 4&
Private Const eSPTop5 As Long = eSPTop * 5&
Private Const hwndSpectrumLeftDef As Long = 244&
Private Const hwndSpectrumTopDef As Long = CurSongAndPosHeight
Private Const hwndSpectrumWidth As Long = 130&
Private Const hwndSpectrumHeight As Long = 23&
Private Const PlayControlsTop = hwndSpectrumTopDef + 3&
Private Const VolumePosInitLeft As Long = 152&
Private hwndSpectrumTop As Long
Private hwndSpectrumLeft As Long
Private Const hWndTagsTopDef As Long = hwndSpectrumTopDef + hwndSpectrumHeight
Private Const hWndTagsHeight As Long = 32&
'Private Const hWndTagsHeightX As Long = hWndTagsHeight + 1&
Private hWndTagsTop As Long
Private hWndTagsWidth As Long
Private Const PanelSizeDef = hWndTagsTopDef
Private Const PanelSizeDefTags = hWndTagsTopDef + hWndTagsHeight
Private Const PanelSizeDefKiosk = CurSongAndPosHeight + eSPTop / 2& '- 1&

Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As Long) As Long

Private HWndSplitter As Long
Private HWndSplitterMove As Long
Private SplitterPreResult As Long

Public ShowIRWin As Boolean
'private ShowIR_Font As Long
Private xNoSaveListMenu As Long

Private Const xMenuCountX As Long = 8&
Private xMenu(xMenuCountX) As Long  ' 2 CD Popups
Private xPopupMenu(xMenuCountX) As Long

'private PLFontOrig As Long
'private PLFontOrigCtl1 As Long
'private PLFontOrigCtl2 As Long

Private SysTrayIcon As NOTIFYICONDATA

Private CommandX As String

Public xTrayIconIndicator As Boolean


Private Const ReadDirectory_xArraySMax As Long = 32767&
Private Const ReadDirectory_xArraySMaxX As Long = ReadDirectory_xArraySMax - 1&
Private ReadDirectory_xArrayS(ReadDirectory_xArraySMaxX) As String



Public sCharA1Max As Long
Private sCharA1MaxX As Long
Public sCharA1() As String

Private xDirPath() As Long ' At least 1 song
Private xDirCount As Long

Public xPStateIconSndcard As Boolean

Private xPStateIconQuit As Boolean


Private Const PLIconFolderFake As Long = 100&
Private Const PLIconFileFake As Long = 200&

Private PLIconFolder As Long
Private PLIconFile As Long
Private PLIconURL As Long
Private Interface_Icon As Long
Private Const PLIconFolder_ResID As Long = 1900& '1350&
Private Const PLIconFile_ResID As Long = 1950& '1065&
Private Const PLIconURL_ResID As Long = 2800& '1065&

Private Const PLIconSearch_ResID As Long = 2300& '1065&
Private Const PLIconTimer_ResID As Long = 2600& '1065&
Private Const PLIconRadio_ResID1 As Long = 2820& '1065&
Private Const PLIconRadio_ResID2 As Long = 2840& '1065&

#If BoldDirs = 98 Then
  Private Type tPlaylistItem
    Filename As String
    Caption As String
    DirDepthFlags As Integer
    pxWidthLg As Integer
    pxWidthSm As Integer
    End Type
  Public PLI() As tPlaylistItem
  #Else
  Private PLIFile() As String
  Private PLICap() As String
  Private PLIDF() As Integer
  Private PLIPxLg() As Integer
  Private PLIPxSm() As Integer
  #End If
Private Const PLISize As Long = 14&

Private Const PLI_Depth As Long = &H1FFF&
Public Const PLI_IsDir As Long = &H2000
Public Const PLI_IsAuto As Long = &H4000

Private Type MEMORYSTATUS
  dwLength As Long
  dwMemoryLoad As Long
  dwTotalPhys As Long
  dwAvailPhys As Long
  dwTotalPageFile As Long
  dwAvailPageFile As Long
  dwTotalVirtual As Long
  dwAvailVirtual As Long
  End Type
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)

Private Type MEMORYSTATUSEX
  ullLength As Long
  ullMemoryLoad As Long
  ullTotalPhysLow As Long
  ullTotalPhysHigh As Long
  ullAvailPhysLow As Long
  ullAvailPhysHigh As Long
  ullTotalPageFileLow As Long
  ullTotalPageFileHigh As Long
  ullAvailPageFileLow As Long
  ullAvailPageFileHigh As Long
  ullTotalVirtualLow As Long
  ullTotalVirtualHigh As Long
  ullAvailVirtualLow As Long
  ullAvailVirtualHigh As Long
  ullAvailExtendedVirtualLow As Long
  ullAvailExtendedVirtualHigh As Long
  End Type
Private Declare Function GlobalMemoryStatusEx Lib "kernel32" (lpBuffer As MEMORYSTATUSEX) As Long

Public PLCount As Long
Private PLCurItem As Long

'Private hwndSpektrumHook As Long
Private xFindNtfy As Long

Private xTimerIsEnabled As Boolean

Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal hWnd As Long) As Long

Private Const CalaymClassNameMsg As String = "CalaymInterMsgClass100"
Private Const CalaymClassTitleMsg As String = "Calaym"
Private Const CalaymClassForm As String = "CalaymPlayerWindow"
Public Const CalaymClassStaticWhiteDC As String = "CalaymStaticWhiteAndCombo"
Private Const CalaymClassSplitter As String = "CalaymDirListSplitter"
Private Const CalaymClassSplitterMove As String = "CalaymDirListSplitterMoving"
'Private Const CalaymClassStaticVolume As String = "CalaymVolume"
Private Const CalaymClassToolTip As String = "CalaymToolTip"






'Private xClassIcon As Long
'Private xClassIconSmall As Long

'0-5=defs
Private Const xStaticIcons_Def0 As Long = 0&
Private Const xStaticIcons_Def1 As Long = 1&
Private Const xStaticIcons_Def2 As Long = 2&
Private Const xStaticIcons_Def3 As Long = 3&
Private Const xStaticIcons_Def4 As Long = 4&
Private Const xStaticIcons_Def5 As Long = 5&
Private Const xStaticIcons_Volume As Long = 6&
Private Const xStaticIcons_RepMode As Long = 7&
Private Const xStaticIcons_TimerInd As Long = 8&
'''Public Const xStaticIcons_MP3FormatIcon As Long = 9&
Public Const xStaticIcons_Anim As Long = 9&
Private Const xStaticIcons_LastBMP = 9&
Public xStaticIconsBMP(xStaticIcons_LastBMP) As Long
Private Const xStaticIcons_Spectrum As Long = 10&
Public Const xStaticIcons_DirList As Long = 11&
Public Const xStaticIcons_CurSong As Long = 12&
Private Const xStaticIcons_CurSongPos As Long = 13&
Public Const xStaticIcons_Tags As Long = 14&
Public Const xStaticIcons_ShowIR As Long = 15&
Private Const xStaticIcons_Last = 15&
'private xHooks(xStaticIcons_Last) As Long
Public Const xStaticIcons_ToolTip As Long = 16&
Private Const xStaticIcons_BAR As Long = 17&
Public xStaticIconsHWnd(xStaticIcons_BAR) As Long
Private xStaticIconsToolTip(xStaticIcons_Tags, 1) As String
Public CurShownToolTip As Long
Private CurShownToolTip_SpHeight As Long
Private CurShownToolTip_SpKeyPre As Long
Private CurShownToolTip_SpKey As Long
Private CurShownToolTip_TotalWidthX As Long
Private Const TTSplitter = vbCr
Private Const TTSplitterTab = vbTab
Private Const TTSplitter2 = TTSplitter '+ TTSplitter

Private MP3FormatBitmap As Long

Public xListHWnd(2) As Long
Private xListHWndHook(2) As Long
Private VolumeHookOld As Long
Private BARHookOld As Long

Private ePic4Top As Long

Public xSharedDC As Long
Private xSharedDCOldFont As Long
Private hFontPLLg As Long
Public hFontPLLgB As Long
Private hFontPLSm As Long
Public hFontGSys As Long ' global dialog unit stuff
Private hFontTTip As Long
Public hFontGFix As Long ' win default font in my size?
Private hFontShIR As Long
Private hFontShIRIsVerticalBitmapFont As Boolean
Private VertIRBarHeight As Long
#If SelfPathElipse Then
  Public NeedIRPathElipse As String
  Private ShownIRPath As String
  #End If

Private Type InitCommonControlsExType
  dwSize As Long
  dwICC As Long
  End Type
Private Declare Function InitCommonControlsEx Lib "comctl32.dll" (ByRef xType1 As InitCommonControlsExType) As Long

'Private FirstDown As Long

' 0=calaymclass -> custom
' 1=animstuff   -> playlistsize    2   ok
' 2=dropdown1   -> smallgeneric    3

'Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Declare Sub OleInitialize Lib "Ole32.dll" (ByVal pvReserved As Long)
Private Declare Sub OleUninitialize Lib "Ole32.dll" ()
Public Declare Sub RegisterDragDrop Lib "Ole32.dll" (ByVal hWnd As Long, ByVal pDropTarget As Long)
Private Declare Sub RevokeDragDrop Lib "Ole32.dll" (ByVal hWnd As Long)


#If FlatCastCF17 Then
  Dim xFCI As CCtrlProd
  #End If

Private DragClass As IDropTarget
Public PlayerInMenuLoop As Boolean
Private Declare Function IsThemeBackgroundPartiallyTransparent Lib "uxtheme.dll" (ByVal hTheme As Long, ByVal iPartId As Long, ByVal iStateId As Long) As Long
Private Declare Function DrawThemeBackground Lib "uxtheme.dll" (ByVal hTheme As Long, ByVal lhdc As Long, ByVal iPartId As Long, ByVal iStateId As Long, pRect As RECT, ByVal pClipRect As Long) As Long
Private Declare Function DrawThemeParentBackground Lib "uxtheme.dll" (ByVal hWnd As Long, ByVal hdc As Long, prc As RECT) As Long

Private Declare Function IsBadWritePtr Lib "kernel32" (ByVal hMem As Long, ByVal ucb As Long) As Long
'Public IsVistaOrXP As Boolean

Private CurrentFindType As FINDREPLACE

Public GBN2 As String
'Public GBN3 As String

'private Const ToolTipDelay As Long = 2000&
Public ToolTipDelay As Long
Public ToolTipInitTime As Long

Public Reg_IsAutoRun As Boolean
Private Declare Function OpenFileMappingA Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
Private Declare Function UnmapViewOfFile Lib "kernel32" (ByVal lpBaseAddress As Long) As Long
Private Type MEMORY_BASIC_INFORMATION
  BaseAddress As Long
  AllocationBase As Long
  AllocationProtect As Long
  RegionSize As Long
  State As Long
  Protect As Long
  lType As Long
  End Type
Private Declare Function VirtualQuery Lib "kernel32" (ByVal lpAddress As Long, lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Long) As Long

Public Const W_BUTTON As String = "BUTTON"
Public Const W_EDIT As String = "EDIT"
Public Const W_LISTBOX As String = "LISTBOX"
Public Const W_STATIC As String = "STATIC"

'Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SystemTime)
Public sSystemTime As SYSTEMTIME

Private Declare Function IsThemeActive Lib "uxtheme.dll" () As Long
Private Declare Function IsAppThemed Lib "uxtheme.dll" () As Long
Private Declare Function OpenThemeData Lib "uxtheme.dll" (ByVal hWnd As Long, ByVal pszClassList As Long) As Long
Private Declare Function CloseThemeData Lib "uxtheme.dll" (ByVal hTheme As Long) As Long
Private hThemeComboDropButtons As Long

Private xCanSpectrum  As Boolean
  
#If 0 Then
  Private Type THREAD32ENTRY
    dwSize As Long
    cntUsage As Long
    th32ThreadID As Long
    th32OwnerProcessID As Long
    tpBasePri As Long
    tpDeltaPri As Long
    dwFlags As Long
    End Type
  Private Declare Function Thread32First Lib "kernel32" (ByVal hndl As Long, ByRef pstru As THREAD32ENTRY) As Boolean
  Private Declare Function Thread32Next Lib "kernel32" (ByVal hndl As Long, ByRef pstru As THREAD32ENTRY) As Boolean
  Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
  Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal dwIdProc As Long) As Long
  #End If
  
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
  
  
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
  
Public xPStateIconStartup As Boolean

Public PlayerIsLocked As Boolean

Public IsDialoging As Long
'private PLFontCur As Long
'private FontChooseDHWnd As Long

Public GlobalXInitRun As Boolean

Public xForceAnimationNow As Boolean


'Public xLastSearchExResultsSpace As Long
Public Const lWidth As Long = 4& '+ 4
Private DynButtonsResX As String
Private DynButtonsResXRep As String
Private xTimerIconBMP As String
Private FormatIconBackSunken As Long
Private FormatIconBackSunkenD As Long
Private FormatIconBackSunkenW As Long
Public xCmdSaveSet As Boolean
'private Const TrackerSkipperMinVal As Long = 20&
Public Const xAnySkipperMin As Long = 3&
Public Const xAnySkipperMax As Long = 60&
Private Const xAnySkipperMinN As Long = -xAnySkipperMin
Private Const xAnySkipperMaxN As Long = -xAnySkipperMax
Public Const IntroMdSkipperMinVal As Long = 20&
Public Const IntroMdSkipperMaxVal As Long = 99&
Private Const IntroModeTimeDef As Long = 30&
Public IntroModeActive As Boolean
Public IntroModeTime As Long
'Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Private GrabErrorVar As Long
Private ShowIR_IconHWnd1 As Long
Private ShowIR_IconHWnd2 As Long
Private ShowIR_IconHWnd As Long
Private ShowIR_Width As Long

'private Declare Function PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long

Public KeepMusicAliveTime As Long




Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpPoint As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long


'private xDirList As Long
Private Const FillSysColorBack As Long = 5& + 1& '&HF& + 1&
Private Const FillSysColorFore As Long = 13& + 1&
Private Const FillSysColorFace As Long = 15& + 1& '&HF& + 1&
Private Const FillSysColorSunkenD As Long = 16& + 1& '&HF& + 1&
Private Const FillSysColorSunkenW As Long = 20& + 1& '&HF& + 1&


Public ShowIRTicks As Long

Private FormatIconFore As Long
Private FormatIconBack As Long

Public MaxDeviceCount As Long
Private ePic1ScaleBonus As Long

Private xGlobalDialogUnits As Single
Private xGlobalDialogUnitsPLSm As Single
'private xGlobalDialogUnitsPL As Single
Public xGlobalDialogUnitsGFix As Single

Public xSearchAutoClose As Boolean
Public xSearchAutoPlay As Boolean

Public xRadioAutoClose As Boolean
'Public xCurrentRadioURL As String


Private OLEDropMemory As Long


Public Const ctSSLayer_MusicTypeNone As Long = 0&
Public Const ctSSLayer_MusicTypeTracker As Long = 1&
Private Const ctSSLayer_MusicTypeStream As Long = 2&
Private Const ctSSLayer_MusicTypeCDAudio As Long = 3&
Private Const ctSSLayer_MusicTypeInetStream As Long = 4&
Private Const ctSSLayer_SpectrumArrayCount As Long = 32&

Private xMWinPos As RECT


Private Const ctSSLayer_SpectrumArrayCountX = ctSSLayer_SpectrumArrayCount - 1&



Private Const KeyBoardVersionID As String = "100f00:"
Private Const KeyBoardVersionIDLen As Long = 7&
Private Const KeyBoardVersionIDLenP As Long = KeyBoardVersionIDLen + 1&
'#Const AlternateCallback = True
'#Const CallBackAddLog = True
#Const NoClearSpectrum = False
#Const MultiGUITimer = True
'#Global Const TimeTestDebug = 0
'#Global Const DetailLog = 0












Private Const AutoIgnoreWaitTime As Long = 10&
'Private Const AutoIgnoreWaitTimeStr As String = "10"
'Public Const FadingSteps As Long = 20& ' 10 ' 12&
'Public Const FadingTimer As Long = 100& ' 150 '250& '100&
Public Const SimpleFadingTotalTime As Long = 2000&
Public Const LongFadingTotalTimeStrt As Long = 8000&
Public Const LongFadingTotalTimeStrtF As Long = 4000&
Public Const LongFadingTotalTimeQuit As Long = 4000&
Public Const LongFadingTotalTimeShut As Long = 6500&
'Private Const FadingTimerEx As Long = 225& '700& '400&


'private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long
Private Const CalaymBackupSchemeREGName As String = ".CalaymBackup"
Public xCmdQuitMode As Long
Public xShellMode As Boolean
Public PlayerPriority As Long
Public SoundSchemePointerMode As Long
Private SoundSchemePointer As Long


Public xNoVerWarnDLL As String


Public StartUpWindowMode As Long



Public xSupportedFiles As String
Private xSupportedFilesS As String
Private xSupportedFilesM As String

Public xUsedTypes As String
Public xUsedTypesResort As Boolean
'private Const xUsedTypeID_None As Long = &H0&
'private Const xUsedTypeIDOgg As Long = &H1&
'private Const xUsedTypeIDmpg As Long = &H2&
'private Const xUsedTypeIDwma As Long = &H4&
'private Const xUsedTypeIDflk As Long = &H8&
'private Const xUsedTypeIDmpc As Long = &H10&
'private Const xUsedTypeIDaac As Long = &H20&
'private Const xUsedTypeIDwav As Long = &H40&
'    private Const xUsedType_Streams As Long = &H7F&
'private Const xUsedTypeIDmod As Long = &H80&
'private Const xUsedTypeIDmid As Long = &H100&
'    private Const xUsedType_Tracker As Long = (xUsedTypeIDmod Or xUsedTypeIDmid)
'private Const xUsedTypeIDcda As Long = &H200&
'private Const xUsedTypeIDCount As Long = 10&
'private Const xUsedTypeIDCountX As Long = 9&





Public CDDAUse As Boolean
Public CDDAJitter As Boolean
Public CurMusic As Long
Public xPStateIconPlaying As Boolean
Public xPStateIconPaused As Boolean
Private xHasNextSong As Boolean
Private SilentPlNext As Boolean
'private Const CDDAVersion As Long = 37100


Public KioskDown As Boolean

Public ShowFullPath As Boolean
Public ShowFullTitle As Boolean

Private Const GUIInterval As Long = 1000&
Private Const GUIMinWaitInterval As Long = 100&
'private ShowIRWin As Long
Private xCalaymClassAlign As Long

Public AbortReading As Boolean
Private LEDState As Long
Public LEDMode As Long
Public MainHWnd As Long
Public xAutoIgnoreTime As Long
Public xNoSpektrumOnStartup As Boolean
Public xRealBaseFile As String

Public xLastDir As String

Public m3uList As String
Public TimerDir As String
Public xLastDirectory As String
Public PresetFile As String

Private FirstTimeHelp As Boolean
'private settingx(5) As Long   ' timer setup!
Public xSetVolume As Long
Public xRep As Long
Public xGenFading As Boolean
Public xProcessMode As Long
Public xRemoveMissingSongs As Boolean
Public xRemovePlayedSongs As Boolean
Public xRemoveSkippedSongs As Boolean
Private xFollowCurSong As Boolean
Public MP3GetID3Tag As Boolean
Public xShowSpectrum As Boolean
'#If PresetBinded = 2 Then
'  private PSetManVisible As Boolean
'  #End If

Public RandomPosOnTrackerSkipper As Boolean

Public AskSndReset As Long
Public AskSndResetButton As Long
Public Const xAutoRefreshTimeMinAll As Long = 10&
Public Const xAutoRefreshTimeMaxAll As Long = 180&
'Private Const xAutoRefreshTimeDefAll As Long = -30&
Public Const xAutoRefreshTimeMin As Long = 2&
Public Const xAutoRefreshTimeMax As Long = 30&
'Private Const xAutoRefreshTimeDef As Long = -5&
Public xModPanning As Long
Private LastDoneTimer As Long
Public Const vPLXMax As Long = 64&
Public vPLX(vPLXMax * 3& - 1&) As String
Public xTimerPLMenu(2) As Long
Public xTimerPLMenuLoaded(2) As Long
Public vPLXMain As String
Public xSorting As Boolean
Private xTimer1 As Boolean
Private xTimer2 As Boolean
Private xTimer3 As Boolean
Public GlobalKeys As Boolean
Public Const xTimerEvMax As Long = 256&
Public xTimerEv(xTimerEvMax) As String
Public xTimerEvTime(xTimerEvMax) As Long
Public xTimerEvCount As Long
Public xTimerEvIR As Long
Public xTimerEvMode As Long
Public xTimerEvUpdateTimer As Long


Public Const xTimerEvMaxSubEvents As Long = 24&
Public Const xTimerEvMaxSubEventsX As Long = xTimerEvMaxSubEvents - 1&
Public Const TimerSepp As String = "|"

Public CheckAccurate As Boolean
Public IgnoreUnderscored As Boolean
Public RemoveMissingOnPLLoad As Boolean
Public xClassicBlockingOnLoad As Boolean

Public xShowElapsed As Boolean
Public xTitlesWhenMinimized As Long
Public xTitlesPos As Long
Public vKioskMode As Boolean
Public UsePLSettings As Boolean


Public FLineColor As Long
Private FLineColorX As Long
Private FLineColorWDCTxt As Long
'private xColorMod As Long

Private Const InfoProcInterval As Long = 100& '100&

Private xMaxVU(32) As Long
Private xMaxVUWait(32) As Long
Private Const xMaxVUWaitInit = 1200& / InfoProcInterval

'private Const MaxInt As Long = 32766&
Public xAutoDoing As Boolean
Private xHiddenAutoDoing As Boolean
Public xOutputSSLID As String
Public xDelMode As Boolean
Public xSecureMode As Boolean
Public xAutoPlayback As Boolean
Public xReAutoPlay As Boolean

Public xTrackerSkipper(3) As Long
Public xTrayIconEnabled As Boolean
Public xTrayIconVisible As Boolean
Public xTrayIconLeft As Long
Public xTrayIconRight As Long
Private xInfoQuit As Boolean
Public xDeluxeSpectrum As Boolean
'private AutoRefreshVisible As Boolean
Private AutoRefreshApplyHWnd As Long
'private SetupVisible As Boolean
'private xfrmSearchExVisible As Boolean
Public CPListText As String
Public CPListMustDo As Boolean
Public CPListTime As Long
Private Const CPListDelay As Long = 1100&
Public PlDClickAction As Long
Public MP3GetFormat As Boolean

Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private MapVirtualKeyA_H14 As Long
Private MapVirtualKeyA_H90 As Long
Private MapVirtualKeyA_H91 As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

#Const HowToDeferWindowPos = 0 ' No real improvements either!
#If HowToDeferWindowPos = 3 Then
  Private TrDeferWindowPos_Coll As Collection
  Private Type TrDeferWindowPos_Type
    hWnd As Long
    hWndInsertAfter As Long
    x As Long
    y As Long
    cx As Long
    cy As Long
    wFlags As Long
    End Type
  Private Const TrDeferWindowPos_TypeLen As Long = 28&
  Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
  Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  Private Declare Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
  Private TrDeferWindowPos_LastParent As Long
  Private Declare Function GetParent Lib "User32.dll" (ByVal hWnd As Long) As Long
  #ElseIf HowToDeferWindowPos = 2 Then
  Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
  Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  Private Declare Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
  Private TrDeferWindowPos_Handle As Long
  Private Declare Function GetParent Lib "User32.dll" (ByVal hWnd As Long) As Long
  #ElseIf HowToDeferWindowPos = 3 Then
  Private TrDeferWindowPos_Active As Boolean
  #Else
  Private Declare Function TrDeferWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  #End If

#If HowToDeferWindowPos Then
  Private Sub TrDeferWindowPos(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    #If HowToDeferWindowPos = 1 Then
      Dim xType1 As TrDeferWindowPos_Type
      Dim xLong1 As Long
      Dim xLong2 As Long
      'Dim xLong3 As Long
     'Const SWP_SHOWWINDOW = &H40
      'Const SWP_HIDEWINDOW = &H80
      ' SWP_NOSENDCHANGING 400
      If Not GetParent(hWnd) = MainHWnd Then
        If Not IsWindowVisible(hWnd) = 0& Then
          ShowWindow hWnd, 0&
          End If
        Exit Sub
        End If
    'Stop
      If Not wFlags And &H4C0& Then
        xLong1 = LocalAlloc(0&, TrDeferWindowPos_TypeLen)
        End If
      If xLong1 = 0& Then
        TrEndDeferWindowPos
        Call SetWindowPos(hWnd, hWndInsertAfter, x, y, cx, cy, wFlags)
        Else
        If Not xLong2 = TrDeferWindowPos_LastParent Then
          If Not TrDeferWindowPos_Coll.Count = 0& Then
            TrEndDeferWindowPos
            End If
          xLong2 = TrDeferWindowPos_LastParent
          End If
        With xType1
          .hWnd = hWnd
          .hWndInsertAfter = hWndInsertAfter
          .x = x
          .y = y
          .cx = cx
          .cy = cy
          .wFlags = wFlags
          End With
        Call RtlMoveMemory(ByVal xLong1, xType1, TrDeferWindowPos_TypeLen)
        TrDeferWindowPos_Coll.Add xLong1
        End If
      #ElseIf HowToDeferWindowPos = 2 Then
      Dim xLong1 As Long
      
      If Not GetParent(hWnd) = MainHWnd Then
        If Not IsWindowVisible(hWnd) = 0& Then
          Debug.Print "HideIgnore!"
          ShowWindow hWnd, 0&
          End If
        Debug.Print "Ignore!"
        Exit Sub
        End If
      If TrDeferWindowPos_Handle = 0& Then
        TrDeferWindowPos_Handle = BeginDeferWindowPos(99&)
        End If
      If Not TrDeferWindowPos_Handle = 0& Then
        TrDeferWindowPos_Handle = DeferWindowPos(TrDeferWindowPos_Handle, hWnd, hWndInsertAfter, x, y, cx, cy, wFlags Or &H8&)
        End If
      #ElseIf HowToDeferWindowPos = 3 Then
      TrDeferWindowPos_Active = True
      Call SetWindowPos(hWnd, hWndInsertAfter, x, y, cx, cy, wFlags Or &H8)
      #Else
      Call SetWindowPos(hWnd, hWndInsertAfter, x, y, cx, cy, wFlags)
      #End If
    End Sub
  
  Private Sub TrEndDeferWindowPos()
    #If HowToDeferWindowPos = 1 Then
      Dim xLong1 As Long
      Dim xLong2 As Long
      Dim xLong3 As Long
      Dim xType1 As TrDeferWindowPos_Type
    'Stop
      xLong1 = TrDeferWindowPos_Coll.Count
      If Not xLong1 = 0& Then
        xLong2 = BeginDeferWindowPos(xLong1)
        If Not xLong2 = 0& Then
          For xLong1 = 1& To TrDeferWindowPos_Coll.Count
            xLong3 = TrDeferWindowPos_Coll(xLong1)
            Call RtlMoveMemory(xType1, ByVal xLong3, TrDeferWindowPos_TypeLen)
            With xType1
    If xLong1 = 1& Then
      Debug.Print .hWnd, .hWndInsertAfter, .x, .y, .cx, .cy, .wFlags
      End If
              xLong2 = DeferWindowPos(xLong2, .hWnd, .hWndInsertAfter, .x, .y, .cx, .cy, .wFlags)
              End With
            If xLong2 = 0& Then Exit For
            Next
          End If
        If Not xLong2 = 0& Then
    Debug.Print "NewDone"
    'Stop
          xLong2 = EndDeferWindowPos(xLong2)
          End If
        If xLong2 = 0& Then
    Debug.Print "OldDone"
          For xLong1 = 1& To TrDeferWindowPos_Coll.Count
            xLong3 = TrDeferWindowPos_Coll(xLong1)
            Call RtlMoveMemory(xType1, ByVal xLong3, TrDeferWindowPos_TypeLen)
            With xType1
              xLong2 = SetWindowPos(.hWnd, .hWndInsertAfter, .x, .y, .cx, .cy, .wFlags)
              End With
            Next
          End If
        End If
      Debug.Print "  Cleanup"
      For xLong1 = TrDeferWindowPos_Coll.Count To 1& Step -1&
        xLong3 = TrDeferWindowPos_Coll(xLong1)
        Call LocalFree(xLong3)
        TrDeferWindowPos_Coll.Remove xLong1
        Next
      #ElseIf HowToDeferWindowPos = 2 Then
      Call EndDeferWindowPos(TrDeferWindowPos_Handle)
      TrDeferWindowPos_Handle = 0&
      #End If
    End Sub
  #End If

Private Sub Main()
'  AquirePrivilege ("Test")
'  FirstDown = -1&
  Dim xType1 As InitCommonControlsExType

  #If HowToDeferWindowPos = 1 Then
    Set TrDeferWindowPos_Coll = New Collection
    #End If
  App.TaskVisible = False

  xType1.dwSize = 8&
  xType1.dwICC = &H138&
  Call InitCommonControlsEx(xType1)
  
  
  'Load OLEHelperForm
  DoThemeUpdates
  MainForm_StartPlayer
  If MainHWnd = 0& Then
    If ptrTemp = 0& Then Call ApplicationClose
    Else
    MessageHandler
    End If
  End Sub

Private Function MainForm_CreatePlayerForm() As Boolean
  Dim xType1 As WNDCLASSEX
    
  'Dim xType2 As RECT
  Dim xLong1 As Long
  
  'MainFormDraggingIs = false
  'PLRemoveShifted1 = &H7FFFFFFF
  'PLRemoveShifted3 = &H7FFFFFFF


  With xType1
    '.cbSize = 48&
    .Style = &H83&
    '.hInstance = App.hInstance
    
    
    
    
    NSCursor = LoadCursor(&H7F85&)
    .hCursor = NSCursor
    .hbrBackground = 19&
    .ClassName = CalaymClassSplitterMove
    Call RegisterClassEx(xType1, AddressOf NoProc)
    
    
    .Style = &H83&
    .hCursor = LoadCursor(&H7F00&)
    .hbrBackground = 25&
    .ClassName = CalaymClassToolTip
    Call RegisterClassEx(xType1, AddressOf RedrawProc)
    
    
    .hbrBackground = 6&
    .ClassName = CalaymClassNameMsg
    Call RegisterClassEx(xType1, AddressOf ProcClassWnd)
    
    
    
    .Style = &HB  ' &H4B& / 40 causes disp. problems after standby (win7)
    .ClassName = CalaymClassStaticWhiteDC
    Call RegisterClassEx(xType1, AddressOf RedrawProc)
    
    
    
    
    .Style = &H83&
    .hbrBackground = 16&
    .ClassName = CalaymClassSplitter
    Call RegisterClassEx(xType1, AddressOf SplitterProc)
    
    
    .Style = &H3&
'    xClassIcon = LoadResEx_Icon(2900&, 32&)
'    xClassIconSmall = LoadResEx_Icon(2900&, 16&)
    '.hIconSm = xClassIconSmall
    '.hIcon = xClassIcon
    .ClassName = CalaymClassForm
    
    Call RegisterClassEx(xType1, AddressOf PlayerFormProc)
    End With
  

  'MainHWnd = CreateWindowEx(&H0&, CalaymClassForm, "", &H8CF1000, 0&, 0&, 0&, 0&, 0&, 0&, 0&)
  MainHWnd = CreateWindowEx(&H0&, CalaymClassForm, "", &HECF1000, 0&, 0&, 0&, 0&, 0&, 0&, 0&)
  If Not MainHWnd = 0& Then
    Call OleInitialize(0&)

    ePic4Top = DirAndCurSongBarHeight '25&
    HWndSplitter = CreateWindowEx(0&, CalaymClassSplitter, "", &H56000000, 0&, ePic4Top, 369&, 4&, MainHWnd, 0&, 1055&)
    HWndSplitterMove = CreateWindowEx(0&, CalaymClassSplitterMove, "", &H46000000, 0&, ePic4Top, 369&, 4&, MainHWnd, 0&, 1055&)
    
    'private Const LBS_HASSTRINGS = &H40&
    xListHWnd(2) = CreateWindowEx(&H200&, W_LISTBOX, "", &H56300918, 0&, 24&, 377&, 308&, MainHWnd, 0&, 1050&)
    Call SendMessage(xListHWnd(2), &H1A8&, PLCountMax, 0&)
    xListHWnd(0) = CreateWindowEx(&H20201, W_LISTBOX, "", &H46300110, 0&, 17&, 361&, 122&, MainHWnd, 0&, 1060&)
    xListHWnd(1) = CreateWindowEx(&H20201, W_LISTBOX, "", &H46300110, 0&, 216&, 377&, 122&, MainHWnd, 0&, 1061&)
    
    xStaticIconsHWnd(xStaticIcons_Anim) = CreateWindowEx(&H0&, W_STATIC, "", &H4600090E, 320&, 40&, 32&, 40&, MainHWnd, 0&, 1090&)
    Call SetWindowPos(xListHWnd(2), 1&, 0&, 0&, 0&, 0&, &H213&)
    Call SetWindowPos(xListHWnd(0), 0&, 0&, 0&, 0&, 0&, &H213&)
    Call SetWindowPos(xListHWnd(1), 0&, 0&, 0&, 0&, 0&, &H213&)
    Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, 0&, 0&, 0&, 0&, &H213&)
      
    For xLong1 = 0& To 2&
      If xListHWnd(xLong1) = 0& Then
        Exit Function
        Else
        xListHWndHook(xLong1) = SetWindowLong(xListHWnd(xLong1), -4&, AddressOf ListsHook)
        End If
      Next
    
      
    xStaticIconsHWnd(xStaticIcons_BAR) = CreateWindowEx(&H0&, W_STATIC, "", &H56000000, 0&, 0&, 0&, 0&, MainHWnd, 0&, 0&)
    If Not xStaticIconsHWnd(xStaticIcons_BAR) = 0& Then
      BARHookOld = SetWindowLong(xStaticIconsHWnd(xStaticIcons_BAR), -4&, AddressOf BARHook)
'      Stop
      If vKioskMode Then
        xStaticIconsHWnd(xStaticIcons_Def3) = CreateWindowEx(&H0&, W_STATIC, "", &H5000090E, 344, 0&, 20&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1000& + xLong1)
        Else
        For xLong1 = 0& To 5&
          xStaticIconsHWnd(xLong1) = CreateWindowEx(&H0&, W_STATIC, "", &H5000090E, 8& + 24& * xLong1, PlayControlsTop, 20&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1000& + xLong1)
          Next
        xStaticIconsHWnd(xStaticIcons_Volume) = CreateWindowEx(&H0&, W_STATIC, "", &H5000090E, VolumePosInitLeft, PlayControlsTop, 40&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1006&)
        VolumeHookOld = SetWindowLong(xStaticIconsHWnd(xStaticIcons_Volume), -4&, AddressOf VolumeHook)
        
        'xStaticIconsHWnd(xStaticIcons_Volume) = CreateWindowEx(&H0&, CalaymClassStaticVolume, "", &H50000000, 152&, 31&, 40&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1006&)
        xStaticIconsHWnd(xStaticIcons_RepMode) = CreateWindowEx(&H0&, W_STATIC, "", &H5000090E, 196&, PlayControlsTop, 20&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1007&)
        xStaticIconsHWnd(xStaticIcons_TimerInd) = CreateWindowEx(&H0&, W_STATIC, "", &H5000090E, 220&, PlayControlsTop, 20&, 16&, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1008&)
        End If
      
      #If ProcCtl Then
        xStaticIconsHWnd(xStaticIcons_CurSongPos) = CreateWindowEx(&H200, w_Progress, "", &H56000001, 0&, CurSongPosRealTop, 377&, CurSongPosRealHeight, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1025&)
        #Else
        xStaticIconsHWnd(xStaticIcons_CurSongPos) = CreateWindowEx(&H200, CalaymClassStaticWhiteDC, "", &H56000000, 0&, CurSongPosRealTop, 377&, CurSongPosRealHeight, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1025&)
        #End If
      xStaticIconsHWnd(xStaticIcons_CurSong) = CreateWindowEx(&H200, CalaymClassStaticWhiteDC, "", &H56000000, 0&, 0&, 324&, DirAndCurSongBarRealHeightOverlap, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 1021&)
      hWndTagsTop = hWndTagsTopDef
'      xStaticIconsHWnd(xStaticIcons_Tags) = CreateWindowEx(&H20000, W_EDIT, "", &H50000944, 0&, hWndTagsTop, hWndTagsWidth, hWndTagsHeight, xStaticIconsHWnd(xStaticIcons_BAR), &H1001&, 0&)
      xStaticIconsHWnd(xStaticIcons_Tags) = CreateWindowEx(&H20000, W_EDIT, "", &H50000944, 0&, hWndTagsTop, hWndTagsWidth, hWndTagsHeight, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 0&)
      xStaticIconsHWnd(xStaticIcons_Spectrum) = CreateWindowEx(&H20000, CalaymClassStaticWhiteDC, "", &H40000000, hwndSpectrumLeft, hwndSpectrumTop, hwndSpectrumWidth, hwndSpectrumHeight, xStaticIconsHWnd(xStaticIcons_BAR), 0&, 0&)
      xStaticIconsHWnd(xStaticIcons_ShowIR) = CreateWindowEx(&H88&, CalaymClassNameMsg, CalaymClassTitleMsg, &H8E800000, 0&, 0&, 400&, 35&, 0&, 0&, 0&)
      xStaticIconsHWnd(xStaticIcons_ToolTip) = CreateWindowEx(&H84&, CalaymClassToolTip, "", &H8E800000, 0&, 0&, 160&, 16&, MainHWnd, 0&, 0&)

      xSharedDC = CreateCompatibleDC(0&)

      If Not xStaticIconsHWnd(xStaticIcons_Anim) = 0& Then
        xStaticIconsBMP(xLong1) = CreateCompatibleBitmapX(32&, 40&)
        End If
      If Not xStaticIconsHWnd(xStaticIcons_ShowIR) = 0& Then
        ShowIR_IconHWnd1 = LoadResEx_Icon(2900&)
        If Not ShowIR_IconHWnd1 = 0& Then
          ShowIR_IconHWnd2 = LoadResEx_Icon(2920&)
          ShowIR_IconHWnd = ShowIR_IconHWnd1
          End If
        End If
      
      End If
    xStaticIconsHWnd(xStaticIcons_DirList) = CreateWindowEx(&H200&, CalaymClassStaticWhiteDC, "", &H56000000, 0&, 0&, 361&, DirAndCurSongBarHeight, MainHWnd, 0&, 1020&)
    
    PLIconFolder = LoadResEx_Icon(PLIconFolder_ResID, 16&)
    If Not PLIconFolder = 0& Then
      PLIconFile = LoadResEx_Icon(PLIconFile_ResID, 16&)
      If PLIconFile = 0& Then
        Call DestroyIcon(PLIconFolder)
        PLIconFolder = 0&
        End If
      End If
    PLIconURL = LoadResEx_Icon(PLIconURL_ResID, 16&)

    SysTrayIcon.hWnd = xStaticIconsHWnd(xStaticIcons_ShowIR)
    SysTrayIcon.uFlags = 7&
    SysTrayIcon.uCallBackMessage = 512&
    
    If Not (xListHWnd(0) = 0& Or xSharedDC = 0&) Then
      MainForm_CreatePlayerForm = True
      'MainForm_ResizePlayer False
      #If XPDeviceRemoveDetect Then
        Call RemovableDevices_RegisterAll(MainHWnd)
        #End If
      'Load OLEHelperForm
      End If
    End If
  End Function

Private Sub DestroyPlayerForm()
  Dim xLong1 As Long

  If Not MainHWnd = 0& Then
    If Not InterfaceMode = Interface_Mode_None Then Call ToggleInterface(Interface_Mode_None)
    If Not OldAnimIcon = 0& Then DestroyIcon OldAnimIcon
    
    If Not PLIconFolder = 0& Then
      Call DestroyIcon(PLIconFolder)
      Call DestroyIcon(PLIconFile)
      End If

    #If XPDeviceRemoveDetect Then
      RemovableDevices_UnregisterAll
      #End If

    If Not xSharedDC = 0& Then
      If ShowIRWin Then
        Call KillTimer(xStaticIconsHWnd(xStaticIcons_ShowIR), 3&)
        ShowIRWin = False
        End If
      Call DestroyWindow(xStaticIconsHWnd(xStaticIcons_ShowIR))
      If Not ShowIR_IconHWnd1 = 0& Then
        Call DestroyIcon(ShowIR_IconHWnd1)
        If Not ShowIR_IconHWnd2 = 0& Then Call DestroyIcon(ShowIR_IconHWnd2)
        End If
      
      If Not xSharedDCOldFont = 0& Then
        Call SelectObject(xSharedDC, xSharedDCOldFont)
        End If
      
      Call DeleteDC(xSharedDC)
      End If
    
    
    'For xLong1 = 0& To xStaticIcons_Last
    '  If Not xHooks(xLong1) = 0& Then
    '    Call SetWindowLong(xStaticIconsHWnd(xLong1), -4&, xHooks(xLong1))
    '    End If
    '  Next
    'Unload OLEHelperForm
    UnTheme
    
    For xLong1 = 0& To 2&
      If Not xListHWndHook(xLong1) = 0& Then
        Call SetWindowLong(xListHWnd(xLong1), -4&, xListHWndHook(xLong1))
        End If
      Next
    
    If Not xStaticIconsHWnd(xStaticIcons_BAR) = 0& Then Call SetWindowLong(xStaticIconsHWnd(xStaticIcons_BAR), -4&, BARHookOld)
    If Not xStaticIconsHWnd(xStaticIcons_Volume) = 0& Then Call SetWindowLong(xStaticIconsHWnd(xStaticIcons_Volume), -4&, VolumeHookOld)
    
    Call DestroyWindow(xStaticIconsHWnd(xStaticIcons_ToolTip))
    
    Call LoadResEx_WindowIcon(MainHWnd, -1&, VarPtr(SysTrayIcon))
    
    'Call DoAnimateSetIcon(-1&)
    DestroyWindow MainHWnd
    
    If Not hFontGFix = 0& Then Call DeleteObject(hFontGFix)
    If Not hFontShIR = 0& Then Call DeleteObject(hFontShIR)
    If Not hFontPLLgB = 0& Then Call DeleteObject(hFontPLLgB)
    If Not hFontPLLg = 0& Then Call DeleteObject(hFontPLLg)
    If Not hFontPLSm = 0& Then Call DeleteObject(hFontPLSm)
    If Not hFontTTip = 0& Then Call DeleteObject(hFontTTip)
    
    
    
    Call OleUninitialize
    MainHWnd = 0&

    For xLong1 = 0& To xStaticIcons_LastBMP
      If Not xStaticIconsBMP(xLong1) = 0& Then
        Call DeleteObject(xStaticIconsBMP(xLong1))
        End If
      Next
    If Not MP3FormatBitmap = 0& Then
      Call DeleteObject(MP3FormatBitmap)
      End If
    End If
  
  Call UnregisterClass(CalaymClassNameMsg)
  Call UnregisterClass(CalaymClassStaticWhiteDC)
  Call UnregisterClass(CalaymClassForm)
  Call UnregisterClass(CalaymClassSplitterMove)
  Call UnregisterClass(CalaymClassSplitter)
  Call UnregisterClass(CalaymClassToolTip)
  'Call UnregisterClass(CalaymClassStaticVolume)
  
  
  'If Not xClassIcon = 0& Then
  '  DestroyIcon xClassIcon
  '  xClassIcon = 0&
  '  End If
  'If Not xClassIconSmall = 0& Then
  '  DestroyIcon xClassIconSmall
  '  xClassIconSmall = 0&
  '  End If
  End Sub

Private Sub ResizeRemember()
  Dim xType1 As RECT
  If Not GetWindowRect(MainHWnd, xType1) = 0& Then
    If IsIconic(MainHWnd) = 0& Then
      If IsZoomed(MainHWnd) = 0& Then
'Debug.Print "Applying!"
        xMWinPos.Left = xType1.Left
        xMWinPos.Top = xType1.Top
        xMWinPos.Right = xType1.Right - xType1.Left
        xMWinPos.Bottom = xType1.Bottom - xType1.Top
        End If
      End If
    End If
  End Sub

Private Function PlayerFormProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H2B& ' WM_DRAWITEM
      Call DrawPLItem(lParam)
      PlayerFormProc = 1&
      Exit Function
    Case &H3& ' WM_MOVE
      ResizeRemember
    Case &H5& ' WM_SIZE
      If wParam = 0& Then ResizeRemember
      MainForm_ResizePlayer True
    Case &H7& ' WM_SETFOCUS
      BestFocus
    Case &H6& ' WM_ACTIVATE
      If Not IsWindowVisible(xListHWnd(0)) = 0& And xDirList < 0& Then
        ShowWindow xListHWnd(0), 0&
        DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
        End If
      If Not IsWindowVisible(xListHWnd(1)) = 0& Then
        ShowWindow xListHWnd(1), 0&
        DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
        End If
      If xIsVoluming Then xIsVoluming = False: ReleaseCapture
      If MainFormIsSplitterMoving Then
        MainFormIsSplitterMoving = False
        ReleaseCapture
        Call SetWindowPos(HWndSplitterMove, 1&, 0&, 0&, 0&, 0&, &H213&)
        ShowWindow HWndSplitterMove, 0&
'        ResizePlayer
        End If
      MainFormDraggingIs = False
      SearchFormDraggingIs = False
      'PlayerFormProc = 0&
      Exit Function
    Case &H16& ' WM_ENDSESSION
      If Not wParam = 0& Then
        GoTo EndSessionClose
        End If
    Case &H10& ' WM_CLOSE
EndSessionClose:
      If xIsVoluming Then xIsVoluming = False: ReleaseCapture
      If MainFormIsSplitterMoving Then
        MainFormIsSplitterMoving = False
        ReleaseCapture
        Call SetWindowPos(HWndSplitterMove, 1&, 0&, 0&, 0&, 0&, &H213&)
        ShowWindow HWndSplitterMove, 0&
        End If
      MainFormDraggingIs = False
      SearchFormDraggingIs = False
      MainForm_UnloadPlayer
    Case &H111& ' WM_COMMAND
      Select Case InterfaceMode
        Case Interface_Mode_Search
          If Not lParam = 0& Then Call SearchForm_TrySCommand(lParam, wParam)
        Case Interface_Mode_Timer
          If lParam = 0& Then
            TimerListForm_MenuEvent wParam
            Else
            TimerListForm_ButtonEvent lParam ', wParam
            End If
        Case Interface_Mode_Radio
          If lParam = 0& Then
            RadioForm_MenuEvent wParam
            Else
            Call RadioForm_TrySCommand(lParam, wParam)
            End If
        Case Else
          If lParam = 0& Then MainForm_MenuEvent wParam
        End Select
      If IsUnicode Then
        PlayerFormProc = DefWindowProcW(hWnd, Message, wParam, lParam)
        Else
        PlayerFormProc = DefWindowProcA(hWnd, Message, wParam, lParam)
        End If
    'Case &H233& ' WM_DROPFILES
    '  DropFiles wParam ', 1&, 0&
    Case &HA1& ' WM_NCLBUTTONDOWN
      If hWnd = xStaticIconsHWnd(xStaticIcons_CurSongPos) Then
        MainForm_ePic_MouseDown 100&, 0&
        ElseIf IsUnicode Then
        PlayerFormProc = DefWindowProcW(hWnd, Message, wParam, lParam)
        Else
        PlayerFormProc = DefWindowProcA(hWnd, Message, wParam, lParam)
        End If
    Case &H100& ' WM_KEYDOWN
      Select Case wParam
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
          Exit Function
        Case 27&
          If Not InterfaceMode = Interface_Mode_None Then ToggleInterface Interface_Mode_None
        End Select
    Case &H20B&, &HAB&
      If (wParam And &H60) Then
        MainForm_TryFindKeyEvent (Hex(wParam And &H60) \ 20& + 4&) + ((wParam And &HC&) \ 4&) * &H100&
        End If
    Case &H211& ' WM_ENTERMENULOOP
      PlayerInMenuLoop = True
    Case &H212& ' WM_EXITMENULOOP
      PlayerInMenuLoop = False
    Case &H105& 'syskeyup
      'If (lParam And &H40000000) = 0& Then
        Select Case InterfaceMode
          Case Interface_Mode_Search: Call SearchForm_TryAccel(wParam)
          Case Interface_Mode_Timer:  Call TimerListForm_TryAccel(wParam)
          Case Interface_Mode_Radio: Call RadioForm_TryAccel(wParam)
          End Select
      '  End If
    Case &H20A& 'WM_MouseWheel
      If (wParam And &HC) And Not PlayerIsLocked Then
        'MouseWheelDeltaVol = MouseWheelDeltaVol + (wParam And &HFFFF0000) \ &H10000
        Message = (wParam And &HFFFF0000) \ &H10000
        If Message < 0& And MouseWheelDeltaVol > 0& Then
          MouseWheelDeltaVol = 0&
          ElseIf Message > 0& And MouseWheelDeltaVol < 0& Then
          MouseWheelDeltaVol = 0&
          End If
        MouseWheelDeltaVol = MouseWheelDeltaVol + Message
        Message = xSetVolume + Fix(MouseWheelDeltaVol / MouseWheelDeltaDiv)
        MouseWheelDeltaVol = MouseWheelDeltaVol Mod MouseWheelDeltaDiv
        If Message > 255& Then
          Message = 255&
          ElseIf Message < 0& Then
          Message = 0&
          End If
        If Not xSetVolume = Message Then
          DoVolume Message, True
          End If
        End If
'&H211
    Case RegisteredFindMsgID
      If RegisteredFindMsgID = 0& Then
        If IsUnicode Then
          PlayerFormProc = DefWindowProcW(hWnd, Message, wParam, lParam)
          Else
          PlayerFormProc = DefWindowProcA(hWnd, Message, wParam, lParam)
          End If
        ElseIf (CurrentFindType.Flags And &H40&) = 0& Then
        If Not (CurrentFindType.Flags And &H8&) = 0& Then
          If (CurrentFindType.Flags And &H4&) = 4& Then
            CurrentFindMatchCase = vbBinaryCompare
            Else
            CurrentFindMatchCase = vbTextCompare
            End If
          If (CurrentFindType.Flags And &H1&) = 1& Then
            CurrentFindInvert = 1&
            Else
            CurrentFindInvert = -1&
            End If
          FindNowCB
          End If
        ElseIf Not CurrentFindHWnd = 0& Then
        CurrentFindHWnd = 0&
        LocalFree CurrentFindHMem
        CurrentFindHMem = 0&
        DialogTimer False
        EnableWindow MainHWnd, 1&
        End If
    Case &H219& 'Const WM_DEVICECHANGE As Long = &H219
      'Const BROADCAST_QUERY_DENY As Long = &H424D5144
      'BROADCAST_QUERY_DENY
      Select Case wParam
        Case &H8000& ' DBT_DEVICEARRIVAL
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "DeviceChange: DBT_DEVICEARRIVAL"
            #End If
          If Not xPStateIconQuit Then
            MainForm_XMenuRefresherDevices
            'If vKioskMode Then                'Or DeviceAddAutoPlay Then
              Call RemoveDevicesAutoAdd(lParam)
            '  End If
            End If
          PlayerFormProc = True
        Case &H8001& ' DBT_DEVICEQUERYREMOVE
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "DeviceChange: DBT_DEVICEQUERYREMOVE"
            #End If
#If UseFileLogging Then
          If RemoveDevicesLock(lParam, True) Then
            MainForm_ctConsoleA_AddLog "  permitted (player is locked now)"
#Else
          If RemoveDevicesLock(lParam, True) Then
#End If
            RemoveDevices lParam, False
            PlayerFormProc = 1&
'GoTo AlsoRemoveNow
            Else
            PlayerFormProc = &H424D5144
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  denied (player is locked)"
              #End If
            End If
        Case &H8002& ' DBT_DEVICEQUERYREMOVEFAILED
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "DeviceChange: DBT_DEVICEQUERYREMOVEFAILED"
            #End If
          If IsDevicePlugging And RemoveDevicesAffectsVolumes(lParam) Then
            If HasDevicePlugStoppedSong Then
              StoreResume True, 0&
              End If
            RemoveDevicesUnlock
            End If
          PlayerFormProc = True
        Case &H8003& ' DBT_DEVICEREMOVEPENDING
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "DeviceChange: DBT_DEVICEREMOVEPENDING"
            #End If
          Call RemoveDevicesLock(lParam, False)
'AlsoRemoveNow:
          RemoveDevices lParam, True
          'RemoveDevicesUnlock
          
          PlayerFormProc = True
        Case &H8004& ' DBT_DEVICEREMOVECOMPLETE
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "DeviceChange: DBT_DEVICEREMOVECOMPLETE"
            #End If
          If RemoveDevicesAffectsVolumes(lParam) Then
            '#If UseFileLogging Then
            '  MainForm_ctConsoleA_AddLog "  affects volumes"
            '  #End If
            If Not HasDevicePlugged Then
              'HasDevicePlugged = False
              'Else 'If Not xLocked Or xAutoDoing Then
              RemoveDevices lParam, True
              'RemoveDevicesUnlock
              'ProcClassWnd = True
              End If
            HasDevicePlugged = False
            MainForm_XMenuRefresherDevices
            
            If HasDevicePlugStoppedSong Then
              RemoveDevicesUnlock
              StoreResume True, 1&
              'If xPlayItem(False, False, False, False, False, "") Then
              '  Else
              '  RemoveDevicesUnlock
              '  If PlNextSong = "" Then
              '    If xRep = 2& Then
              '      PLCurItem = RandomSong
              '      Else
              '      If Not PLCurItem = 0& Then PLCurItem = PLCurItem - 1&
              '      xFindNextItem 1&, True, False, False, False
              '      End If
              '    Else
              '    MainForm_LockMe False
              '    MainForm_PlayNext True, 1&, False, False
              '    End If
              '  MainForm_xPlayClick False
              '  End If
              Else
              RemoveDevicesUnlock
              End If
            'Debug.Print "xAutoDoing", xAutoDoing
            'RemoveDevicesUnlock
            'Debug.Print "xAutoDoing", xAutoDoing
            #If XPDeviceRemoveDetect Then
              #If XPDeviceRemoveDetectPST Then
                PendingDeviceRegB = True
                PendingDeviceRegT = DoEventsEx_GetTickCount
                #Else
                Call RemovableDevices_UnregisterAll
                Call RemovableDevices_RegisterAll(MainHWnd)
                #End If
              #End If
            End If
          PlayerFormProc = True
#If 0 Then
        Case &H17& ' DBT_QUERYCHANGECONFIG
          MainForm_ctConsoleA_AddLog "DeviceChange: DBT_QUERYCHANGECONFIG"
          GoTo ForceProc
        Case &H19& ' DBT_CONFIGCHANGECANCELED
          MainForm_ctConsoleA_AddLog "DeviceChange: DBT_CONFIGCHANGECANCELED"
          GoTo ForceProc
        Case &H18& ' DBT_CONFIGCHANGED
          MainForm_ctConsoleA_AddLog "DeviceChange: DBT_CONFIGCHANGED"
          GoTo ForceProc
#End If
        Case Else
          GoTo ForceProc
        End Select
#If MainMenuCloseButton Then
    Case &H2C& ' WM_MEASUREITEM
      PlayerFormProc = MainMenuCloseButton_MeasureItem(lParam)
      Exit Function
#End If
    Case Else
ForceProc:
      If IsUnicode Then
        PlayerFormProc = DefWindowProcW(hWnd, Message, wParam, lParam)
        Else
        PlayerFormProc = DefWindowProcA(hWnd, Message, wParam, lParam)
        End If
    End Select
  End Function

Private Function BARHook(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  If IsUnicode Then
    BARHook = CallWindowProcW(BARHookOld, hWnd, Message, wParam, lParam)
    Else
    BARHook = CallWindowProcA(BARHookOld, hWnd, Message, wParam, lParam)
    End If
  
  If Message = &H111& Then
    If wParam = 0& Then
      MainForm_IconsMouseUpByHWnd lParam
      ElseIf wParam = &H10000 Then
      If lParam = xStaticIconsHWnd(xStaticIcons_TimerInd) Then
        If Not (PlayerIsLocked Or xAutoDoing) Then
          ToggleInterface Interface_Mode_Timer
          End If
        End If
      End If
    End If
  End Function

Private Function VolumeHook(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  If IsUnicode Then
    VolumeHook = CallWindowProcW(VolumeHookOld, hWnd, Message, wParam, lParam)
    Else
    VolumeHook = CallWindowProcA(VolumeHookOld, hWnd, Message, wParam, lParam)
    End If
  
  Select Case Message
    Case &H200&
      If xIsVoluming Then
        PseudoVolumeNow (lParam And &HFFFF&)
        End If
    Case &H201&
      Call SetCapture(hWnd)
      xIsVoluming = True
    Case &H202&
      If xIsVoluming Then
        PseudoVolumeNow (lParam And &HFFFF&)
        ReleaseCapture
        xIsVoluming = False
        End If
    End Select
  End Function

Private Sub PseudoVolumeNow(ByVal lParam As Long)
  Dim xInt1 As Integer
  
  Call RtlMoveMemory(ByVal VarPtr(xInt1), ByVal VarPtr(lParam), 2&)
  MainForm_ePic_MouseDown 2&, CLng(xInt1)
  End Sub

Private Function ListsHook(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  For xLong1 = 0& To 2&
    If hWnd = xListHWnd(xLong1) Then
      If xLong1 = 2& And Not InterfaceMode = Interface_Mode_None Then
        GoTo NoProcKey
        Else
        Select Case Message
          Case &H200& 'move
            If xLong1 = 2& And MainFormDraggingIs And Not (lParam = MainFormDraggingLParam) Then
              Load OLEHelperForm
              OLEHelperForm.OLEDrag
              Unload OLEHelperForm
              App.TaskVisible = False
              'ElseIf IsUnicode Then
              'Call CallWindowProcW(xListHWndHook(2), hWnd, MainFormDraggingWMsgPlus, MainFormDraggingWParam, MainFormDraggingLParam)
              'Else
              'Call CallWindowProcA(xListHWndHook(2), hWnd, MainFormDraggingWMsgPlus, MainFormDraggingWParam, MainFormDraggingLParam)
              End If
            MainFormDraggingIs = False
          Case &H201&, &H204 'down
            If xLong1 = 2& And DragOut Then
              If DragDetectWinlikePL(lParam) Then
                MainFormDraggingIs = True
                MainFormDraggingWParam = wParam
                MainFormDraggingLParam = lParam
                MainFormDraggingWMsgPlus = Message
                Exit Function
                End If
              End If
            'GoTo no
          Case &H202&, &H205& 'up
            If xLong1 = 2& And MainFormDraggingIs Then
              If IsUnicode Then
                Call CallWindowProcW(xListHWndHook(2), hWnd, MainFormDraggingWMsgPlus, MainFormDraggingWParam, MainFormDraggingLParam)
                Else
                Call CallWindowProcA(xListHWndHook(2), hWnd, MainFormDraggingWMsgPlus, MainFormDraggingWParam, MainFormDraggingLParam)
                End If
              MainFormDraggingIs = False
              End If
            If IsUnicode Then
              ListsHook = CallWindowProcW(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
              Else
              ListsHook = CallWindowProcA(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
              End If
              'Else
  '          Case &H202&, &H205&
    '          FirstDown = -1&
              Select Case xLong1
                Case 0&
                  If xDirList < 0& Then
                    ShowWindow xListHWnd(0), 0&
                    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
                    End If
                  If Message = &H202& Then
                    MainForm_xSelDirNow
                    Else
                    MainForm_CheckDir True
                    End If
                Case 1&
                  If Message = &H202& Then
                    If MainForm_xLastDirNow(False) Then
                      ShowWindow xListHWnd(1), 0&
                      DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
                      End If
                    Else
                    ShowWindow xListHWnd(1), 0&
                    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
                    MainForm_xLastDirNot
                    End If
                Case 2&
                  'If IsUnicode Then
                  '  ListsHook = CallWindowProcW(xListHWndHook(xLong1), hwnd, Message, wParam, lParam)
                  '  Else
                  '  ListsHook = CallWindowProcA(xListHWndHook(xLong1), hwnd, Message, wParam, lParam)
                  '  End If
                  If Not (PlayerIsLocked Or xAutoDoing) And Message = &H205& Then
                    'MainForm_MenuEvent TrackListBoxListHWnd(2), ((lParam And &HFFFF0000) \ &H10000), True, xMenu(5), 1&)
                    xLong1 = TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), xListHWnd(2), ((lParam And &HFFFF0000) \ &H10000), True, 0&, 1&)
                    If Not xLong1 = -1& Then
                      MainForm_CheckDir False
                      MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(5), 0&)
                      'MainForm_MenuEvent TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR),xListHWnd(2), ((lParam And &HFFFF0000) \ &H10000), True, xMenu(5), 1&)
                      End If
                    End If
                  MainForm_CheckDir False
                  'GoTo NoProcKeyX
                End Select
              GoTo NoProcKeyX
              'End If
          Case &H100&
            xLong2 = GetShiftCode
            Select Case wParam
              Case 8&, 9&
                'If Not (IsCtrlPressed Or IsAltPressed) Then
                If Not IsAltPressed Then
                  If wParam = 8& Then
                    If xDirList < 0& Then
                      MainForm_AdvanceSong 0&
                      End If
                    ElseIf xDirList > 0& Then
                    If hWnd = xListHWnd(2) Then
                      SetFocusAPI xListHWnd(0)
                      Else
                      SetFocusAPI xListHWnd(2)
                      End If
                    Else
                    If hWnd = xListHWnd(2) Then
                      MainForm_AdvanceSong xLong2 + 1& '- 2& * IsCtrlPressed
                      Else
                      SetFocusAPI xListHWnd(2)
                      End If
                    End If
                  End If
              Case 13&
                Select Case xLong1
                  Case 0&
                    If xDirList < 0& Then
                      ShowWindow xListHWnd(0), 0&
                      DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
                      End If
                    MainForm_xSelDirNow
                  Case 1&
                    If MainForm_xLastDirNow(True) Then
                      ShowWindow xListHWnd(1), 0&
                      DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
                      End If
                  Case 2&
                    MainForm_PlayListDoubleClickEvent
                  End Select
                GoTo NoProcKeyX
              Case 27&
                If (xLong1 = 1& And xDirList > 0& Or xLong1 = 2&) And Not vKioskMode Then AbortReading = True
                Select Case xLong1
                  Case 0&
                    If xDirList < 0& Then
                      ShowWindow xListHWnd(0), 0&
                      DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
                      MainForm_CheckDir True
                      End If
                  Case 1&
                    ShowWindow xListHWnd(1), 0&
                    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
                    MainForm_xLastDirNot
                  End Select
                GoTo NoProcKeyX
              Case 33& To 36&, 38&, 40&
                xFollowCurSongLastDiff = 0&
                GoTo NoProcKey
              Case 0& To 4&, 7& To 32&, 93& ' 38&, 40&, 93&
                GoTo NoProcKey
              Case 5&, 6&, 33& To 40& ', &HE7&, &HE8&  '40&
                If xLong2 = 0& Then GoTo NoProcKey
              End Select
            If Not IsAltPressed Then
              MainForm_TryFindKeyEvent (wParam And &HFF&) + xLong2 * &H100&
              Exit Function
              End If
          Case &H101& 'keyup
            If xLong1 = 2& Then
  '            If wParam = 9& And xDirList < 0& And IsCtrlPressed Then
  '              If hwnd = xListHWnd(2) Then
  '                'Debug.Print "Advance: ", xLong2 + 1& - 2& * IsCtrlPressed
  '                MainForm_AdvanceSong GetShiftCode + 1& '- 2& * IsCtrlPressed
  '                Else
  '                SetFocusAPI xListHWnd(2)
  '                End If
  '              End If
              
              
              MainForm_CheckDir False
              If Not (PlayerIsLocked Or xAutoDoing) And wParam = 93& And GetShiftCode = 0& Then
                MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(5), xListHWnd(2))
                End If
              End If
          Case &H20B&, &HAB&
            If (wParam And &H60) Then
              MainForm_TryFindKeyEvent (Hex(wParam And &H60) \ 20& + 4&) + ((wParam And &HC&) \ 4&) * &H100&
              End If
            Exit Function
  '        Case &H200&
  '          If Not FirstDown = -1& Then
  '            If DoEventsEx_IsTickCountElapsed(FirstDown, 1000&) Then
  '              MainForm_OLEDrag
  '              FirstDown = -1&
  '              End If
  '            End If
  '        Case &H201&
  '          If xLong1 = 2& Then
  '            FirstDown = DoEventsEx_GetTickCount
  '            End If
          Case &H203&
            If IsUnicode Then
              ListsHook = CallWindowProcW(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
              Else
              ListsHook = CallWindowProcA(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
              End If
            Select Case xLong1
              'Case 0&
              Case 1&
                If Not xAutoDoing Then
                  If MainForm_xLastDirNow(True) Then
                    ShowWindow xListHWnd(1), 0&
                    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
                    End If
                  End If
              Case 2&
               MainForm_PlayListDoubleClickEvent
              End Select
            GoTo NoProcKeyX
  #If ListFocusedLists Then
          Case &H7&
            xCurFocusHWnd = hWnd
  #End If
          Case &H8&
  #If ListFocusedLists Then
            xCurFocusHWnd = wParam
  #End If
            If xLong1 = 0& And xDirList < 0& Then
              ShowWindow xListHWnd(0), 0&
              DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
              ElseIf xLong1 = 1& Then
              ShowWindow xListHWnd(1), 0&
              DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
              End If
          End Select
NoProcKey:
        If IsUnicode Then
          ListsHook = CallWindowProcW(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
          Else
          ListsHook = CallWindowProcA(xListHWndHook(xLong1), hWnd, Message, wParam, lParam)
          End If
NoProcKeyX:
        End If
      Exit For
      End If
    Next
  End Function

Private Function DragDetectWinlikePL(ByVal lParam As Long) As Boolean
  Dim xLong1 As Long
  Dim X1 As Long, X2 As Long
  
  If Not PLCount = 0& Then
    xLong1 = ((lParam And &HFFFF0000) \ &H10000) \ SendMessage(xListHWnd(2), LB_GETITEMHEIGHT, 0&, 0&) + SendMessage(xListHWnd(2), LB_GETTOPINDEX, 0&, 0&)
    If xLong1 < PLCount And Not xLong1 < -1& Then
      'xLong1 = SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&)
'Print PLI(xLong1).pxWidthSm
' +28...? 16+3*4? / 16=scrollbarwidth? NO
      
      If Not SendMessage(xListHWnd(2), LB_GETSEL, xLong1, 0&) > 0& Then Exit Function
      
#If BoldDirs = 98 Then
      X1 = (PLI(xLong1).DirDepthFlags And PLI_Depth) * 20& + 24&
      X2 = X1 + PLI(xLong1).pxWidthLg
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
      X1 = (PLIDF(xLong1).DirDepthFlags And PLI_Depth) * 20& + 24&
      X2 = X1 + PLIPxLg(xLong1)
      If (PLIDF(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#End If
        If Not PLIconFolder = 0& Then
          X1 = X1 - 20&
          End If
        ElseIf Not PLIconFile = 0& Then
        X1 = X1 - 20&
        End If
      
      
      xLong1 = lParam And &HFFFF&
      'Debug.Print "Item " + Str(xLong1) + "   @  " + Str(X1) + " < " + Str(X); " < " + Str(X2)
      If xLong1 >= X1 And xLong1 <= X2 Then
        DragDetectWinlikePL = True
        End If
      End If
    End If
  
  End Function

Public Function DragDetectWinLikeSL(ByVal lParam As Long) As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim X1 As Long

  If Not (PLCount = 0& Or xSearchCount = 0&) Then
    xLong1 = ((lParam And &HFFFF0000) \ &H10000) \ SendMessage(Interface_Control(Interface_Control_Search_Results), LB_GETITEMHEIGHT, 0&, 0&) + SendMessage(Interface_Control(Interface_Control_Search_Results), LB_GETTOPINDEX, 0&, 0&)
    If xLong1 < xSearchCount And Not xLong1 < -1& Then
      'xLong1 = SendMessage(Interface_Control(Interface_Control_Search_Results), LB_GETITEMDATA, xLong1, 0&)
'Print PLI(xLong1).pxWidthSm
' +28...? 16+3*4? / 16=scrollbarwidth? NO
      
      
      xLong2 = xSearch(xLong1)
      If xLong2 < PLCount And Not xLong2 < -1& Then
      
      If Not SendMessage(Interface_Control(Interface_Control_Search_Results), LB_GETSEL, xLong1, 0&) > 0& Then Exit Function

#If BoldDirs = 98 Then
        X1 = 24&
        If (PLI(xLong2).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
        X1 = 24&
        If (PLIDF(xLong2).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#End If
          If Not PLIconFolder = 0& Then
            X1 = X1 - 20&
            End If
          ElseIf Not PLIconFile = 0& Then
          X1 = X1 - 20&
          End If
        
        
        xLong1 = lParam And &HFFFF&
        'Debug.Print "Item " + Str(xLong1) + "   @  " + Str(X1) + " < " + Str(xLong1); " < " + Str(24& + PLI(xLong2).pxWidthLg)
#If BoldDirs = 98 Then
        If xLong1 >= X1 And xLong1 <= 24& + PLI(xLong2).pxWidthLg Then
#Else
        If xLong1 >= X1 And xLong1 <= 24& + PLIPxLg(xLong2) Then
#End If
          DragDetectWinLikeSL = True
          End If
        End If
      End If
    End If
  End Function

Public Sub MainForm_PlayListDoubleClickEvent()
  If DoEventsEx_IsTickCountElapsed(xGUIClickDelayTimer, 200&) Then
    xGUIClickDelayTimer = DoEventsEx_GetTickCount
    MainForm_MenuEventPlaylist PlDClickAction
    End If
  End Sub

Private Function MainForm_TryFindKeyEvent(ByVal xKeyToFind As Long)
  Dim xLong1 As Long

  For xLong1 = 0& To KbdSplitP
    If xKey(xLong1) = xKeyToFind Then
      MainForm_ProcessKey xLong1
      Exit For
      End If
    Next
  End Function

Private Function QueryUnload()
  If Not (PlayerIsLocked Or xAutoDoing Or xTimer1 Or xTimer2) Then
  'If UnloadMode = 0 Then 'And Not vShutDownDTimer < -1 Then
    Select Case xCmdQuitMode
      Case 0& 'NoQuit
      Case 1& 'ShutDown
        'If Not KioskDown Then
          Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 2&)
        '  End If
      Case 2& 'Shell
        SHShutDownDialog MainHWnd
      Case 3& 'Standart/Quit
        QueryUnload = True
      Case 4& 'hibernate
        'If Not KioskDown Then
          Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 4&)
        '  End If
      Case 5& 'standby
        'If Not KioskDown Then
          Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 5&)
        '  End If
      End Select
    End If
  End Function

Public Function MainForm_UnloadP() As Boolean
  'Dim xBool1 As Boolean
  Dim xLong1 As Long
  
  If Not xInfoQuit Then
    MainForm_UnloadP = True
    xInfoQuit = True
    MainForm_LockMe True
    xTimer3 = True
    xTimerIsEnabled = False
    MainForm_HideIRWin
    
    MainForm_RestoreScheme
    UpdateFolderReg False
    'GetIsAutoRun
    TestRestoreReg True
    
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Killing timers and virtual window"
      #End If
    MainForm_DesireSpectrumState False, True
    If Not LEDMode = 0& Then
      Call KillTimer(MainHWnd, 0)
      LEDState = &H2& '&h1&?
      Call InfoProcKb(0&, 0&, 0&, 0&)
      End If
    MainForm_SetAutoDirs False
    If Not xFindNtfy = 0& Then
      FindCloseChangeNotification (xFindNtfy)
      xFindNtfy = 0&
      End If
    Call KillTimer(MainHWnd, 1&)
    Call KillTimer(MainHWnd, 4&)
    
    #If UseFileLogging Then
      If QuitMode = 1& Then
        MainForm_ctConsoleA_AddLog "Unloading player, preparing to shut down"
        Else
        MainForm_ctConsoleA_AddLog "Unloading player"
        End If
      #End If
    xPStateIconQuit = True
    xTimerIsEnabled = False
    EnableWindow MainHWnd, 0&
    MainForm_UpdateMainIcon
    #If Not FailSaveTest Then
      If xCmdSaveSet Then
        MainForm_SaveSettings 0&, False
        End If
      #End If
    If GlobalKeys Then
      GlobalKeys = False
      MainForm_SetGlobalKeys False
      End If
    xTimerIsEnabled = False
    If Not (xSecureMode Or vKioskMode) Then
      If xUseChmHelp Then
        Call HtmlHelp(0&, 0&, &H12&, 0&)
        Else
        Call WinHelp(MainHWnd, xRealBaseFile + "hlp", &H2&, 0&)
        End If
      End If
    DoEvents
    
    
    If Not xTrayIconIndicator Then
      ShowWindow MainHWnd, 6&
      End If
    
    'MainForm_HideIRWin
    DoEvents
    DoEvents
    If Not (InStr(CommandX, "/SAVE") = 0) Or xNoSaveListMenu = 0& And Not vKioskMode Then
      SavePList AppBase(DefaultPLExtension), False
      End If
    'xBool1 = (Not CurMusic = ctSSLayer_MusicTypeNone And xGenFading And xPStateIconPlaying And Not xPStateIconPaused)
    If Not CurMusic = ctSSLayer_MusicTypeNone Then
      'If not QuitMode=0& Then
        MainForm_xEndSong (xPStateIconPlaying And Not xPStateIconPaused), False, True
      '  Else
      '  MainForm_xEndSong (xPStateIconPlaying And Not xPStateIconPaused), False, False
      '  End If
      End If
    DoEvents
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "PreTearResults: " & Hex(AllTears_TypesCount) & ": " & Hex(AllTears_Types(0)) & "-" & Hex(AllTears_Types(1)) & "-" & Hex(AllTears_Types(2))
      #End If
    Call Tear_WaitFor(-2&)
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "PostTearResults: " & Hex(AllTears_TypesCount) & ": " & Hex(AllTears_Types(0)) & "-" & Hex(AllTears_Types(1)) & "-" & Hex(AllTears_Types(2))
      #End If
    #If UseFileLogging Then
      Dim xMyTimer1 As Long
        
      MainForm_ctConsoleA_AddLog "Pre-Freeing memory..."
      xMyTimer1 = DoEventsEx_GetTickCount
      #End If
    If Not OLEDropMemory = 0& Then
      Call LocalFree(OLEDropMemory)
      OLEDropMemory = 0&
      End If
    #If BoldDirs = 98 Then
      Erase PLI()
      #Else
      Erase PLIFile()
      Erase PLICap()
      Erase PLIDF()
      Erase PLIPxLg()
      Erase PLIPxSm()
      #End If
    Erase xDirPath()
    Erase sCharA1()
    Erase ReadDirectory_xArrayS()
'#End If
    
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  done in " + CStr(DoEventsEx_GetElapsedTime(xMyTimer1)) + " ms (maximum used index: " + CStr(PLClearCounter) + ")"
      #End If
    'Close 1
    Call ctSSLayer_Close(0&)
    Call ctSSLayer_UnLoadVB
    
    If Not vKioskMode Then
      Call RevokeDragDrop(MainHWnd)
      Set DragClass = Nothing
      End If
    
    #If FlatCastCF17 Then
      Set xFCI = Nothing
      #End If
    
    DoEvents
'    xTrayIconEnabled = False
'    xTrayIconVisible = False
'    MainForm_ShowTray True
    
    
    If QuitMode = 1& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Sending ShutDown signal"
        #End If
      ShutdownWindowsEx SDWE_Shutdown
      End If
    End If
  
  If Not SoundSchemePointerMode = 0& Then
    MainForm_RestoreScheme
    End If
  
  If Not InStr(CommandX, "/NOTASKMGR") = 0 And InStr(CommandX, "/NOTASKMGR!") = 0 Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Activating task manager"
      #End If
    On Error Resume Next
    Call SystemParametersInfoA(97&, False, ByVal 1&, 0&)
    On Error GoTo 0
    End If
  
  
  
  
  xTrayIconEnabled = False
  xTrayIconVisible = False
  MainForm_ShowTray True
  
  DestroyPlayerForm
  For xLong1 = 1& To xMenuCountX
    If Not xMenu(xLong1) = 0& Then Call DestroyMenu(xMenu(xLong1))
    If Not xPopupMenu(xLong1) = 0& Then Call DestroyMenu(xPopupMenu(xLong1))
    Next
  If Not OLEDropMemory = 0& Then
    Call LocalFree(OLEDropMemory)
    OLEDropMemory = 0&
    End If
  
  If Not LEDMode = 0& Then
    Call KillTimer(MainHWnd, 0)
    LEDState = &H1& '&h1&?
    Call InfoProcKb(0&, 0&, 0&, 0&)
    End If
  
  'If Not xMenu(0) = 0& Then
  '  Call DestroyMenu(xMenu(0))
  '  xMenu(0) = 0&
  '  End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Unbinding language file (if required)"
    #End If
  Call LoadResEx_SetLanguageFile
  Call LoadResEx_SetThemeFile

  DoEvents
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Unloading main form finished, should now terminate (Quitting finished)"
    #End If

  If IsMessageing Then
    Call PostQuitMessage(0&)
    End If
  Call ApplicationClose
  End Function

Private Sub MainForm_UnloadPlayer()
  If QueryUnload Then
    If MainForm_UnloadP Then
'
      End If
    End If
  End Sub


Public Sub MainForm_ResizePlayer(ByVal SimpleResize As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xBool1 As Boolean
  Dim xType1 As RECT
  Dim SimpleResizeNoPos As Long
  
  If SimpleResize Then
    SimpleResizeNoPos = &H2&
    End If
  
  Call GetClientRect(MainHWnd, xType1)
  'xtype1.Right=xtype1.r
  'xType1.Bottom = xType1.Bottom - xType1.Top
  
  DoEvents
  
  
  
  If IsIconic(MainHWnd) Then
    MainForm_DesireSpectrumState False, False
    If xTrayIconEnabled Then
      Call ShowWindow(MainHWnd, 0&)
      xTrayIconIndicator = True
      MainForm_ShowTray False
      End If
    Else
    'If Not IsZoomed(MainHWnd) Then
    '  If Not xPStateIconStartup Then
    '    Call GetMyWindowEx(MainHWnd, xMWindowID_Player)
    '    End If
    '  End If
    
    'If MustVeryClean Then MainForm_HideIRWin: Debug.Print "!!!!!Hidden1"
    
    MainForm_DesireSpectrumState True, False
  
    
    xLong1 = xType1.Right 'Me.ScaleWidth
    If xLong1 < 0& Then xLong1 = 0&
    xLong4 = xLong1 - ScrollComboWidth 'eImage0Width
    If xLong4 < 0& Then xLong4 = 0&
    
    xLong2 = xType1.Bottom
    If xLong2 < 0& Then xLong2 = 0&
    'Call TrDeferWindowPos(MainForm_hwnd, 0&, 0&, 0&, xType1.Right, xLong2 - ePic0Height, &H216&)
    
#If 1 Then
    If InterfaceMode = Interface_Mode_Search Or InterfaceMode = Interface_Mode_Radio Then
      xLong3 = 160&
      Else
      xLong3 = 96&
      End If
    If xLong2 < xLong3 + ePic0Height Then
      If vKioskMode Then
        xLong3 = PanelSizeDefKiosk
        Else
        xLong3 = PanelSizeDef
        End If
      'xBool1 = True
      Else
      xLong3 = ePic0Height
      'xBool1 = (xLong2 < ePic4Top + eSPTop + 48&)
      End If
    xLong2 = xLong2 - xLong3
    xBool1 = (xLong2 < ePic4Top + eSPTop)
#Else
    xLong3 = ePic0Height
    If xLong2 < ePic0Height Then
      If vKioskMode Then
        xLong3 = PanelSizeDefKiosk
        Else
        xLong3 = PanelSizeDef
        End If
      'xBool1 = True
      End If
    xLong2 = xLong2 - xLong3
    xBool1 = (xLong2 < ePic4Top + eSPTop)
#End If
    Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_BAR), 0&, 0&, xLong2, xType1.Right, xLong3, &H214&)
    'xLong2 = xLong2 - ePic0Height
    If xLong2 < 0& Then xLong2 = 0&
    xLong3 = ((xLong2 + DirAndCurSongBarHeight) * 2) \ 3
    Call TrDeferWindowPos(xListHWnd(1), 0&, 0&, xLong2 - xLong3, xLong4, xLong3, &H214&)
    Select Case InterfaceMode
      Case Interface_Mode_Search
        SearchForm_Resize xLong1, xLong2 - eSPTop2, eSPTop  '* 0.8, 25&'xLong2 * 0.1
        ' +56 -> +34
        Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - 8&, DirAndCurSongBarHeight + eSPTop + 34&, 0&, 0&, &H215&)
      Case Interface_Mode_Timer
        TimerListForm_Resize xLong1, xLong2 - eSPTop2, eSPTop  '* 0.8, 25&'xLong2 * 0.1
        Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - 8&, DirAndCurSongBarHeight + eSPTop + 34&, 0&, 0&, &H215&)
      Case Interface_Mode_Radio
        RadioForm_Resize xLong1, xLong2 - eSPTop2, eSPTop  '* 0.8, 25&'xLong2 * 0.1
        ' +56 -> +34
        Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - 8&, DirAndCurSongBarHeight + eSPTop + 34&, 0&, 0&, &H215&)
      Case Else
        If xDirList > 0& Then
          xLong2 = xLong2 - eSPTop2
          xLong3 = (xLong2 * xDirList) \ xDirListMaxDiv
          If xLong3 < 0 Then xLong3 = 0
          Call TrDeferWindowPos(xListHWnd(0), 0&, 0&, 0&, xLong1, xLong3, &H214& Or SimpleResizeNoPos)
          ePic4Top = xLong3
          Call TrDeferWindowPos(HWndSplitter, 0&, 0&, ePic4Top, xLong1, eSPTop, &H214&)
          Call TrDeferWindowPos(HWndSplitterMove, 0&, 0&, ePic4Top, xLong1, eSPTop, &H214&)
          xLong2 = xLong2 - xLong3
          If xLong2 < 0 Then xLong2 = 0
          Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - ScrollComboWidth, xLong3 + eSPTop + 12&, 0&, 0&, &H215&)
          Call TrDeferWindowPos(xListHWnd(2), 0&, 0&, xLong3 + eSPTop, xLong1, xLong2, &H214&)
          Else
          Call TrDeferWindowPos(xListHWnd(0), 0&, 0&, DirAndCurSongBarHeight, xLong4, xLong3, &H214& Or SimpleResizeNoPos)
          xLong2 = xLong2 - DirAndCurSongBarHeight - eSPTop2
          If xLong2 < 0& Then xLong2 = 0&
          ePic4Top = DirAndCurSongBarHeight
          If xBool1 Or xLong2 < 48& Then
            Call TrDeferWindowPos(HWndSplitter, 0&, 0&, -ePic4Top, xLong1, eSPTop, &H214& Or SimpleResizeNoPos)
            Call TrDeferWindowPos(HWndSplitterMove, 0&, 0&, -ePic4Top, xLong1, eSPTop, &H214& Or SimpleResizeNoPos)
            Else
            Call TrDeferWindowPos(HWndSplitter, 0&, 0&, ePic4Top, xLong1, eSPTop, &H214& Or SimpleResizeNoPos)
            Call TrDeferWindowPos(HWndSplitterMove, 0&, 0&, ePic4Top, xLong1, eSPTop, &H214& Or SimpleResizeNoPos)
            End If
          Call TrDeferWindowPos(xListHWnd(2), 0&, 0&, DirAndCurSongBarHeight + eSPTop, xLong1, xLong2, &H214& Or SimpleResizeNoPos)
          'If InterfaceMode = Interface_Mode_search Then
          '  Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - ScrollComboWidth, xLong3 + eSPTop + 12& + 20&, 0&, 0&, &H215&)
          '  Else
            Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, xLong1 - ePicXWidthX2 - 8&, DirAndCurSongBarHeight + eSPTop + 12&, 0&, 0&, &H215&)
            Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_DirList), 0&, 0&, 0&, xLong1, DirAndCurSongBarHeight, &H214& Or SimpleResizeNoPos)
          '  End If
          End If
        lPlayList_Height = xLong2
      End Select
    
    
    Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_CurSongPos), 0&, 0&, CurSongPosRealTop, xLong1, CurSongPosRealHeight, &H214& Or SimpleResizeNoPos)
    'MainForm_ePic(1).Width = xLong1
    ePic1ScaleWidth = xLong1 'MainForm_ePic(1).Width 'ScaleWidth
    If ePic1ScaleWidth = 0& Then ePic1ScaleWidth = 1&
  
    If vKioskMode And xShowSpectrum Then
      If MP3GetID3Tag Then
        xLong3 = xLong1 - hwndSpectrumWidth - eSPTop
        If xLong3 < 10& Then
          xLong3 = 10&
          End If
        hwndSpectrumLeft = xLong3 + eSPTop
        hWndTagsWidth = xLong3
        Else
        hwndSpectrumLeft = (xLong1 - hwndSpectrumWidth) / 2
        End If
      Else
      xLong3 = (xLong1 - hwndSpectrumWidth + eImage1Right) / 2
      'If xLong3 < 244& Then xLong3 = 244&
      If xLong3 < eImage1Right Then xLong3 = eImage1Right
      hwndSpectrumLeft = xLong3
      hWndTagsWidth = xLong1
      End If
  
    Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Spectrum), 0&, hwndSpectrumLeft, hwndSpectrumTop, hwndSpectrumWidth, hwndSpectrumHeight, &H215&)
    Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Tags), 0&, 0&, hWndTagsTop, hWndTagsWidth, hWndTagsHeight, &H214& Or SimpleResizeNoPos)
  
    'xLong3 = xLong1 - eButton0Width
    'MainForm_eButton(1).Left = xLong4
    If vKioskMode Then
      'MainForm_eImage(10).Left = xLong1 - 20& '- 22
      xLong1 = xLong1 - 20& '24
      Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Def3), 0&, xLong1, 0&, 0&, 0&, &H215&)
      End If
    'If MP3GetFormat Then
    '  xLong1 = xLong1 - eImage0Width
    '  If xLong1 < 0& Then xLong1 = 0&
    '  Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_CurSong), 0&, eImage0Width, 0&, xLong1, DirAndCurSongBarRealHeightOverlap, &H214&)
    '  Else
      Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&, 0&, xLong1, DirAndCurSongBarRealHeightOverlap, &H214& Or SimpleResizeNoPos)
    '  End If
    
    'bringwindowtotop(xStaticIconsHWnd(xStaticIcons_dirlist), 0&, 0&, 0&, 0&, 11&, &H214&)
    End If
'  UpdateWindow MainHWnd
  #If HowToDeferWindowPos Then
    TrEndDeferWindowPos
    #End If
  End Sub

Private Sub MainForm_IconsMouseUpByHWnd(ByVal hWnd As Long) ', ByVal lParam As Long)
  Dim Index As Long

  For Index = 0& To xStaticIcons_Last
    If xStaticIconsHWnd(Index) = hWnd Then
      Select Case Index
        Case xStaticIcons_Def0 To xStaticIcons_Def5, xStaticIcons_RepMode
          MainForm_IconsMouseUp Index
        End Select
      Exit For
      End If
    Next
  End Sub


Public Sub MainForm_IconsMouseUp(ByVal Index As Long)
  If Not (PlayerIsLocked Or xAutoDoing) Then
    xGUIClickDelayTimer = DoEventsEx_GetTickCount
    If (Index = xStaticIcons_Def0 Or Index = xStaticIcons_Def4 Or Index = xStaticIcons_Def5 And Not xRemovePlayedSongs) And xPStateIconPlaying And xRemoveSkippedSongs And Not (xNoRemove Or PLCount = 0& Or Not PlNextSong = "" And PLNext = PLCurItem) Then
      MainForm_RemoverX (Index = xStaticIcons_Def0)
      End If
    Select Case Index
      Case xStaticIcons_Def0 'previous
        MainForm_PlayNext True, -1&, False, False
      Case xStaticIcons_Def1 'play
        If xPStateIconPlaying And xPStateIconPaused Then
          MainForm_xPauseClick
          Else
          xPStateIconPlaying = True
          MainForm_xPlayClick False
          End If
      Case xStaticIcons_Def2 'pause
        MainForm_xPauseClick
      Case xStaticIcons_Def3 'stop
        If vKioskMode Then
          If Not PlNextSong = "" Then SetNextSong False
          Else
          If PlNextSong = "" Then
            If xPStateIconPlaying Then
              xPStateIconPlaying = False
              MainForm_LockMe True
              MainForm_xEndSong Not xPStateIconPaused, False, False
              MainForm_UpdateMainIcon
              MainForm_LockMe False
              End If
            xReAutoPlay = False
            ReAutoPlayDR = False
            Else
            SetNextSong False
            End If
          End If
      Case xStaticIcons_Def4 'next
        MainForm_PlayNext True, 1&, False, False
      Case xStaticIcons_Def5 'endsong
        If xPStateIconPlaying Then
          If xPStateIconPaused Then
            xPStateIconPlaying = False
            MainForm_UpdateMainIcon
            xPStateIconPaused = False
            xReAutoPlay = False
            ReAutoPlayDR = False
            MainForm_CheckAfter False, True, True, False
            Else
            MainForm_CheckAfter True, True, True, False
            End If
          End If
      Case xStaticIcons_RepMode
        xRep = (xRep + 1&) Mod 3&
        MainForm_ApplyRepMode
      End Select
    End If
  End Sub

Public Sub SaveOpenURL(ByVal xURL As String)
  Dim xLong1 As Long
  
  If Not Len(xURL) = 0& And UrlsAllowed Then
    xLong1 = InStr(xURL, "://")
    If xLong1 = 0& Then
      xURL = "http://" + xURL
      GoTo UrlOk
      Else
      Select Case LCase(Left(xURL, xLong1 - 1&))
        Case "http", "https", "ftp"
UrlOk:
          If ShellExecute(MainHWnd, "Open", xURL, "", "", 5&) = 0& Then
            SetUnicodeClipboard xURL
            End If
        End Select
      End If
    End If
  End Sub

Private Sub SetUnicodeClipboard(ByVal NewSongName As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  
  If OpenClipboard(MainHWnd) Then
    NewSongName = NewSongName + vbNullChar '+ vbNullChar
    xLong3 = Len(NewSongName)

    xLong1 = GlobalAlloc(&H2&, xLong3)
    If Not xLong1 = 0& Then
      xLong2 = GlobalLock(xLong1)
      If xLong2 Then
        Call RtlMoveMemory(ByVal xLong2, ByVal NewSongName, xLong3)
        Call GlobalUnlock(xLong1)
        Call EmptyClipboard
        Call SetClipboardData(1&, xLong1)
        Else
        'Call GlobalUnlock(xLong1)
        Call GlobalFree(xLong1)
        End If
      End If
    
    xLong3 = xLong3 * 2&
    xLong1 = GlobalAlloc(&H2&, xLong3)
    If xLong1 Then
      xLong2 = GlobalLock(xLong1)
      If xLong2 Then
        Call RtlMoveMemory(ByVal xLong2, ByVal StrPtr(NewSongName), xLong3)
        Call GlobalUnlock(xLong1)
        Call EmptyClipboard
        Call SetClipboardData(13&, xLong1)
        Else
        'Call GlobalUnlock(xLong1)
        Call GlobalFree(xLong1)
        End If
      End If
    
    
    CloseClipboard
    End If
  End Sub

Private Sub MainForm_SetDirCaption(ByVal xChar1 As String)
  DirLblCaption = xChar1
  'MainForm_EraseBackground xStaticIconsHWnd(xStaticIcons_DirList)
'  RewriteDirlist
  If InterfaceMode = Interface_Mode_None Then
    DrawWhiteDC xStaticIconsHWnd(xStaticIcons_DirList), xListHWnd(0), 1&, 1&
    End If
'  If Len(DirLblCaption) = 0& Then Stop
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "DirCap: " + DirLblCaption
    #End If
  
  'Call SendMessage(xStaticIconsHWnd(xStaticIcons_DirList), &H14&, 0&, 0&)
  'Call SendMessage(xStaticIconsHWnd(xStaticIcons_DirList), &HF&, 0&, 0&)
  End Sub

Private Function RedrawProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  'Dim xLong1 As Long
  'Dim xType1 As RECT
  'Dim xType2 As POINTAPI
  
  Select Case Message
    Case &H14&
      Select Case hWnd
        Case xStaticIconsHWnd(xStaticIcons_DirList)
          'MainForm_EraseBackground hwnd
          RedrawProc = 1&
          Exit Function
        Case xStaticIconsHWnd(xStaticIcons_CurSong)
          'MainForm_EraseBackground hwnd
          RedrawProc = 1&
          Exit Function
        Case xStaticIconsHWnd(xStaticIcons_CurSongPos)
          'MainForm_EraseBackground hwnd
          RedrawProc = 1&
          Exit Function
        End Select
    Case &HF& ', &H14& 'WM_PAINT = &HF
      'If IsUnicode Then
      '  RedrawProc = DefWindowProcW(hwnd, Message, wParam, lParam)
      '  Else
      '  RedrawProc = DefWindowProcA(hwnd, Message, wParam, lParam)
      '  End If
'      Exit Function '???
      
      Select Case hWnd
        Case xStaticIconsHWnd(xStaticIcons_DirList)
          'If InterfaceMode = Interface_Mode_none Then
            DrawWhiteDC xStaticIconsHWnd(xStaticIcons_DirList), xListHWnd(0), 1&, True  ', DirLblCaption
          '  End If
        Case xStaticIconsHWnd(xStaticIcons_CurSong)
          'If Not CurMusic = ctSSLayer_MusicTypeNone Then
            DrawWhiteDC xStaticIconsHWnd(xStaticIcons_CurSong), xListHWnd(1), 0&, True
          '  End If
#If ProcCtl Then
#Else
        Case xStaticIconsHWnd(xStaticIcons_CurSongPos)
          'If Not (CurMusic = ctSSLayer_MusicTypeNone Or PLCurSongLength = 0&) Then
            MainForm_RedrawPosition
          '  End If
#End If
        Case xStaticIconsHWnd(xStaticIcons_Anim)
          DoAnimateReadDir 0&
        Case xStaticIconsHWnd(xStaticIcons_Spectrum)
          If xCanSpectrum Then
            Call InfoProc(0&, 0&, 0&, 0&)
            End If
        Case xStaticIconsHWnd(xStaticIcons_ToolTip)
          If ToolTipInitTime = -1& And Not CurShownToolTip = -1& Then WriteToolTip
        End Select
'    Case &H200&
'      'mousemove -> volume
'      'debug.
'      If wParam = &H1& And hwnd = xStaticIconsHWnd(xStaticIcons_Volume) Then
'        Debug.Print "Volume:", lParam And &HFFFF&
'        End If
    Case &H202&
      If IsUnicode Then
        RedrawProc = DefWindowProcW(hWnd, Message, wParam, lParam)
        Else
        RedrawProc = DefWindowProcA(hWnd, Message, wParam, lParam)
        End If

'      Debug.Print "Mouse:", wParam
'      If wParam = &H1& Then
'      IconsMouseUpByHWnd hwnd, lParam
        Select Case hWnd
          Case xStaticIconsHWnd(xStaticIcons_DirList)
            If InterfaceMode = Interface_Mode_None Then
              If xDirList < 0& Then
                If IsWindowVisible(xListHWnd(0)) = 0& Then
                  ShowWindow xListHWnd(0), 5&
                  DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 1&
                  SetFocusAPI xListHWnd(0)
                  Else
                  ShowWindow xListHWnd(0), 0&
                  DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
                  End If
                End If
              End If
          Case xStaticIconsHWnd(xStaticIcons_CurSong)
            If IsWindowVisible(xListHWnd(1)) = 0& Then
              ShowWindow xListHWnd(1), 5&
              DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 1&
              SetFocusAPI xListHWnd(1)
              Else
              ShowWindow xListHWnd(1), 0&
              DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
              End If
          Case xStaticIconsHWnd(xStaticIcons_CurSongPos)
            MainForm_ePic_MouseDown 1&, (lParam And &HFFFF&)
          End Select
'        End If
      Exit Function
    Case &H203&
      If hWnd = xStaticIconsHWnd(xStaticIcons_Spectrum) And Not vKioskMode Then
        xDeluxeSpectrum = Not xDeluxeSpectrum
        End If
      Case &HD& ' WM_GETTEXT
        RedrawProc = FastCopy(hWnd, wParam, lParam)
        Exit Function
      Case &HE& ' WM_GETTEXTLENGTH = &HE
        RedrawProc = FastCopy(hWnd, wParam, ptrTemp)
        Exit Function
    Case &HA1&
      If hWnd = xStaticIconsHWnd(xStaticIcons_CurSongPos) Then
        ExtraSongPos lParam
        End If
    End Select
  If IsUnicode Then
    RedrawProc = DefWindowProcW(hWnd, Message, wParam, lParam)
    Else
    RedrawProc = DefWindowProcA(hWnd, Message, wParam, lParam)
    End If
  End Function

Private Sub ExtraSongPos(ByVal lParam As Long)
  Static xType1 As POINTAPI
  
  xType1.x = 0
  xType1.y = 0
  Call ClientToScreen(xStaticIconsHWnd(xStaticIcons_CurSongPos), xType1)
  MainForm_ePic_MouseDown 100&, (lParam And &HFFFF&) - xType1.x
  End Sub

Private Sub DrawWhiteDC(ByVal hWnd As Long, ByVal HWndDownCtrl As Long, ByVal ID As Long, ByVal xEraseBackgrnd As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType1 As RECT
  Dim xType2 As RECT
  
  If Not GetClientRect(hWnd, xType1) = 0& Then
    xLong1 = GetDC(hWnd)
    If Not xLong1 = 0& Then
      'If ID = 3& Then
      '  If Not Interface_Icon = Interface_Mode_none Then
      '    Call DrawIconEx(xLong1, 4&, 0&, Interface_Icon, 16&, 16&, 0&, 0&, 3&)
      '    End If
      '  ID = 1&
      '  Else
        xType2.Bottom = xType1.Bottom
        xLong2 = SelectObject(xLong1, hFontGFix)
        Call SetBkColor(xLong1, FLineColorX) ' GetSysColor(5&))
        Call SetTextColor(xLong1, FLineColorWDCTxt)
        xType2.Right = xType1.Right - ScrollComboWidth
        If xEraseBackgrnd Then
          Call FillRect(xLong1, xType2, FillSysColorBack)
          End If
        If ID = 0& Then
          If Not CurMusic = ctSSLayer_MusicTypeNone Then
            xType2.Right = xType2.Right - PLCurSongDisplaySpacePos
            xType2.Left = PLCurSongDisplaySpacePos
            xType2.Bottom = xType2.Bottom - DirAndCurSongBarRealOverlap
            Call DrawText(xLong1, PLCurSongDisplaySpaceStr, xType2, &H8924&) ' + &H4000 * CLng(ShowFullPath))
            If MP3GetFormat And Not MP3FormatBitmap = 0& Then
              xLong2 = SelectObject(xSharedDC, MP3FormatBitmap)
              If Not xLong2 = 0& Then
                Call BitBlt(xLong1, 4&, 0&, 16&, 16&, xSharedDC, 0&, 0&, vbSrcCopy)
                Call SelectObject(xSharedDC, xLong2)
                End If
              End If
            End If
          ElseIf ID = 1& And Not Len(DirLblCaption) = 0& Then
          'Debug.Print "RePrintDir: " + DirLblCaption
          If PLIconFolder = 0& Then
            Call DrawText(xLong1, DirLblCaption, xType2, &H4924&) '&H8924& + &H4000 * CLng(ShowFullPath))
            Else
            Call DrawIconEx(xLong1, 4&, 0&, PLIconFolder, 16&, 16&, 0&, 0&, 3&)
            'xType2.Right = xType2.Right - 20&
            xType2.Left = 20&
            Call DrawText(xLong1, DirLblCaption, xType2, &H4924&) '&H8924& + &H4000 * CLng(ShowFullPath))
            End If
          End If
'        Call SetBkColor(xSharedDC, FLineColorX)
        Call SelectObject(xLong1, xLong2)
        'End If
  
      xType1.Left = xType1.Right - ScrollComboWidth '3&
      If hThemeComboDropButtons = 0& Then
        If IsWindowVisible(HWndDownCtrl) = 0& Then
          Call DrawFrameControl(xLong1, xType1, 3&, ID) '(ID And &H1&))
          Else
          Call DrawFrameControl(xLong1, xType1, 3&, &H200& + ID) '(ID And &H1&))
          End If
        Else
        'If IsWindowVisible(HWndDownCtrl) Then
        '  xLong2 = (ID And &H1&) * 4& + 3&
        '  Else
        '  xLong2 = (ID And &H1&) * 4& + 1&
        '  End If
        'xLong2 = (ID And &H1&) * 4& + 1& - 2& * CLng(IsWindowVisible(HWndDownCtrl))
        xLong2 = ID * 4& + 1& + 2& * IsWindowVisible(HWndDownCtrl)
        If Not IsThemeBackgroundPartiallyTransparent(hThemeComboDropButtons, 1&, xLong2) = 0& Then
          Call DrawThemeParentBackground(hWnd, xLong1, xType1)
          End If
        Call DrawThemeBackground(hThemeComboDropButtons, xLong1, 1&, xLong2, xType1, 0&)
        End If
      Call ReleaseDC(hWnd, xLong1)
      End If
    End If
  End Sub

Private Sub DrawWhiteDCButton(ByVal hWnd As Long, ByVal IDUpOrDown As Long, ByVal IsDown As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType1 As RECT
  
  If Not GetClientRect(hWnd, xType1) = 0& Then
    xLong1 = GetDC(hWnd)
    If Not xLong1 = 0& Then
      xType1.Left = xType1.Right - ScrollComboWidth '3&
      If hThemeComboDropButtons = 0& Then
        Call DrawFrameControl(xLong1, xType1, 3&, IsDown * &H200& + IDUpOrDown) '(ID And &H1&))
        Else
        'xLong2 = (ID And &H1&) * 4& + 1& + IsDown * 2&
        xLong2 = IDUpOrDown * 4& + 1& + IsDown * 2&
        If Not IsThemeBackgroundPartiallyTransparent(hThemeComboDropButtons, 1&, xLong2) = 0& Then
          Call DrawThemeParentBackground(hWnd, xLong1, xType1)
          End If
        Call DrawThemeBackground(hThemeComboDropButtons, xLong1, 1&, xLong2, xType1, 0&)
        End If
      Call ReleaseDC(hWnd, xLong1)
      End If
    End If
  End Sub

Private Sub MainForm_EraseBackground(ByVal hWnd As Long)
  Dim xLong1 As Long
  Dim xType1 As RECT
  
'  If hwnd = xStaticIconsHWnd(xStaticIcons_DirList) Then
'    Debug.Print "ClearHWnd"
'    End If
  xLong1 = GetDC(hWnd)
  If Not xLong1 = 0& Then
    Call GetClientRect(hWnd, xType1)
    'xType1.Right = xType1.Right - 16&
    Call FillRect(xLong1, xType1, FillSysColorBack)
    Call ReleaseDC(hWnd, xLong1)
    End If
  End Sub

Private Sub MainForm_RedrawPosition()
#If ProcCtl Then
  If Not (CurMusic = ctSSLayer_MusicTypeNone Or PLCurSongLength = 0&) Then
    Call SendMessage(xStaticIconsHWnd(xStaticIcons_CurSongPos), 1026&, CLng(PLCurSongPos * 100& / PLCurSongLength), 0&)
    End If
#Else
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType1 As RECT
  
  xLong2 = GetDC(xStaticIconsHWnd(xStaticIcons_CurSongPos))
  If Not xLong2 = 0& Then
    Call GetClientRect(xStaticIconsHWnd(xStaticIcons_CurSongPos), xType1)
    If Not (CurMusic = ctSSLayer_MusicTypeNone Or PLCurSongLength = 0&) Then
      xLong1 = xType1.Right
      xType1.Right = PLCurSongPos / PLCurSongLength * (xLong1 - ePic1ScaleBonus)
      Call FillRect(xLong2, xType1, 14&)
      xType1.Left = xType1.Right
      xType1.Right = xLong1
      End If
    'ePic(1).Line (0&, 0&)-(xLong1, 10&), FLineColor, BF
    'ePic(1).Line (xLong1 + 1&, 0&)-(ePic1ScaleWidth, 10&), FLineColorX, BF
    Call FillRect(xLong2, xType1, 6&)
'      End If
    Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_CurSongPos), xLong2)
    End If
#End If
  End Sub

Private Sub MainForm_DrawVolFaderA(ByRef xVolume As Long, ByRef SpecialAction As Long)
  Static FaderPos As Long
  Dim xBmp As Long
  Dim xBmpOld As Long
  Dim xhPen As Long
  Dim xhPen2 As Long
  Dim xhPenOld As Long
  Dim xType1 As RECT

  If SpecialAction = 1& Then
    xVolume = xVolume - 4
    If xVolume < 0& Then
      xVolume = 0&
      ElseIf xVolume > 31& Then
      xVolume = 31
      End If
    xVolume = (xVolume / 31&) * 255&
    Else
    xBmp = CreateCompatibleBitmapX(40&, 16&)
    If Not xBmp = 0& Then
      xBmpOld = SelectObject(xSharedDC, xBmp)
      xType1.Right = 40&
      xType1.Bottom = 16&
      Call FillRect(xSharedDC, xType1, FillSysColorFace)
      
      FaderPos = CLng(32 * CSng(xVolume) / 255)
      Call MoveToEx(xSharedDC, 0&, 11&, 0&)
      
      xhPen = CreatePen(0&, 0&, FormatIconBackSunkenD)
      If Not xhPen = 0& Then
        xhPenOld = SelectObject(xSharedDC, xhPen)
        Call LineTo(xSharedDC, 38&, 3&)
        
        Call SetPixel(xSharedDC, 38&, 3&, FormatIconBackSunkenD)
        Call MoveToEx(xSharedDC, 0&, 12&, 0&)
        xhPen2 = CreatePen(0&, 0&, FormatIconBackSunkenW)
        If Not xhPen2 = 0& Then
          Call SelectObject(xSharedDC, xhPen2)
          Call LineTo(xSharedDC, 40&, 12&)
          Call MoveToEx(xSharedDC, 39&, 3&, 0&)
          Call LineTo(xSharedDC, 39&, 12&)
          Call SelectObject(xSharedDC, xhPen)
          Call DeleteObject(xhPen2)
          End If
        xType1.Left = FaderPos
        xType1.Right = xType1.Left + 8&
        Call FillRect(xSharedDC, xType1, FillSysColorSunkenW)
        xType1.Left = xType1.Left + 1&
        xType1.Top = 1&
        
        'Call SelectObject(xshareddc, xhPen)
        Call FillRect(xSharedDC, xType1, FillSysColorSunkenD)
        Call SelectObject(xSharedDC, xhPenOld)
        Call DeleteObject(xhPen)
        End If
      xType1.Right = xType1.Right - 1&
      xType1.Bottom = 15&
      Call FillRect(xSharedDC, xType1, FillSysColorFace)
      Call SelectObject(xSharedDC, xBmpOld)
      End If
    If Not xBmp = 0& Then
      SendMessage xStaticIconsHWnd(xStaticIcons_Volume), &H172&, 0&, xBmp
      If Not xStaticIconsBMP(xStaticIcons_Volume) = 0& Then
        Call DeleteObject(xStaticIconsBMP(xStaticIcons_Volume))
        End If
      xStaticIconsBMP(xStaticIcons_Volume) = xBmp
      End If
    End If
  End Sub

Private Function SplitterProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  If Not PlayerIsLocked Then
    Select Case Message
      Case &H200& 'private Const WM_MOUSEMOVE = &H200
        If MainFormIsSplitterMoving Then SplitterMove (lParam And &HFFFF0000) \ &H10000
      Case &H20&
        Call SetCursor(NSCursor)
        Exit Function
      Case &H201& 'private Const WM_LBUTTONDOWN = &H201
        If Not MainFormIsSplitterMoving Then
          MainFormIsSplitterMoving = True
          Call SetCapture(HWndSplitter)
          Call SetWindowPos(HWndSplitterMove, 0&, 0&, 0&, 0&, 0&, &H213&)
          ShowWindow HWndSplitterMove, 5&
          End If
      Case &H202& 'private Const WM_LBUTTONUP = &H202
        If MainFormIsSplitterMoving Then
          SplitterMove (lParam And &HFFFF0000) \ &H10000
          ReleaseCapture
          Call SetWindowPos(HWndSplitterMove, 1&, 0&, 0&, 0&, 0&, &H213&)
          ShowWindow HWndSplitterMove, 0&
          If SplitterPreResult = -1& Then
            If xDirList > 0& Then
              xDirList = -xDirList
              MainForm_ToggleDirList True
              End If
            ElseIf xDirList < 0& Then
            xDirList = SplitterPreResult
            MainForm_ToggleDirList True
            Else
            xDirList = SplitterPreResult
            MainForm_ResizePlayer False
            End If
          'SplitterDragUp (lParam And &HFFFF0000) \ &H10000
          MainFormIsSplitterMoving = False
          End If
      End Select
    End If
  If IsUnicode Then
    SplitterProc = DefWindowProcW(hWnd, Message, wParam, lParam)
    Else
    SplitterProc = DefWindowProcA(hWnd, Message, wParam, lParam)
    End If
  End Function

Private Sub SplitterMove(ByVal y As Long)
  'Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType1 As RECT
  
  y = y + ePic4Top
  Call GetClientRect(MainHWnd, xType1)
  xLong2 = xType1.Bottom - ePic0Height - eSPTop2
  If xLong2 < 1& Then xLong2 = 1&
  If y > DirAndCurSongBarHeight Then
    SplitterPreResult = CInt(y * xDirListMaxDiv / xLong2)
    If SplitterPreResult = 0& Then SplitterPreResult = -1&
    Else
    SplitterPreResult = -1&
    End If
  If SplitterPreResult > 0& Then
    If SplitterPreResult > xDirListMax Then SplitterPreResult = xDirListMax
    xLong2 = (xLong2 * SplitterPreResult) \ xDirListMaxDiv
    If xLong2 < 0& Then xLong2 = 0&
    Else
    xLong2 = DirAndCurSongBarHeight
    End If
  Call SetWindowPos(HWndSplitterMove, 0&, 0&, xLong2, 0&, 0&, &H215&)
  End Sub

Private Function NoProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  If IsUnicode Then
    NoProc = DefWindowProcW(hWnd, Message, wParam, lParam)
    Else
    NoProc = DefWindowProcA(hWnd, Message, wParam, lParam)
    End If
  End Function


































Private Sub MainForm_xPauseClick()
If Not (PlayerIsLocked Or xAutoDoing) Then
'If PlayerIsLocked Or xAutoDoing Then Exit Sub
  If Not xPStateIconPlaying Then
    If xPStateIconPaused Then
      xPStateIconPaused = False
      MainForm_UpdateMainIcon
      End If
    Exit Sub
    End If
  MainForm_LockMe True
  If xPStateIconPaused Then
    xPStateIconPaused = False
    MainForm_UpdateMainIcon
    'DoVolume 0, False, True
    ForceLoudestTear = CurrentSongID
    ShowIRI
    If xGenFading Or Not Tear_StepT(CurrentSongID) = 0& Then
      If Tear_StepT(CurrentSongID) = 0& Then
        Tear_Volume(CurrentSongID) = 0&
        Call ctSSLayer_SetVolume(SongPointer(CurrentSongID), 0&)
        Call ctSSLayer_SetPause(SongPointer(CurrentSongID), False)
        End If
      Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 12&, 0&, SimpleFadingTotalTime, 1&)
      Else
      Call ctSSLayer_SetVolume(SongPointer(CurrentSongID), xSetVolume)
      Call ctSSLayer_SetPause(SongPointer(CurrentSongID), False)
      Tear_Volume(CurrentSongID) = 1&
      'ShowIR PLCurSongDisplay
      'DoVolume xSetVolume, False, True 'False
      End If
    'DoVolume xSetVolume, False, True 'False
    xEndSongTimerEventTime = DoEventsEx_GetTickCount
    LastPosTime = xEndSongTimerEventTime
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Sound unpaused"
      #End If
    Else
    xEndSongTimerEventTime = -1&
    xPStateIconPaused = True
    MainForm_UpdateMainIcon
    
    If xGenFading Or Not Tear_StepT(CurrentSongID) = 0& Then
      If Tear_StepT(CurrentSongID) = 0& Then Tear_Volume(CurrentSongID) = 1&
      Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 2&, 0&, SimpleFadingTotalTime, 0&)
    'If xGenFading Then DoFading -1&
      Else
      Call ctSSLayer_SetPause(SongPointer(CurrentSongID), True)
      Tear_Volume(CurrentSongID) = 0&
      End If
    
    'DoVolume 0, False, True
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Sound paused"
      #End If
    End If
  MainForm_LockMe False
  End If
End Sub

Private Sub MainForm_ePic_MouseDown(ByVal Index As Long, ByVal x As Long)
  Dim xLong1 As Long

  If Not PlayerIsLocked Then
    Select Case Index
      Case 100&
        If Not (xSongIsMidi = 3& Or PLCurSongLength = 0&) Then
          xLong1 = (x + ePic1ScaleBonus) / ePic1ScaleWidth * PLCurSongLength
          If Not (vKioskMode Or PlayerIsLocked Or CurMusic = ctSSLayer_MusicTypeNone Or xLong1 < 0& Or xLong1 > PLCurSongLength) Then
            Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), xLong1)
            If CurMusic = ctSSLayer_MusicTypeTracker Then
              ResetPatternLog
              'xModCurPattern = -1&
              'xModPatterns = ""
              End If
            End If
          End If
      Case 1&
        'Exit Sub
        If Not (xSongIsMidi = 3& Or PLCurSongLength = 0&) Then
          xLong1 = (x + ePic1ScaleBonus) / ePic1ScaleWidth * PLCurSongLength
          If Not (vKioskMode Or PlayerIsLocked Or CurMusic = ctSSLayer_MusicTypeNone Or xLong1 < 0& Or xLong1 > PLCurSongLength) Then
            Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), xLong1)
            If CurMusic = ctSSLayer_MusicTypeTracker Then
              ResetPatternLog
              End If
            End If
          End If
      Case 2&
        If Not (vKioskMode Or PlayerIsLocked) Then
          xLong1 = CLng(x) '- VolCtrlPos
          MainForm_DrawVolFaderA xLong1, 1&
          DoVolume xLong1, True
          End If
      End Select
    End If
  End Sub

'Private Sub ePic_MouseMove(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
'Static xLong1 As Long
'
'If Index = 2 And Button = 1 And Not PlayerIsLocked Then
'  xLong1 = CLng(x) '- VolCtrlPos
'  MainForm_DrawVolFaderA xLong1, 1&
'  DoVolume xLong1, True, True
'  End If
'End Sub

'private Sub MainForm_Form_KeyDown(KeyCode As Integer, Shift As Integer)
'  Static xLong1 As Long
'  Static xLong2 As Long
'
'  xLong1 = CLng(Shift)
'  If (xLong1 And &HFFFFFFFC) = 0& Then
'    xLong2 = (CLng(KeyCode) And &HFF&) + (xLong1 And &H3&) * &H100&
'    For xLong1 = 0& To KbdSplitP
'      If xKey(xLong1) = xLong2 Then
'        ProcessKey xLong1
'        KeyCode = 0&
'        Exit For
'        End If
'      Next
'    End If
'  End Sub

#If UseFileLogging Then
  Private Function Debug_SIntToDouble(ByVal xLong1 As Long) As Double
    If (xLong1 And &H80000000) Then
      Debug_SIntToDouble = 4294967296# + CDbl(xLong1)
      Else
      Debug_SIntToDouble = CDbl(xLong1)
      End If
    End Function
  
  Private Function DebugDoubleMemStr(ByVal xDbl1 As Double) As String
    xDbl1 = Fix(xDbl1 / 1024)
      If xDbl1 > 1024# * 1024# * 1024# Then
      DebugDoubleMemStr = CStr(Fix(xDbl1 * 100 / 1024 / 1024 / 1024) / 100) + " TB"
      ElseIf xDbl1 > 1024# * 1024# Then
      DebugDoubleMemStr = CStr(Fix(xDbl1 * 100 / 1024 / 1024) / 100) + " GB"
      ElseIf xDbl1 > 1024# Then
      DebugDoubleMemStr = CStr(Fix(xDbl1 * 100 / 1024) / 100) + " MB"
      Else
      DebugDoubleMemStr = CStr(xDbl1) + " KB"
      End If
    End Function
  #End If

Private Sub GetOptimalPLMaxCount()
  Dim xType1 As MEMORYSTATUSEX
  Dim xType2 As MEMORYSTATUS
  
  #If UseFileLogging Then
    Static ProcessedOnce As Long
    #End If
  
  If IsUnicode Then
    On Error GoTo Er1
    PLCountMax = GlobalMemoryStatusEx(xType1)
Rs1:
    On Error GoTo 0
    If PLCountMax = 0& Then
      Call GlobalMemoryStatus(xType2)
      If xType2.dwTotalPhys And &H80000000 Then
        PLCountMax = &H7FFFFFFF
        Else
        PLCountMax = xType2.dwTotalPhys
        End If
      #If UseFileLogging Then
        'If LogFileDoLog Then
          MainForm_ctConsoleA_AddLog "  32Limit; Total Physical: " + DebugDoubleMemStr(Debug_SIntToDouble(xType2.dwTotalPhys))
          'End If
        #End If
      Else
      If xType1.ullAvailPhysHigh = 0& And Not (xType1.ullAvailPhysLow And &H80000000) Then
        PLCountMax = xType1.ullAvailPhysLow
        Else
        PLCountMax = &H7FFFFFFF
        End If
      #If UseFileLogging Then
        'If LogFileDoLog Then
          MainForm_ctConsoleA_AddLog "  Total Physical: " + DebugDoubleMemStr(Debug_SIntToDouble(xType1.ullAvailPhysHigh) * 4294967296# + Debug_SIntToDouble(xType1.ullAvailPhysLow))
          'End If
        #End If
      End If
    
    
    #If UseFileLogging Then
      'If LogFileDoLog Then
        MainForm_ctConsoleA_AddLog "  Considered / max 2GB: " + DebugDoubleMemStr(Debug_SIntToDouble(PLCountMax))
        'End If
      #End If
    PLCountMax = PLCountMax / 256& / PLISize 'len PLI
    End If
    
  If PLCountMax < PLCountMax95 Then
    PLCountMax = PLCountMax95
    End If
  #If UseFileLogging Then
    'If LogFileDoLog Then
      If IsUnicode Then
        MainForm_ctConsoleA_AddLog "  Real array size: " + CStr(PLCountMax)
        Else
        MainForm_ctConsoleA_AddLog "Windows 9x, fixed array size (" + CStr(PLCountMax) + ")"
        End If
      'End If
    #End If
  
  #If UseFileLogging Then
    If ProcessedOnce = 0& Then
      ProcessedOnce = PLCountMax
      Else
      PLCountMax = ProcessedOnce
      Exit Sub
      End If
    #End If
  
  PLCountWarn = PLCountMax * 0.925
  Exit Sub
Er1:
  PLCountMax = 0&
  Resume Rs1
  End Sub

Private Sub MainForm_StartPlayer()
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  Dim xChar4 As String
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim hFile As Long
  Static xBool1 As Boolean
  Dim xBool2 As Boolean
  Dim xBool3 As Boolean
  Dim xBool4_HasBase As Boolean
  Dim xType2 As STARTUPINFO
  Dim CommandZ As String
  Dim RestoreRegOnlyNow As Long
  'Dim xType1 As OSVERSIONINFO
  #If UseFileLogging Then
    Dim GBN1 As String
    #End If

  Call ApplicationStart(LoadResEx_IconOS_WinVista)
  'On Error Resume Next
  'Call SetProcessDEPPolicy(0&)
  'On Error GoTo 0
  
  GetOptimalPLMaxCount
  PLCountMaxX = PLCountMax - 1&
  PLCountMaxX2 = PLCountMax - 2&
  
  #If BoldDirs = 98 Then
    ReDim PLI(PLCountMaxX)
    #Else
    ReDim PLIFile(PLCountMaxX)
    ReDim PLICap(PLCountMaxX)
    ReDim PLIDF(PLCountMaxX)
    ReDim PLIPxLg(PLCountMaxX)
    ReDim PLIPxSm(PLCountMaxX)
    #End If
  
  
  
  RndPLPatternCount = (PLCountMaxX \ RandomizerPattern) + 1&
  ReDim RndPLPattern(RndPLPatternCount)
  sCharA1Max = PLCountMax ' MaxInt
  sCharA1MaxX = sCharA1Max - 1&
  ReDim sCharA1(sCharA1MaxX)
  ReDim xDirPath(PLCountMaxX2)
  
  
  
  
  
  xRealBaseFile = AppBase("")
  CommandZ = GetStrippedCommandLine
  CommandX = UCase(CommandZ)
  GBN2 = GetFileVersionField(GetFileVersionField_ProductName) + " " + GetFileVersionField(GetFileVersionField_ProductVersion) + vbCr + Replace(GetFileVersionField(GetFileVersionField_LegalCopyright), "; ", vbCrLf)
  #If UseFileLogging Then
    GBN1 = GetFileVersionField(GetFileVersionField_ProductName) + " " + GetFileVersionField(GetFileVersionField_ProductVersion)
    #End If

  If Len(Trim(CommandX)) = 0& Then
    hFile = TxtFile_ReadOpen(xRealBaseFile + "cmd")
    If Not hFile = 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Reading command file (" & xRealBaseFile + "cmd" & ")"
        #End If
      Do While TxtFile_ReadLine(hFile, xChar1)
        CommandX = Trim(CommandX) & " " & UCase(Trim(xChar1))
        CommandZ = CommandZ & " " & Trim(xChar1)
        Loop
#If UseFileLogging Then
      Call TxtFile_ReadClose(hFile)
      Else
      MainForm_ctConsoleA_AddLog "Error reading command file (" + xRealBaseFile + "cmd" + ")"
#End If
      End If
    End If

  xLong1 = InStr(CommandX, "/BASE=")
  If Not xLong1 = 0& Then
    xChar1 = Trim(Mid(CommandZ, xLong1 + 6))
    If Not Len(xChar1) = 0& Then
      AppBase Chr(2) & GetFullPathName(xChar1) & ".exe"
      xLong1 = GetFileAttributes(SimpleDir(AppBase("exe")))
      If (xLong1 And 16&) And Not xLong1 = -1& Then
        xBool4_HasBase = True
        'AppBase Chr(2) & xChar1 & ".exe"
        Else
        AppBase Chr(2) & xRealBaseFile + "exe"
        End If
      End If
    End If

  xAutoUSBQuit = (Not (InStr(CommandX, "/PORTQUIT") = 0&))

  xLong1 = InStr(CommandX, "/PORTABLE")
  If Not xLong1 = 0& Then
    xChar3 = AppBase("lng")
    xChar4 = AppBase("icl")
    xLong1 = 256&
    If Not GetComputerNameA(ptrTemp, xLong1) = 0& Then
      xChar1 = PeekS(ptrTemp, xLong1)
      If Not Len(xChar1) = 0& Then
        AppBase Chr(2) + AppBase(Chr(3)) + LCase(xChar1) + ".exe"
        End If
      'Else
      'AppBase Chr(2) + AppBase(Chr(3)) + "%PCNAME%.exe"
      End If
    'Debug.Print "PortableDir: " + AppBase(Chr(3)) + "%PCNAME%.exe" ' Chr(2) & xChar1 & ".exe"
    End If

#If UseFileLogging Then
  If FileExists(AppBase("ini")) Then
    Call MainForm_ctConsoleA_AddLog("AppBase: Ini found!")
    Else
#Else
  If Not FileExists(AppBase("ini")) Then
#End If
    xChar2 = RegReadString("Software\Microsoft\Windows\CurrentVersion", "ProgramFilesDir")
    If Len(xChar2) = 0& Then
      xChar2 = RegReadString("Software\Microsoft\Windows\CurrentVersion", "ProgramFilesPath")
      If Len(xChar2) = 0& Then
        xChar2 = RegReadString("Software\Microsoft\Windows\CurrentVersion\SETUP\VarLDID", "28700")
        End If
      End If
    If Len(xChar2) = 0& Then
      'If xType1.dwPlatformId = &H2& Then
        #If UseFileLogging Then
          'Call MainForm_ctConsoleA_AddLog("AppBase: Cannot detect programs dir, and is NT")
          Call MainForm_ctConsoleA_AddLog("AppBase: Cannot detect programs dir!")
          #End If
        GoTo UseAppData
#If UseFileLogging Then
      '  Else
      '  Call MainForm_ctConsoleA_AddLog("AppBase: Cannot detect programs dir, and is 9x")
  #End If
      '  End If
      Else
      If Not Right(xChar2, 1&) = "\" Then xChar2 = xChar2 + "\"
      If LCase(Left(AppBase(""), Len(xChar2))) = LCase(xChar2) Then
        #If UseFileLogging Then
          Call MainForm_ctConsoleA_AddLog("AppBase: Is within programs dir")
          #End If
UseAppData:
        xChar1 = Trim(GetSpecialFolderPath(26&, True))
        If Not Len(xChar1) = 0& Then
          If CreateDirectoryEx(xChar1 & "ctuser\Calaym") Then
            xBool4_HasBase = False
            AppBase Chr(2) & Chr(34&) & xChar1 & "ctuser\Calaym\Calaym.exe" & Chr(34&)
            End If
          End If
#If UseFileLogging Then
        Else
        Call MainForm_ctConsoleA_AddLog("AppBase: Is anywhere")
#End If
        End If
      End If
    End If
  #If UseFileLogging Then
    Call MainForm_ctConsoleA_AddLog("  " + AppBase("*"))
    #End If
  
  
  
  
  #If UseFileLogging Then
    LogFileLog = AppBase("dbg")
    hFile = TxtFile_WriteCreate(LogFileLog)
    If Not hFile = 0& Then
      LogFileDoLog = True
      Call TxtFile_WriteClose(hFile)
      Call MainForm_ctConsoleA_AddLog("Log file for " + GBN1)
      End If
    #End If


  #If UseFileLogging Then
    Call GetOptimalPLMaxCount
    #End If



  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Attempting to load language file..."
    #End If
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  base0: " + AppBase("lng/icl")
    #End If
  If Not LoadResEx_SetLanguageFile(AppBase("lng"), ResFileIDString) Then
    If Not Len(xChar3) = 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  base1: " + xChar3
        #End If
      If LoadResEx_SetLanguageFile(xChar3, ResFileIDString) Then GoTo HasLngPass
      End If
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  base2: " + xRealBaseFile + "lng"
      #End If
    Call LoadResEx_SetLanguageFile(xRealBaseFile + "lng", ResFileIDString)
HasLngPass:
    End If


  If Not LoadResEx_SetThemeFile(AppBase("icl"), ThemeFileIDString) Then
    If Not Len(xChar4) = 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  base1: " + xChar4
        #End If
      If LoadResEx_SetThemeFile(xChar4, ThemeFileIDString) Then GoTo HasIclPass
      End If
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  base2: " + xRealBaseFile + "icl"
      #End If
'      Stop
    Call LoadResEx_SetThemeFile(xRealBaseFile + "icl", ThemeFileIDString)
HasIclPass:
    End If




  
#If InIDE And 0 Then
    If Not LoadResEx_SetLanguageFile(xRealBaseFile + "lng", ResFileIDString) Then
      If IsUnicode Then
        xChar1 = PeekS_Wide(GetCommandLineW)
        Else
        xChar1 = PeekS_Ansi(GetCommandLineA)
        End If
      AppBase Chr(2) & xChar1 & ".exe"
        xRealBaseFile = AppBase("")
      End If
#End If
  





  
  If Not InStr(CommandX, "/UNINSTALLREG") = 0& Then
    RestoreRegOnlyNow = 2&
    GoTo RestoreRegOnly1
    ElseIf Not InStr(CommandX, "/RESTOREREG") = 0& Then
    RestoreRegOnlyNow = 1&
    GoTo RestoreRegOnly1
    End If

  If InStr(CommandX, "/MULTIINST") = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Sending m3u command to previous instance"
      #End If
    If PrevInstanceEx(CommandZ) Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  success"
        #End If
      GoTo ErMustQuitB
      'xInfoQuit = True
      'MainForm_UnloadP
      'Exit Sub
#If UseFileLogging Then
      Else
      MainForm_ctConsoleA_AddLog "  no previous instance found"
#End If
      End If
    End If







  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  If InStr(CommandX, "/KEEPLOGS") = 0& Then
    LogFile = AppBase("log")
    hFile = TxtFile_WriteCreate(LogFile)
    If Not hFile = 0& Then
      LogFileDo = True
      Call TxtFile_WriteClose(hFile)
      End If
    Else
    LogFile = AppBase("")
    LogFile = Left(LogFile, Len(LogFile) - 1&)
    LogFileDo = True
    LogFileMulti = True
    End If
  
  
  
  
  
  
  
  
  



  









  
  
  
  
  
  
  
  CurShownToolTip = -1&
  'ToolTipInitTime = -1&
  xCalaymClassAlign = -1&
  ForceLoudestTear = -1&
  If IsUnicode Then
    DefaultPLExtension = "m3u8"
    Else
    DefaultPLExtension = "m3u"
    End If

  For xLong1 = 0& To 4&
    FormatIcon(xLong1) = LoadResEx_Data(1900& + xLong1, 2&, LoadResEx_ThemeFile_hModule)
    Next
  DynButtonsResX = LoadResEx_Data(1005&, 2&, LoadResEx_ThemeFile_hModule)
  If Len(DynButtonsResX) = 0& Then DynButtonsResX = LoadResEx_Data(1005&, 2&, App.hInstance)
  DynButtonsResXRep = LoadResEx_Data(1010&, 2&, LoadResEx_ThemeFile_hModule)
  If Len(DynButtonsResXRep) = 0& Then DynButtonsResXRep = LoadResEx_Data(1010&, 2&, App.hInstance)
  xTimerIconBMP = LoadResEx_Data(2600&, 2&, LoadResEx_ThemeFile_hModule)
  If Len(xTimerIconBMP) = 0& Then xTimerIconBMP = LoadResEx_Data(2600&, 2&, App.hInstance)
  
  vKioskMode = (InStr(CommandX, "/LOCKED") + InStr(CommandX, "/KIOSK")) > 0&
  xSecureMode = (Not InStr(CommandX, "/SECURE") = 0 Or vKioskMode)
  UrlsAllowed = (Not InStr(CommandX, "/URL") = 0) Or Not (xSecureMode Or vKioskMode)
  
  If Not MainForm_CreatePlayerForm Then GoTo ErMustQuitB
  
  GetWhiteBackGrnd 'True
   
   
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Loading & initializing layer"
    #End If
  
  EnqueueFlag = (Not InStr(CommandX, "/ENQUEUE") = 0&)
  xShellMode = (Not InStr(CommandX, "/SHELL") = 0&)
  
  Do
    DoEvents
    Select Case ctSSLayer_LoadVB(xRealBaseFile + "lay", "ctsslay.dll")
      Case -1&
        GenericMessageHandler_Param = LoadLibrary(xRealBaseFile + "lay")
        If GenericMessageHandler_Param = 0& Then GenericMessageHandler_Param = LoadLibrary("ctsslay.dll")
        If GenericMessageHandler_Param = 0& Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  Error: Layer missing"
            #End If
          GenericMessageHandler_Param = 2050&
          Else
          Call FreeLibrary(GenericMessageHandler_Param)
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  Error: Layer cannot load"
            #End If
          GenericMessageHandler_Param = 2051&
          End If
        If Not LoadResEx_Dialog(0&, 2000&, 2090&, 2091&, AddressOf GenericMessageHandler) = 2090& Then
          GoTo ErMustQuit
          End If
      Case 0&
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  Error: Layer incompatible"
          #End If
        GenericMessageHandler_Param = 2052&
        'UnloadLayerOnDialog = True
        If Not LoadResEx_Dialog(0&, 2000&, 2090&, 2091&, AddressOf GenericMessageHandler) = 2090& Then
          'If UnloadLayerOnDialog Then ctSSLayer_UnLoadVB: UnloadLayerOnDialog = False
          GoTo ErMustQuit
          End If
      Case Else
        LayerHP = ctSSLayer_GetInfoString(&H40&)
        Call ctSSLayer_PreInit(MainHWnd)
        xLong1 = ctSSLayer_CanUseItem(&H20&)
        SSRestrictions = ""
        For xLong2 = &H50& To &H5F&
          xChar1 = ctSSLayer_GetInfoString(xLong2)
          If Len(xChar1) = 0& Then Exit For
          SSRestrictions = SSRestrictions + ", " + xChar1
          Next
        SSRestrictions = Mid(SSRestrictions, 3&)
        If Len(SSRestrictions) = 0& And xLong1 = 13& Then SSRestrictions = "???"
        #If UseFileLogging Then
          If xLong1 < 20& Then
            If Not xLong1 = 0& Then
              xChar1 = "Respond"
              GoTo ShareIniVer
              End If
            Else
            xChar1 = "Error"
ShareIniVer:
            If xLong1 < 10& Or xLong1 > 19& Then
              xLong2 = 9&
              Else
              xLong2 = xLong1
              End If
            MainForm_ctConsoleA_AddLog "  " + xChar1 + " (" + CStr(xLong1) + "); " + ctSSLayer_GetInfoString(&H0&) + " " + ctSSLayer_GetInfoString(&H30&)
            MainForm_ctConsoleA_AddLog "  " + Replace(LoadResEx_String(2100& + xLong2), "$S", ctSSLayer_GetInfoString(5&))
            End If
          If Not Len(SSRestrictions) = 0& Then
            MainForm_ctConsoleA_AddLog "  Restrictions: " + SSRestrictions
            End If
          If xLong1 = 90& Then
            MainForm_ctConsoleA_AddLog "  Possible dlls missing: " + ctSSLayer_GetInfoString(&H31&)
            End If
          #End If
        If Len(SSRestrictions) = 0& Then
          SSRestrictions = "$I"
          Else
          SSRestrictions = Replace(LoadResEx_String(2102&), "$R", SSRestrictions)
          End If
        
        GenericMessageHandler_Param = 2100& + xLong1
        If xLong1 < 20& Then
          Exit Do
          Else
          Select Case xLong1
            Case 20&, 21&
              xLong1 = 2120&
              GenericMessageHandler_Param = 2120&
            Case 30& To 32&
              xLong1 = 2130&
            Case 90&
              xLong1 = 2190&
            Case Else
              xLong1 = 2100& ' + xLong1
            End Select
          UnloadLayerOnDialog = True
          If Not LoadResEx_Dialog(0&, xLong1, 2090&, 2091&, AddressOf GenericMessageHandler) = 2090& Then
            If UnloadLayerOnDialog Then ctSSLayer_UnLoadVB: UnloadLayerOnDialog = False
            GoTo ErMustQuit
            End If
          End If
      End Select
    If UnloadLayerOnDialog Then ctSSLayer_UnLoadVB: UnloadLayerOnDialog = False
    Loop
  GoTo NoMustQuit
     
ErMustQuit:
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  quitting"
    #End If
  xInfoQuit = True
  DoEvents
  MainForm_UnloadP
  'UnloadP
  Exit Sub

ErMustQuitB:
  xInfoQuit = True
  Exit Sub


NoMustQuit:
  MaxDeviceCount = ctSSLayer_CanUseItem(&H7&)
  If MaxDeviceCount = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  Error: No sounddevices"
      #End If
    DoEvents
    GenericMessageHandler_Param = 2100&
    Call LoadResEx_Dialog(0&, 2100&, 2180&, 2091&, AddressOf GenericMessageHandler)
    ctSSLayer_UnLoadVB
    GoTo ErMustQuit
    End If
 
 
 
 
 
 
 
 
 
 
  ePic1TopX = 15&
  ePic1TopX2 = 28&
  
  Randomize DoEventsEx_GetTickCount
  RndPatternAlias(0) = ConstByte_4
  RndPatternAlias(1) = ConstByte_0
  RndPatternAlias(2) = ConstByte_6
  RndPatternAlias(3) = ConstByte_2
  RndPatternAlias(4) = ConstByte_B
  RndPatternAlias(5) = ConstByte_9
  RndPatternAlias(6) = ConstByte_D
  RndPatternAlias(7) = ConstByte_7
  RndPatternAlias(8) = ConstByte_F
  RndPatternAlias(9) = ConstByte_1
  RndPatternAlias(10) = ConstByte_C
  RndPatternAlias(11) = ConstByte_5
  RndPatternAlias(12) = ConstByte_8
  RndPatternAlias(13) = ConstByte_A
  RndPatternAlias(14) = ConstByte_3
  RndPatternAlias(15) = ConstByte_E
   
  xUsedTypes = ctSSLayer_GetInfoString(&H19&)
  For xLong1 = 0& To RndPLPatternCount
    RndPLPattern(xLong1) = CByte(Rnd * RandomizerPattern)
    Next
   
  
  xTimerEvIR = -1
  
  
  
  'xLong1 = GetWindowThreadProcessId(mainhwnd, xProcID)
  'If xLong1 = 0& Then xProcID = 0&
  xProcID = GetCurrentProcessId
  
  
  DoEvents
  
  
  Call GetControls(GetDefControls, xKey())
  
  
  
  xOldIconState = -1&
  CheckAccurate = True
  UsePLSettings = True
  'xCurStreamChannelIndex = 0&
  xTrayIconOld = 255&
  xDelMode = False
  xSetVolume = 200&
  xTrackerSkipper(ctSSLayer_MusicTypeStream) = -7&
  xTrackerSkipper(ctSSLayer_MusicTypeTracker) = 5&
  xTrackerSkipper(ctSSLayer_MusicTypeCDAudio) = 5&
  IntroModeTime = IntroModeTimeDef
  RandomPosOnTrackerSkipper = True
  
  xModPanning = 75&
  xAutoPlayback = True
  xReAutoPlay = True
  xDirList = -4&
  
  xGenFading = True
  CurrentFindMatchCase = vbTextCompare
  MP3GetFormat = True
  MP3GetID3Tag = True
  xBool1 = False 'True
  xFollowCurSong = True
  xRemoveMissingSongs = True
  'RemoveMissingOnPLLoad = True
  ShowFullPath = False
  ShowFullTitle = False
  #If SelfPathElipse Then
    'NeedIRPathElipse = True
    #End If
  
  xProcessMode = 1&
  xTrayIconLeft = 1&
  xTrayIconRight = 1&
  PlDClickAction = 1&
  
  xCrossFadeTimeM = 12& - xFadeConsts1
  StartUpWindowMode = 1&
  xRep = 2& 'random
  xSearchAutoPlay = True
  xSearchAutoClose = True
  
  xRadioAutoClose = True
  
  xSorting = True
  SoundSchemePointerMode = 1&

  OutOfSessionPause = True
  InitOutOfSession
  
  
  
  xTrayIconVisible = True
  xMWinPos.Left = -1&
  xMWinPos.Right = -1&
  xMWinPos.Top = -1&
  xMWinPos.Bottom = -1&
  Call RePosWindow
  
RestoreRegOnly1:
  'CommandZ = "/BASE=\TEST"
  'CommandX = "/BASE=\TEST"
  
  
  If Not RestoreRegOnlyNow = 0& Then GoTo RestoreRegOnly2
  
  If vKioskMode Then
    xMenu(0) = LoadResEx_Menu(1010&)
    xMenu(2) = LoadResEx_Menu(1130&)
    'xMenu(3) = LoadResEx_Menu(1130&)
    xMenu(5) = LoadResEx_Menu(1070&)
    xMenu(8) = LoadResEx_Menu(1075&)
    Else
    xMenu(0) = LoadResEx_Menu(1000&)
    xMenu(2) = LoadResEx_Menu(1100&)
    xMenu(3) = LoadResEx_Menu(1110&)
    xMenu(5) = LoadResEx_Menu(1060&)
    xMenu(6) = LoadResEx_Menu(1140&)
    xMenu(7) = LoadResEx_Menu(1141&)
    xMenu(8) = LoadResEx_Menu(1065&) ' 1000&
    'xPopupMenu(3) = CreatePopupMenu()
    End If
  xMenu(1) = LoadResEx_Menu(1050&)
  xMenu(4) = LoadResEx_Menu(1120&)
  If Not xMenu(0) = 0& Then
    SetMenu MainHWnd, xMenu(0)
  '  'Call InsertMenuA(GetMenu(mainhwnd), -1&, &H410&, xMenu(0), ByVal vbNullChar)
    DrawMenuBar MainHWnd
    End If
  
  For xLong1 = 1& To xMenuCountX
    xPopupMenu(xLong1) = LoadResEx_Menu_MakePopupMenu(xMenu(xLong1), False)
    Next

  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Loading player form"
    #End If



  If Not InStr(CommandX, "/LED") = 0& Then
    If IsUnicode Then
      '= nt
      LEDMode = 2&
      MapVirtualKeyA_H14 = MapVirtualKey(&H14&, 0&)
      MapVirtualKeyA_H90 = MapVirtualKey(&H90&, 0&)
      MapVirtualKeyA_H91 = MapVirtualKey(&H91&, 0&)
      Else
      LEDMode = 1&
      End If
    LEDState = &H2&
    Call InfoProcKb(0&, 0&, 0&, 0&)
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Starting LED timer"
      #End If
    Call SetTimer(MainHWnd, 0, InfoProcIntervalKb, AddressOf InfoProcKb)
    'xTimerLoaded = xTimerLoaded Or &H100&
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "LED timer started"
      #End If
    LEDState = 0&
    End If


  If Not (InStr(CommandX, "/HIDETASK") = 0& Or xProcID = 0&) Then
    On Error Resume Next
    Call RegisterServiceProcess(xProcID, 1)
    On Error GoTo 0
    End If
  If Not InStr(CommandX, "/NOTASKMGR") = 0 Then
    On Error Resume Next
    Call SystemParametersInfoA(97&, True, ByVal 1&, 0&)
    On Error GoTo 0
    End If

  'Call LoadResEx_SetDlgItemCaption(Me)
  Call LoadResEx_WindowIcon(MainHWnd, 1300&, VarPtr(SysTrayIcon))
  'If MP3GetFormat Then
  '  Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(-1), 0&)
  '  End If

  'MainForm_EnableApiMenu 405&, False
  'MainForm_EnableApiMenu 406&, False
  MainForm_EnableApiMenu 407&, False
  MainForm_EnableApiMenu 408&, False
  
  MainForm_EnableApiMenu 411&, False
  MainForm_EnableApiMenu 412&, False
  MainForm_EnableApiMenu 413&, False
  MainForm_EnableApiMenu 414&, False
  MainForm_EnableApiMenu 420&, False
  MainForm_EnableApiMenu 428&, False
  MainForm_EnableApiMenu 429&, False
  MainForm_EnableApiMenu 443&, False
  MainForm_EnableApiMenu 444&, False
  'EnableApiMenu 445&, False
  DoEvents
  If Not InStr(CommandX, "/FAILSAVE") + InStr(CommandX, "/NOLOADSETTINGS") + InStr(CommandX, "/DEFAULT") = 0 Then
    GoTo Rs1
    End If

RestoreRegOnly2:

  hFile = TxtFile_ReadOpen(AppBase("ini"))
  If Not hFile = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Reading setup file (" & AppBase("ini") & ")"
      #End If
    Do While TxtFile_ReadLine(hFile, xChar1)
      xLong1 = InStr(xChar1 + "=", "=")
      If Not xLong1 = 0& Then
        xChar2 = Mid(xChar1, xLong1 + 1&)
        xChar1 = Left(xChar1, xLong1 - 1&)
        xLong1 = Val(xChar2)
        Select Case UCase(xChar1)
          
          Case "ALLOWFILEDELETING"
            xDelMode = (xLong1 > 0&)
            'EnableApiMenu 133, xDelMode
            'EnableApiMenu 230, xDelMode
          Case "AUTOPLAY"
            xAutoPlayback = (xLong1 > 0&)
            xReAutoPlay = xAutoPlayback
          Case "AUTOPAUSEONUSERSWITCH"
            OutOfSessionPause = (xLong1 > 0&)
          Case "BLOCKIDEWHILELOADING"
            xClassicBlockingOnLoad = (xLong1 > 0&)
          Case "CDDA"
            CDDAUse = (xLong1 > 0&)
          Case "CDDAJITTERCORRECTION"
            CDDAJitter = (xLong1 > 0&)
          Case "CHECKACCURATESTREAMS"
            CheckAccurate = (xLong1 > 0&)
          Case "CHECKSONGSONPLAYLISTLOAD"
            RemoveMissingOnPLLoad = (xLong1 > 0&)
          Case "CONTROLS"
            Call GetControls(xChar2, xKey())
          Case "CROSSFADETIME"
            xLong1 = xLong1 - xFadeConsts1
            If xLong1 < 0& Then
              xCrossFadeTimeM = 0&
              'xCrossFadeTimeM = xCrossFadeTime - xFadeConsts1
              ElseIf xLong1 > xFadeConstsX Then
              xCrossFadeTimeM = xFadeConstsX
              Else
              xCrossFadeTimeM = xLong1
              End If
            'xCrossFadeTimeM = xCrossFadeTime - xFadeConsts1
            'CalcCrossFadeConsts
          Case "DIRECTORYLIST"
            If xLong1 < xDirListMaxX Then
              xDirList = xDirListMaxX
              ElseIf xLong1 > xDirListMax Then
              xDirList = xDirListMax
              ElseIf Not (xLong1 = 0&) Then
              xDirList = xLong1
              End If
          Case "DISABLEWINDOWSSOUNDS"
            If Not xLong1 < 0& And xLong1 < 3& Then
              SoundSchemePointerMode = xLong1
              'ApplyProcessMode
              End If
          Case "ELAPSEDTIME"
            xShowElapsed = (xLong1 > 0&)
          Case "EMPTYPLAYLISTSTARTUP"
            If Not xLong1 < 0& And xLong1 < 7& Then
              xOnEmptyPLStartup = xLong1
              End If
          Case "ENDSONGMODE"
            If Not xLong1 < 0& And xLong1 < 4& Then
              xProcessMode = xLong1
              'ApplyProcessMode
              End If
          Case "ENDSTREAMS"
            GetValidSkipper ctSSLayer_MusicTypeStream, xLong1
          Case "ENDTRACKERS"
            GetValidSkipper ctSSLayer_MusicTypeTracker, xLong1
          Case "ENDCDAUDIO"
            GetValidSkipper ctSSLayer_MusicTypeCDAudio, xLong1
          Case "FADING"
            xGenFading = (xLong1 > 0&)
            'CheckApiMenu 301&, xGenFading
          Case "FAILSAVE"
            If InStr(CommandX, "/NOFAILSAVE") = 0 And RestoreRegOnlyNow = 0& Then
              xBool2 = (xLong1 > 0&)
              End If
          Case "FOLLOWSONG"
            xFollowCurSong = (xLong1 > 0&)
            'CheckApiMenu 310&, xFollowCurSong
          Case "GETFILEFORMAT", "GETMP3FORMAT"
            MP3GetFormat = (xLong1 > 0&)
            Call MP3GetFormatChanged(False)
            'If RestoreRegOnlyNow = 0& Then
            '  ShowWindow xStaticIconsHWnd(xStaticIcons_MP3FormatIcon), -5& * CLng(MP3GetFormat)
            '  End If
          Case "GETFILEINFOS"
            MP3GetID3Tag = (xLong1 > 0&)
            'CheckApiMenu 303&, MP3GetID3Tag
            If RestoreRegOnlyNow = 0& Then
              If MP3GetID3Tag Then
                Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 5&)
                Else
                Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 0&)
                End If
              End If
          Case "GLOBALPLAYKEYS"
            GlobalKeys = (xLong1 > 0&)
          Case "IGNOREUNDERSCORED"
            IgnoreUnderscored = (xLong1 > 0&)
          Case "INTROMODE"
            If xLong1 < 0& Then
              IntroModeActive = False
              xLong1 = -xLong1
              Else
              IntroModeActive = True
              End If
            If xLong1 < IntroMdSkipperMinVal Then
              IntroModeTime = IntroMdSkipperMinVal
              ElseIf xLong1 > IntroMdSkipperMaxVal Then
              IntroModeTime = IntroMdSkipperMaxVal
              Else
              IntroModeTime = xLong1
              End If
          Case "NOVERWARN": xNoVerWarnDLL = LCase(xChar2)
          Case "OUTPUTDEVICE"
            xOutputSSLID = xChar2
          Case "PLAYERWINDOWPOS"
            If RestoreRegOnlyNow = 0& Then
              Call GetMyWindowPos(xChar2, xLong1)
              End If
          Case "PLAYERWINDOWSTATE": If Not xLong1 < -1& And xLong1 < 3& Then StartUpWindowMode = xLong1
          Case "PLAYLISTDOUBLECLICK": If Not xLong1 < 0& And xLong1 < 3& Then PlDClickAction = xLong1
          Case "PLAYLISTUPDATEINTERVAL"
            If Not xLong1 < 0& And xLong1 < 3& Then
              LockWinUpd = xLong1
              End If
          Case "PRIORITY"
            If xLong1 < -2& Then
              PlayerPriority = -2&
              ElseIf xLong1 > 3& Then
              PlayerPriority = 3&
              Else
              PlayerPriority = xLong1
              End If
  #If UseIRPlugins Then
          Case "RADIOWINDOWPOS"
            Call GetMyWindowPos(xMWindowID_IRadio, xChar2, xLong1)
  #End If
          Case "RADIOAUTOCLOSE"
            xRadioAutoClose = (xLong1 > 0&)
          Case "RANDOMSEEK"
            RandomPosOnTrackerSkipper = (xLong1 > 0&)
          Case "REGISTERFOLDERS"
            'If Not xLong1 < 0& And xLong1 < 15& Then
              RegFolderMode(0) = CBool(xLong1 And 1&)
              RegFolderMode(1) = CBool(xLong1 And 2&)
              RegFolderMode(2) = CBool(xLong1 And 4&)
              RegFolderMode(3) = CBool(xLong1 And 8&)
            '  End If
          Case "REMOVEMISSINGSONGS"
            xRemoveMissingSongs = (xLong1 > 0&)
            'CheckApiMenu 320&, xRemoveMissingSongs
          Case "REMOVEPLAYEDSONGS"
            xRemovePlayedSongs = (xLong1 > 0&)
            'CheckApiMenu 322&, xRemovePlayedSongs
          Case "REMOVESKIPPEDSONGS"
            xRemoveSkippedSongs = (xLong1 > 0&)
            'CheckApiMenu 321&, xRemoveSkippedSongs
          Case "REPEATMODE"
            If Not xLong1 < 0 And xLong1 < 3 Then
              xRep = xLong1
              'ApplyRepMode
              End If
          Case "SEARCHAUTOCLOSE"
            xSearchAutoClose = (xLong1 > 0&)
          Case "SEARCHAUTOPLAY"
            xSearchAutoPlay = (xLong1 > 0&)
          Case "SHOWFULLPATH"
            ShowFullPath = (xLong1 > 0&)
          Case "SHOWFULLTITLE"
            ShowFullTitle = (xLong1 > 0&)
'            #If SelfPathElipse Then
'              'NeedIRPathElipse = True
'              #End If
          Case "SORTING"
            xSorting = (xLong1 > 0&)
          Case "SPECTRUM"
            xBool1 = (xLong1 And 1&) = 1&
            xDeluxeSpectrum = (xLong1 And 2&) = 2&
  '        Case "TAGSFONT"
  '          GrabPLFont 1&, xChar2
          Case "TITLESPOSITION"
            If Not (xLong1 < 0& Or xLong1 > 15&) Then
              xTitlesPos = xLong1
              End If
          Case "TITLESWHENMINIMIZED"
            If Not (xLong1 < 0& Or xLong1 > 2&) Then
              xTitlesWhenMinimized = xLong1
              End If
          Case "TRACKERPANSEPERATION"
            If xLong1 < 0& Then
              xModPanning = 0&
              ElseIf xLong1 > 100& Then
              xModPanning = 100&
              Else
              xModPanning = xLong1
              End If
          Case "TRAYICON"
            xTrayIconVisible = (xLong1 > 0&)
          Case "TRAYLEFTCLICK": If Not xLong1 < 0 And xLong1 < 7 Then xTrayIconLeft = xLong1
          Case "TRAYMINIMIZE", "MINIMIZETOTRAY"
            xTrayIconEnabled = (xLong1 > 0&)
          Case "TRAYRIGHTCLICK": If Not xLong1 < 0 And xLong1 < 3 Then xTrayIconRight = xLong1
          Case "TUTORIALSHOWN": FirstTimeHelp = Not (xLong1 = 0&)
          Case "USEDFILETYPES"
              xUsedTypes = xChar2
          Case "USEPLAYLISTSETTINGS"
            UsePLSettings = (xLong1 > 0&)
          Case "VOLUME"
            If xLong1 > 255& Then
              xSetVolume = 255&
              ElseIf xLong1 < 0& Then
              xSetVolume = 0&
              Else
              xSetVolume = xLong1
              End If
      '      PlayerIsLocked = False
      '      DoVolume xSetVolume, True, False
            PlayerIsLocked = True
          #If UseFileLogging Then
            Case ""
            Case Else
              'MainForm_ctConsoleA_AddLog "Unknown tag: '" & Left(xChar1, InStr(xChar1 + "=", "=") - 1) & "' (tag ignored)"
              MainForm_ctConsoleA_AddLog "Unknown tag: '" & xChar1 & "' (tag ignored)"
            #End If
          End Select
        End If
      Loop
    
    Call TxtFile_ReadClose(hFile)
#If UseFileLogging Then
    Else
    MainForm_ctConsoleA_AddLog "Error reading setup file (" & AppBase("ini") & ")"
#End If
    End If













Rs1:
  If Not RestoreRegOnlyNow = 0& Then
    If RestoreRegOnlyNow = 2& Then
      If RegOpenKeyEx(&H80000001, "Software\Microsoft\Windows\CurrentVersion\Run", 2&, xLong1) Then
        Call RegDeleteValue(xLong1, LoadResEx_String(3500&))
        Call RegCloseKey(xLong1)
        End If
      For xLong1 = 0& To 3&
        RegFolderMode(xLong1) = True
        Next
      End If
    MainForm_RestoreScheme
    UpdateFolderReg False
    TestRestoreReg True
    xInfoQuit = True
    Exit Sub
    'ctSSLayer_UnLoadVB
    'MainForm_UnloadP
    'GoTo ErMustQuit
    End If
  
  GetIsAutoRun
  
  
  MainForm_UpdateMyFont
  UpdateFolderReg True
  MainForm_ApplyProcessMode
  'TestRestoreReg false
  
  MainForm_EnableApiMenu 133, xDelMode
  MainForm_EnableApiMenu 230, xDelMode
  CheckApiMenu 301&, xGenFading
  CheckApiMenu 310&, xFollowCurSong
  CheckApiMenu 303&, MP3GetID3Tag
  CheckApiMenu 320&, xRemoveMissingSongs
  CheckApiMenu 321&, xRemoveSkippedSongs
  CheckApiMenu 322&, xRemovePlayedSongs
  
  
  MainForm_LockMe True
  xPStateIconStartup = True
  MainForm_UpdateMainIcon
  
  hwndSpectrumLeft = hwndSpectrumLeftDef
  hwndSpectrumTop = hwndSpectrumTopDef
  
  SetPanelSize
  EnableWindow MainHWnd, 1&

  If StartUpWindowMode = -1& Then
    Call GetStartupInfoA(xType2)
    With xType2
      If (.dwFlags And STARTF_USEPOSITION) = STARTF_USEPOSITION Then
        xMWinPos.Left = .dwX
        xMWinPos.Top = .dwY
        End If
      If (.dwFlags And STARTF_USESIZE) = STARTF_USESIZE Then
        xMWinPos.Right = .dwXSize
        xMWinPos.Bottom = .dwYSize
        End If
      CheckValidScreen False
      If (.dwFlags And STARTF_USESHOWWINDOW) = STARTF_USESHOWWINDOW Then
        If (.wShowWindow And &H6&) = &H6& Then
          'WindowState = 2&
          ShowWindow MainHWnd, 3&
          ElseIf (.wShowWindow And &H2&) = &H2& Then
          'WindowState = 1&
          ShowWindow MainHWnd, 7&
          Else
          ShowWindow MainHWnd, 5&
          End If
        End If
      End With
    Else
    CheckValidScreen False
    Select Case StartUpWindowMode
      Case 1&
        ShowWindow MainHWnd, 7&
      Case 2&
        ShowWindow MainHWnd, 3&
      Case Else
        ShowWindow MainHWnd, 5&
      End Select
    End If

  DoEvents

  xTrayIconOld = 0&
  MainForm_ShowTray False




  If Not (InStr(CommandX, "/SHOWFAILSAVE") = 0& Or RestoreRegOnlyNow) Then
    xBool2 = True
    End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Performing settings"
    #End If
  CalcCrossFadeConsts
  xLong1 = InStr(CommandX, "/LOCKED")
  If xLong1 = 0 Then
    xLong1 = InStr(CommandX, "/KIOSK")
    Else
    xLong1 = xLong1 + 1&
    End If

  
  
  If Not InStr(CommandX, "/NOSPECTRUM") = 0 Then
    xNoSpectrumForce = True
    xBool1 = False
    If Not xLong1 = 0& Then
      CheckApiMenu 302&, False
      MainForm_EnableApiMenu 302&, False
      End If
    End If

  SetFormatToolTip LoadResEx_String(1900&), ""
  If vKioskMode Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  Applying kiosk mode settings"
      #End If
    If Not InStr(CommandX, "/VOLUME") = 0& Then
      vCanVolume = True
      Else
      For xLong1 = 340& To 355&
        MainForm_EnableApiMenu xLong1, False
        Next
      End If
    If Mid(CommandX, xLong1 + 6&, 1&) = ":" Then
      xChar1 = Mid(CommandX, xLong1 + 7&)
      xChar1 = LCase(Left(xChar1, InStr(xChar1 & " ", " ") - 1)) & ","
      GoSub HandleSett
      End If
    hWndTagsTop = PanelSizeDefKiosk
    Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Tags), 0&, 0&, hWndTagsTop, hWndTagsWidth, hWndTagsHeight, &H214&)
    MainForm_EnableApiMenu 104&, False
    MainForm_EnableApiMenu 405&, False
    CanIRadio = False
    MainForm_EnableApiMenu 406&, False
    'MainForm_EnableApiMenu 407&, False
    'MainForm_EnableApiMenu 408&, False
    MainForm_EnableApiMenu 419&, False
    MainForm_EnableApiMenu 450&, False
    SetToolTip xStaticIcons_Def3, LoadResEx_String(1140&), ExtKbdEx(KbdC_CStop)
    If Not PlDClickAction = 0& Then MainForm_EnableApiMenu 201&, False
    If PlDClickAction = 2& Then MainForm_EnableApiMenu 202&, False
    If xProcessMode = 3& Then
      xProcessMode = 0&
      End If
    IntroModeActive = False
    xAutoPlayback = True
    xReAutoPlay = True
    xBool2 = False
    Else
    Set DragClass = New IDropTargetClass
    Call DragClass.DragOver(&HF0F00F0F, AddressOf DropFiles, 0&, DROPEFFECT_NONE)
    Call RegisterDragDrop(MainHWnd, ObjPtr(DragClass))
    End If


  #If FlatCastCF17 Then
    Set xFCI = CreateObject("FlP40.CCtrlProd")
    #End If

  MainForm_ApplyRepMode
  
  xLong1 = InStr(CommandX, "/SET")
  If Not (xLong1 = 0& Or vKioskMode) And Mid(CommandX, xLong1 + 4&, 1&) = ":" Then
    xChar1 = Mid(CommandX, xLong1 + 5&)
    xChar1 = LCase(Left(xChar1, InStr(xChar1 & " ", " ") - 1)) & ","
    GoSub HandleSett
    End If
  
  
  MainForm_EnableApiMenu 427&, (xDirList > 0& And Not xDirList = xDirListMax)
  MainForm_EnableApiMenu 428&, (xDirList > 0& And Not xDirList = xDirListMin)
  If xDirList > 0& Then MainForm_ToggleDirList False


  xNoIRMode = (Not InStr(CommandX, "/NOMSG") = 0)

  MainForm_XMenuRefresherDevices

  If Not InStr(CommandX, "/NOPLMENU") = 0& Then
    xNoPLMenu = True
    MainForm_EnableApiMenu 32&, False
    End If
  
  If Not InStr(CommandX, "/NOSAVELIST") = 0& Then
    If Not InStr(CommandX, "/NOSAVELISTMENU") = 0& Then
      MainForm_EnableApiMenu 40&, False
      MainForm_EnableApiMenu 41&, False
      xNoSaveListMenu = 2&
      Else
      xNoSaveListMenu = 1&
      End If
    End If
  
  If InStr(CommandX, "/NOQUIT") = 0 Then
    If InStr(CommandX, "/SHUTDOWN") = 0 Then
      If InStr(CommandX, "/HIBERNATE") = 0 Then
        If InStr(CommandX, "/STANDBY") = 0 Then
          If xShellMode Then
            xCmdQuitMode = 2&
            If xTrayIconEnabled Then
              xTrayIconEnabled = False
              MainForm_ShowTray True
              End If
            Else
            xCmdQuitMode = 3&
            End If
          Else
          xCmdQuitMode = 5&
          End If
        Else
        xCmdQuitMode = 4&
        End If
      Else
      xCmdQuitMode = 1&
      End If
    Else
    xNoQuitMenu = True
    MainForm_EnableApiMenu 60&, False
    End If

  'If Not InStr(CommandX, "/SECURE") = 0 Or vKioskMode Then
  If xSecureMode Then
    'xSecureMode = True
    MainForm_EnableApiMenu 10&, False
    MainForm_EnableApiMenu 12&, False
    MainForm_EnableApiMenu 31&, False
    MainForm_EnableApiMenu 41&, False
    MainForm_EnableApiMenu 133&, False
    MainForm_EnableApiMenu 134&, False
    MainForm_EnableApiMenu 230&, False
    MainForm_EnableApiMenu 250&, False
    MainForm_EnableApiMenu 442&, False
    MainForm_EnableApiMenu 443&, False
    MainForm_EnableApiMenu 461&, False
    MainForm_EnableApiMenu 462&, False
    MainForm_EnableApiMenu 501&, False
    End If
  
  UrlsAllowed = (Not InStr(CommandX, "/URL") = 0) Or Not (xSecureMode Or vKioskMode)
  DragOut = (Not InStr(CommandX, "/DRAG") = 0) Or Not (xSecureMode Or vKioskMode)
  
  If Not InStr(CommandX, "/NOSETUP") = 0 Then
    xNoSetup = True
    MainForm_EnableApiMenu 415&, False
    MainForm_EnableApiMenu 360&, False
    xBool2 = False
    End If
    
    
  xSupportedFilesS = vbNullChar & Replace(Replace(ctSSLayer_GetInfoString(&H10&), ",", vbNullChar), ";", vbNullChar)
  xSupportedFilesM = Replace(Replace(ctSSLayer_GetInfoString(&H12&), ",", vbNullChar), ";", vbNullChar)
  xSupportedFiles = xSupportedFilesS & Replace(Replace(ctSSLayer_GetInfoString(&H11&), ",", vbNullChar), ";", vbNullChar) + xSupportedFilesM
  xSupportedFilesM = vbNullChar & xSupportedFilesM
  If ctSSLayer_CanUseItem(&HA&) = 1& Then
    xSupportedFiles = xSupportedFiles & "cda" & vbNullChar
    End If
  MainForm_ResortTypes xSupportedFiles
  
  CheckFileTypes
    
  
  If InStr(CommandX, "/NOSAVESETTINGS") = 0& Then
    xCmdSaveSet = Not vKioskMode
    Else
    MainForm_EnableApiMenu 470&, False
    End If
  
  If xCmdSaveSet Then
    If xBool2 Then
      If Not (MaxDeviceCount = 0& Or ctSSLayer_CanUseItem(&H20&) > 19&) And (ctSSLayer_CanUseItem(4&) = 1& Or MaxDeviceCount > 1&) Then
        If InStr(CommandX, "/SHOWFAILSAVE") = 0 Then
          xAutoIgnoreTime = DoEventsEx_GetTickCount
          GenericMessageHandler_Param = 2070&
          Else
          GenericMessageHandler_Param = 2071&
          xAutoIgnoreTime = -1&
          End If
        If LoadResEx_Dialog(MainHWnd, 2070&, 2090&, 2091&, AddressOf GenericMessageHandler) = 2091& Then
          xInfoQuit = True
          If Not LEDMode = 0& Then
            LEDState = 1&
            Call KillTimer(MainHWnd, 0&)
            DoEvents
            Call InfoProcKb(0&, 0&, 0&, 0&)
            End If
          ctSSLayer_UnLoadVB
          GoTo ErMustQuit
          'UnloadP
          'Exit Sub
          End If
        End If
      Else
      MainForm_SaveSettings 1&, xBool1
      End If
    End If
  
  If Not InStr(CommandX, "/NOAUTOREFRESH") = 0 Then
    xNoAutoRefresh = True
    End If
  If xNoAutoRefresh Or Not InStr(CommandX, "/NOUSERAUTOREFRESH") = 0 Then
    xNoAutoRefreshByUser = True
    MainForm_EnableApiMenu 410&, False
    MainForm_EnableApiMenu 411&, False
    MainForm_EnableApiMenu 412&, False
    MainForm_EnableApiMenu 413&, False
    MainForm_EnableApiMenu 414&, False
    MainForm_EnableApiMenu 415&, False
    'If Not vKioskMode Then
    '  End If
    Else
    If Not InStr(CommandX, "/NOAUTOSETUP") = 0 Then
      MainForm_EnableApiMenu 415&, False
      xNoAutoSetup = True
      'If Not vKioskMode Then
        'mTMenu(3).Visible = False
        'mTMenu(5).Visible = Not (xAutoDirCount = 0& Or vKioskMode)
      '  End If
      End If
    End If
    
  
  
  DoEvents
  
  If Not InStr(CommandX, "/NOPRESETS") = 0 Then
    xNoPresets = True
    MainForm_EnableApiMenu 460&, False
    MainForm_EnableApiMenu 461&, False
    MainForm_EnableApiMenu 462&, False
    MainForm_EnableApiMenu 463&, False
    End If
  
  If Not InStr(CommandX, "/NOTIMER") = 0 Then
    xNoTimers = True
    MainForm_EnableApiMenu 440&, False
    MainForm_EnableApiMenu 441&, False
    MainForm_EnableApiMenu 442&, False
    MainForm_EnableApiMenu 443&, False
    MainForm_EnableApiMenu 444&, False
    MainForm_EnableApiMenu 445&, False
    End If


  Call ctSSLayer_RefreshDeviceList(MainHWnd)
  xInitMe MainHWnd, xBool2, xBool1, True


  If xInfoQuit Then
    'Exit Sub
    GoTo ErMustQuit
    End If

  If ctSSLayer_CanUseItem(&H11&) = 0& Then
    MainForm_EnableApiMenu 405&, False
    'MainForm_EnableApiMenu 406&, False
    'MainForm_EnableApiMenu 407&, False
    'MainForm_EnableApiMenu 408&, False
    Else
    CanIRadio = True
    End If

  If ctSSLayer_CanUseItem(0&) = 0& Then
    CDDAUse = False
    CDDAJitter = False
    ElseIf ctSSLayer_CanUseItem(0&) = 1& Then
    CDDAUse = True
    End If
  If ctSSLayer_CanUseItem(1&) = 0& Then
    CDDAJitter = False
    ElseIf ctSSLayer_CanUseItem(1&) = 1& Then
    CDDAJitter = True
    End If
  If ctSSLayer_CanUseItem(3&) = 0& Then
    CheckAccurate = False
    ElseIf ctSSLayer_CanUseItem(3&) = 1& Then
    CheckAccurate = True
    End If
  If ctSSLayer_CanUseItem(&H12&) = 0& Then
    xClassicBlockingOnLoad = True
    ElseIf ctSSLayer_CanUseItem(&H12&) = 1& Then
    xClassicBlockingOnLoad = False
    End If

  DoEvents

  lkCtrl = LoadResEx_String(3980&)
  If Left(lkCtrl, 1&) = "#" Then lkCtrl = GetKeyNameText(MapVirtualKey(17&, 0&) * &H10000)
  lkAlt = LoadResEx_String(3981&)
  If Left(lkAlt, 1&) = "#" Then lkAlt = GetKeyNameText(MapVirtualKey(18&, 0&) * &H10000)
  lkShift = LoadResEx_String(3982&)
  If Left(lkShift, 1&) = "#" Then lkShift = GetKeyNameText(MapVirtualKey(16&, 0&) * &H10000)

  lkCombi(0) = lkShift + "+"
  lkCombi(1) = lkCtrl + "+"
  lkCombi(2) = lkCtrl + "+" + lkShift + "+"
  lkCombi(3) = lkCtrl + "+" + lkAlt + "+"
  lkCombi(4) = lkCtrl + "+" + lkAlt + "+" + lkShift + "+"
  
  MainForm_UpdateMenus
  
  ReSetTearArrayCount

  MainForm_DrawVolFaderA xSetVolume, 2&
  DoVolume xSetVolume, True
  
  DoEvents

  SetPanelSize
  NewSongID = Tear_Create()



  Call SetTimer(MainHWnd, 4&, 100&, AddressOf TearDown)



  If InStr(CommandX, "/NOLOADLIST") = 0 Then
    xChar1 = FilteredCMD(GetStrippedCommandLine)
'    Stop
    If Not Len(xChar1) = 0& Then
      xChar1 = GetFullPathName(xChar1)
      If AllowedItem(xChar1, True) = 255& Then
        xBool3 = LoadPList(xChar1, False, True) 'Not xBool2)
        ElseIf AllowedItem(xChar1, True) = 0& Then
        xBool3 = AddFDirNowSubEx(xChar1, False)
        End If
      End If
    If Not xBool3 Then
      xChar1 = FilteredCMD(CommandZ)
      'Stop
      If Not Len(xChar1) = 0& Then
        xChar1 = GetFullPathName(xChar1)
        If AllowedItem(xChar1, True) = 255& Then
          xBool3 = LoadPList(xChar1, False, True) 'Not xBool2)
          ElseIf AllowedItem(xChar1, True) = 0& Then
          xBool3 = AddFDirNowSubEx(xChar1, False)
          End If
        End If
      If Not xBool3 Then
        xCurPlistFilename = AppBase(DefaultPLExtension)
        Call LoadPList(xCurPlistFilename, False, True) 'Not xBool2)
        xCurPlistFilenameSuggestNew = True
        End If
      End If
    MainForm_LockMe True
    End If
  
  
  
  DoEvents
  
  
  
'  On Error GoTo 0
  #If TrayIconViaTimerCheck Then
    xTrayIconCrashUpdateTimer = DoEventsEx_GetTickCount
    #End If


  If Not xBool4_HasBase Then
    xChar1 = Trim(GetSpecialFolderPath(5&, False))
    If Len(xChar1) > 3& Then
      If FileExists(xChar1 + "\Calaym") Then
        m3uList = xChar1 + "\Calaym"
        Else
        m3uList = xChar1
        End If
      End If
    End If
  
  If Len(m3uList) = 0& Then
    xChar1 = AppBase("")
    m3uList = SimpleDir(xChar1)
    End If
  
  'If Not xBool3 Then
  '  xCurPlistFilename = m3uList
  '  End If
  
  
  vPLXMain = m3uList & "\"
  If Len(m3uList) = 2& Then
    m3uList = m3uList & "\"
    End If
  PresetFile = m3uList
  TimerDir = m3uList
  xLastDirectory = m3uList
  
  MainForm_EnableApiMenu 33&, False
  MainForm_EnableApiMenu 463&, False
  MainForm_EnableApiMenu 445&, False
  
  Call MainForm_XMenuRefresher '(False)
  
  DoEvents



  If xAutoPlayback And Not (xBool2 Or xPStateIconPlaying) Or Not InStr(CommandX, "/PLAY") = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Starting playback"
      #End If
    MainForm_LockMe False
    MainForm_xPlayClick False
    MainForm_LockMe True
    End If

  If IsIconic(MainHWnd) Then
    If xPStateIconPlaying Then
      If xTrayIconEnabled Then
        ShowWindow MainHWnd, 7&
        ShowWindow MainHWnd, 0&
        xTrayIconIndicator = True
        End If
      Else
      Call ShowWindow(MainHWnd, 9&)
      DoEvents
      If xTrayIconIndicator Then
        xTrayIconIndicator = False
        MainForm_ShowTray True
        Else
        Call SetForegroundWindow(MainHWnd)
        Call SetWindowPos(MainHWnd, -1&, 0&, 0&, 0&, 0&, &H3&)
        Call SetWindowPos(MainHWnd, -2&, 0&, 0&, 0&, 0&, &H3&)
        DoEvents
        Call SetForegroundWindow(MainHWnd)
        Call ShowWindow(MainHWnd, 9&)
        End If
      End If
    End If

  DoEvents
  If Not PLCurItem = 0 Then xMarkSong PLCurItem, False, False
  DoEvents
  MainForm_UpdateMainIcon
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Starting timers / spectrum"
    #End If
  
  Call SetTimer(MainHWnd, 1&, GUIInterval, AddressOf eTimerEvent)
  xFindNtfy = FindFirstChangeNotification(vPLXMain, 0&, 1&)
  'Call SetTimer(MainHWnd, 4&, 100&, AddressOf TearDown)
  'xTimerLoaded = xTimerLoaded Or &H1&
  xTimerIsEnabled = True


  If xNoSpektrumOnStartup Then xBool1 = False
  If ctSSLayer_CanUseItem(4&) Then
    If xBool1 Then
    'If xBool1 And Not xNoSpektrumOnStartup Then
      'If Not vKioskMode Then
        CheckApiMenu 302&, True
      '  End If
      xShowSpectrum = True
      SetPanelSize
      'If vKioskMode Then Form_Resize
      If LEDMode = 0& Then
        Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Spectrum), 5&)
        End If
      MainForm_DesireSpectrumState True, True
      Else
      CheckApiMenu 302&, False
      End If
    Else
    MainForm_EnableApiMenu 302&, False
    End If
  
  'SetPanelSize

  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Performing misc options"
    #End If
  
  If FileExists(xRealBaseFile + "chm") Then
    xLong1 = LoadLibrary("hhctrl.ocx")
    If Not xLong1 = 0& Then
      xUseChmHelp = True
      Call FreeLibrary(xLong1)
      End If
    End If
  
  If Not (xSecureMode Or vKioskMode Or FirstTimeHelp) And xCmdSaveSet Then
    If InStr(CommandX, "/NOTUTORIAL") = 0& Then
      If xUseChmHelp Then
        If HtmlHelp(MainHWnd, xRealBaseFile + "chm", 0&, "welcome.htm") = 0& Then
          xUseChmHelp = False
          GoTo TryHlp
          End If
        FirstTimeHelp = True
        Else
TryHlp:
        If FileExists(xRealBaseFile + "hlp") Then
          Call WinHelp(MainHWnd, xRealBaseFile + "hlp", &H101&, "Welcome")
          FirstTimeHelp = True
          End If
        End If
      Else
      FirstTimeHelp = True
      End If
    End If
  
  xLong1 = InStr(CommandX, "/LOADTIMER")
  If Not (xNoTimers Or xLong1 = 0&) Then
    xChar1 = Mid(CommandX + " ", xLong1 + 10)
    Select Case Left(xChar1, 1)
      Case ":", "="
        xChar1 = Trim(Mid(xChar1, 2))
        If Left(xChar1, 1) = Chr(34&) Then
          xChar1 = Mid(xChar1, 2)
          xLong1 = InStr(xChar1, Chr(34&))
          Else
          xLong1 = InStr(xChar1 + " ", " ")
          End If
        If Not xLong1 = 0 Then
          xChar1 = Left(xChar1, xLong1 - 1)
          End If
        MainForm_LoadTimers MainHWnd, xChar1, False
      Case " "
        MainForm_LoadTimers MainHWnd, AppBase("cpt"), False
      End Select
    End If
  
  
  
'...      ShowInfo
  
  xPStateIconStartup = False
  Call DoAnimateReadDir(0&)
  
  MainForm_UpdateMainIcon
  MainForm_LockMe False
 
  If Not ctSSLayer_CanUseItem(&H20&) = 0& And xCmdSaveSet And Not RestoreRegOnlyNow Then
    If Not LCase(ctSSLayer_GetInfoString(4&)) = xNoVerWarnDLL Then
      ShowInitInfo
      End If
    End If
 
'  On Error GoTo 0
  
  If (PLCount = 0& And CurMusic = ctSSLayer_MusicTypeNone) And Not vKioskMode Then
    Select Case xOnEmptyPLStartup
      Case 1 'loadpl
        If Not xSecureMode Then
          MenuEventFile 7&
          End If
      Case 2& 'browsedir
        If Not xSecureMode Then
          MenuEventFile 1&
          End If
      Case 3& 'autodirdialog (always)
        If Not (xNoAutoSetup Or xNoAutoRefreshByUser) Then
          ShowAutoRefresh
          End If
      Case 4& 'autodirdialog (ifset)
        If Not (xAutoDirCount = 0& Or xNoAutoSetup Or xNoAutoRefreshByUser) Then
          ShowAutoRefresh
          End If
      Case 5& 'autodirdialog (ifset or browsedir)
        If xAutoDirCount = 0& Or xNoAutoSetup Or xNoAutoRefreshByUser Then
          If Not xSecureMode Then
            MenuEventFile 1&
            End If
          Else
          ShowAutoRefresh
          End If
      Case 6& ' inet radio dialog
        If Not vKioskMode Then
          If Not InterfaceMode = Interface_Mode_Radio Then
            ToggleInterface Interface_Mode_Radio
            End If
          End If
      End Select
    End If
  
  Exit Sub




HandleSett:
  Do
    xLong1 = InStr(xChar1, ",")
    If xLong1 = 0& Then Exit Do
    xChar2 = Left(xChar1, xLong1 - 1&)
    Select Case xChar2
    'sett
      Case "sng": xRep = 0&: MainForm_ApplyRepMode
      Case "all": xRep = 1&: MainForm_ApplyRepMode
      Case "rnd": xRep = 2&: MainForm_ApplyRepMode
      
      Case "ap0":
        If Not vKioskMode Then
          xAutoPlayback = False
          xReAutoPlay = False
          End If
      Case "ap1"
        If Not vKioskMode Then
          xAutoPlayback = True
          xReAutoPlay = True
          End If
      
      Case "fd0": xGenFading = False
      Case "fd1": xGenFading = True
      
      Case "nop": xProcessMode = 0&: MainForm_ApplyProcessMode
      Case "crf": xProcessMode = 1&: MainForm_ApplyProcessMode
      Case "mdm": xProcessMode = 2&: MainForm_ApplyProcessMode
      Case "stp": xProcessMode = 3&: MainForm_ApplyProcessMode
      
      Case "pds": PlDClickAction = 0&
      Case "pde": PlDClickAction = 1&
      Case "pdn": PlDClickAction = 2&
      
      Case "rp0": xRemovePlayedSongs = False
      Case "rp1": xRemovePlayedSongs = True
      Case "rs0": xRemoveSkippedSongs = False
      Case "rs1": xRemoveSkippedSongs = True
      Case "rm0": xRemoveMissingSongs = False
      Case "rm1": xRemoveMissingSongs = True
      
      ' 3 kiosk extra settings:
      'Case "cdm": If vKioskMode Then xBool3 = True
      'Case "msg": If vKioskMode Then xIRMode = 2&
      'Case "vol": If vKioskMode Then vCanVolume = True
      
      Case Else
        If Left(xChar2, 3&) = "vol" Then
          xLong2 = Val(Mid(xChar2, 4&))
          If xLong2 > 0& And Not xLong2 > 100& Or xLong2 = 0& And xChar2 = "vol0" Then
            DoVolume xLong2 * 2.55, True
            End If
          End If
        If Left(xChar2, 3&) = "drl" Then
          xLong2 = Val(Mid(xChar2, 4&))
          If xLong2 = 0& And xChar2 = "drl0" Then
            xDirList = -Abs(xDirList)
            ElseIf Not (xLong2 < xDirListMin And Not xLong2 > xDirListMax) Then
            xDirList = xLong2
            End If
          End If
      End Select
    xChar1 = Mid(xChar1, xLong1 + 1&)
    Loop
  Return
  End Sub

#If RandomizerTest Then
  Private Function MainForm_TestColorFromByte(ByVal xByte As Byte) As Long
    TestColorFromByte = CLng(xByte)
    Select Case TestColorFromByte
      Case 1&: TestColorFromByte = &HE0FFEF
      Case 2&: TestColorFromByte = &HC0FFCF
      Case 3&: TestColorFromByte = &HA0FFAF
      Case 4&: TestColorFromByte = &H80FF8F
      Case 5&: TestColorFromByte = &H60FF6F
      Case 6&: TestColorFromByte = &H40FF4F
      Case 7&: TestColorFromByte = &H20FF2F
      Case 8&: TestColorFromByte = &HFF00&
      
      Case 9&: TestColorFromByte = &HFF20&
      Case 10&: TestColorFromByte = &HFF40&
      Case 11&: TestColorFromByte = &HFF60&
      Case 12&: TestColorFromByte = &HFF80&
      Case 13&: TestColorFromByte = &HFFA0&
      Case 14&: TestColorFromByte = &HFFC0&
      Case 15&: TestColorFromByte = &HFFE0&
      Case 16&: TestColorFromByte = &HFFFF&
      
      Case 17&: TestColorFromByte = &HE0FF&
      Case 18&: TestColorFromByte = &HC0FF&
      Case 19&: TestColorFromByte = &HA0FF&
      Case 20&: TestColorFromByte = &H80FF&
      Case 21&: TestColorFromByte = &H60FF&
      Case 22&: TestColorFromByte = &H40FF&
      Case 23&: TestColorFromByte = &H20FF&
      Case 24&: TestColorFromByte = &HFF&
    
      Case 25&: TestColorFromByte = &HE0&
      Case 26&: TestColorFromByte = &HC0&
      Case 27&: TestColorFromByte = &HA0&
      Case 28&: TestColorFromByte = &H80&
      Case 29&: TestColorFromByte = &H60&
      Case 30&: TestColorFromByte = &H40&
      Case 31&: TestColorFromByte = &H20&
      Case Is > 32&: TestColorFromByte = &H0&
      End Select
    End Function
  #End If

' Works with max &h7ffffffff (unsigned / >=0)
Private Function MemToStr(ByRef ptrMem As Long) As String
  Dim xLong1 As Long
  
  xLong1 = PeekL(ptrMem)
  Call UIntAddUInt(ptrMem, 4&)
  MemToStr = String(xLong1, vbNullChar)
  xLong1 = xLong1 * 2&
  Call RtlMoveMemory(ByVal StrPtr(MemToStr), ByVal ptrMem, xLong1)
  Call UIntAddUInt(ptrMem, xLong1)
  End Function

Private Sub MainForm_DelayedDragDrop()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xChar1 As String
  'Dim xChar2 As String
  Dim FileCount As Long
  Dim Shift As Long
  Dim OLEDropMemoryCopy As Long
  Dim xCharA1() As String
  Dim xLong5 As Long
  
  ReDim xCharA1(sCharA1MaxX)
  'Dim xChar2 As String
  
  
  'Me.Visible = False
  'DoEvents
  
  If Not (xAutoDoing Or PlayerIsLocked) Then
    If Not OLEDropMemory = 0& Then
      FileCount = PeekL(OLEDropMemory)
      OLEDropMemoryCopy = OLEDropMemory
      Call UIntAddUInt(OLEDropMemoryCopy, 4&)

      Shift = PeekL(OLEDropMemoryCopy)
      Call UIntAddUInt(OLEDropMemoryCopy, 4&)
      
      SetAutoDoing True
      xLong3 = PLCount
  
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Analyzing OLE Drag Drop"
        #End If
      xChar1 = MemToStr(OLEDropMemoryCopy)
      If FileCount = 1& Then
        Select Case AllowedItem(xChar1, True)
          Case 253&
            MainForm_LoadSettings xChar1
            SetAutoDoing False
            GoTo Rs1
          Case 254&
            MainForm_LoadTimers MainHWnd, xChar1, False
            SetAutoDoing False
            GoTo Rs1
          Case 255&
            'If AutoRefreshVisible Then
            '  Call SetWindowPos(frmAutoRefresh.hwnd, 0&, 0&, 0&, 0&, 0&, &H3&)
            '  Else
              If Not (PLCount = 0& And xAutoDirCount = 0& And xRadioCount = 0&) Then
                If Not LCase(AppBase(DefaultPLExtension)) = LCase(xChar1) Then
                  SavePList AppBase(DefaultPLExtension), False
                  End If
                End If
              'SetAutoDoing False
              xAutoDoing = False
              Call LoadPList(xChar1, False, False) ', False)
              CheckReplay
              GoTo Rs1
            '  End If
          End Select
        End If
      
      If PLCount < PLCountMaxX Then
        xPStateIconRefreshing = True
        MainForm_UpdateMainIcon
        
        If Shift = 0& Then
          Shift = 1901&
          Else
          Shift = GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(1), 0&)
          If Shift = 0& Then
            xPStateIconRefreshing = False
            MainForm_UpdateMainIcon
            SetAutoDoing False
            GoTo Rs1
            End If
          End If
        
        
        For xLong1 = 1& To FileCount
          If Not xLong1 = 1& Then
            xChar1 = MemToStr(OLEDropMemoryCopy)
            End If
          xLong2 = AllowedItem(xChar1, True)
          Select Case xLong2
            Case 0&
              If xLong5 < sCharA1MaxX Then
                If Len(xChar1) = 3& And Mid(xChar1, 2&) = ":\" Then xChar1 = Left(xChar1, 2&)
                xCharA1(xLong5) = xChar1
                xLong5 = xLong5 + 1&
                End If
            Case 1&
              If Not PLCount = PLCountMax Then
                sCharA1(xLong4) = xChar1
                xLong4 = xLong4 + 1&
                End If
            End Select
          Next
        If Not xLong4 = 0& Then
          InitResearchClear
          GenCalaymStructure True, xLong4, xSorting
          ElseIf Not xLong5 = 0& Then
          InitResearchClear
          End If
        If Not xLong5 = 0& Then
          If PLCount < PLCountMaxX Then
            MainForm_LockMe True
            AbortReading = False
            For xLong4 = 0& To xLong5 - 1&
              ReadDirectory xCharA1(xLong4), 0&, True, 0&
              If AbortReading Then Exit For 'CheckEmptyNodes True, xLong1: Exit For
              If PLCount = PLCountMax Then Exit For
              Next
            MainForm_LockMe False
            End If
          End If
        Else
        SetAutoDoing False
        GoTo Rs1
        End If
      
Rs2:
      If xLong3 = PLCount - 1& Then
        'PLCount = xLong4
        xPStateIconRefreshing = False
        MainForm_UpdateMainIcon
        SetAutoDoing False
        Else
        
        RealPLAdd xLong3
        
        xPStateIconRefreshing = False
        MainForm_UpdateMainIcon
        SetAutoDoing False
        
        If Not (xLong4 = 0& And xLong5 = 0&) Then
          'SetAutoDoing True
          InitResearch False
          'SetAutoDoing False
          End If
        
        'xBuildDirs
        CheckReplay
        End If
      
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Postprocessing OLE Drag Drop"
        #End If
        
      If Not (Shift = 0& Or Shift = 1900& Or xLong3 = PLCount) Then
        'If Not Button = 1900& Then
          xLong1 = Shift - 1900&
          Do
            If xLong3 = PLCount Then Exit Sub
            #If BoldDirs = 98 Then
              If (PLI(xLong3).DirDepthFlags And PLI_IsDir) = 0& Then Exit Do
              #Else
              If (PLIDF(xLong3) And PLI_IsDir) = 0& Then Exit Do
              #End If
            xLong3 = xLong3 + 1&
            Loop
          xMarkSong xLong3, False, False
          'xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
          If xLong1 = 2& Then
            MainForm_LockMe True
            MainForm_xEndSong Not xPStateIconPaused, False, False
            MainForm_LockMe False
            PLCurItem = xLong3
            MainForm_xPlayClick False
            ElseIf xLong1 = 3& Or xLong1 = 4& Then
            PLNext = xLong3
            #If BoldDirs = 98 Then
              PlNextSong = PLI(xLong3).Filename
              PlNextSongCap = PLI(xLong3).Caption
              #Else
              PlNextSong = PLIFile(xLong3)
              PlNextSongCap = PLICap(xLong3)
              #End If
            SetNextSong True
            If xLong1 = 3& Then MainForm_CheckAfter True, False, True, False 'to prevent waitforendsong when xfading
            End If
          'End If
        End If
Rs1:
'        On Error GoTo 0
      'OLEDropMemoryCopy = 0&
      End If
    End If
  
  If Not OLEDropMemory = 0& Then
    Call LocalFree(OLEDropMemory)
    OLEDropMemory = 0&
    End If
  
  'Me.Visible = False
  'DoEvents
'  Exit Sub
'
'Er2:
'  Resume Rs2
  End Sub

Private Function FloodSelected(ByVal FilesOnly As Boolean) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xChar1 As String
  'Dim xChar2 As String
  Dim xChar4 As String
  
  Select Case SendMessage(xListHWnd(2), &H190&, 0&, 0&)
    Case 0&
    Case 1&
      'FloodSelected = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
      FloodSelected = SendMessage(xListHWnd(2), &H19D&, 0&, 0&) 'anchor not caret
      #If BoldDirs = 98 Then
        xChar1 = PLI(FloodSelected).Filename
        #Else
        xChar1 = PLIFile(FloodSelected)
        #End If
  '    If Not IsADir(FloodSelected) And Mid(xChar1, 2&, 1&) = ":" Then
      If Mid(xChar1, 2&, 1&) = ":" Or Left(xChar1, 2&) = "\\" Then
        'sCharA1(1) = xChar1
        'sCharA1(0) = Mid(xChar1, InStrRev(xChar1, "\") + 1&)
        sCharA1(0) = xChar1
        FloodSelected = 1&
        Else
        FloodSelected = 0&
        End If
    Case Else
      If FilesOnly Then
        For xLong1 = 0& To PLCount - 1&
          If SendMessage(xListHWnd(2), &H187&, xLong1, 0&) Then
#If BoldDirs = 98 Then
            If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& And (Mid(PLI(xLong1).Filename, 2&, 1&) = ":" Or Left(PLI(xLong1).Filename, 2&) = "\\") Then
#Else
            If (PLIDF(xLong1) And PLI_IsDir) = 0& And (Mid(PLIFile(xLong1), 2&, 1&) = ":" Or Left(PLIFile(xLong1), 2&) = "\\") Then
#End If
              'xChar1 = Mid(PLItem(xLong1), 3&)
              'xLong2 = FloodSelected * 2&
              'sCharA1(xLong2 + 1&) = xChar1
#If BoldDirs = 98 Then
              sCharA1(FloodSelected) = PLI(xLong1).Filename
#Else
              sCharA1(FloodSelected) = PLIFile(xLong1)
#End If
              FloodSelected = FloodSelected + 1&
              End If
            End If
          Next
        Else
        For xLong1 = 0& To PLCount - 1&
          If SendMessage(xListHWnd(2), &H187&, xLong1, 0&) Then
            #If BoldDirs = 98 Then
              xChar1 = PLI(xLong1).Filename
              xLong2 = (PLI(xLong1).DirDepthFlags And PLI_IsDir)
              #Else
              xChar1 = PLIFile(xLong1)
              xLong2 = (PLIDF(xLong1) And PLI_IsDir)
              #End If
            'xChar2 = UCase(xChar1)
            If Mid(xChar1, 2&, 1&) = ":" Or Left(xChar1, 2&) = "\\" Then
              ' isasong or isadirectory
              xLong3 = InStrRev(xChar1, "\")
              If xLong3 = 0& Then
                xChar4 = ""
                Else
                xChar4 = Left(xChar1, xLong3 - 1&)
                End If
              xLong3 = InStrRev(xChar1, "\")
              If xLong3 = 0& Then
                xChar1 = ""
                Else
                xChar4 = xChar1 & "\"
                If xLong2 Then
                  xLong3 = Len(xChar4)
                  For xLong2 = FloodSelected - 1& To 0& Step -1&
                    'xLong4 = xLong2 * 2&
                    If sCharA1(xLong2) = xChar1 Or Left(sCharA1(xLong2), xLong3) = xChar4 Or sCharA1(xLong2) & "\" = Left(xChar4, Len(sCharA1(xLong2)) + 1&) Then
                      If sCharA1(xLong2) = xChar1 Or sCharA1(xLong2) & "\" = Left(xChar4, Len(sCharA1(xLong2)) + 1&) Then
                        xChar1 = ""
                        Exit For
                        Else
                        For xLong4 = xLong2 To FloodSelected - 2&
                          sCharA1(xLong4) = sCharA1(xLong4 + 1&)
                          Next
                        FloodSelected = FloodSelected - 1&
                        End If
                      End If
                    Next
                  Else
                  For xLong2 = 0& To FloodSelected - 1&
                    If sCharA1(xLong2) = xChar1 Or sCharA1(xLong2) & "\" = Left(xChar4, Len(sCharA1(xLong2)) + 1&) Then
                      xChar1 = ""
                      Exit For
                      End If
                    Next
                  End If
                End If
              If Not xChar1 = "" Then
                'xLong2 = FloodSelected * 2&
                sCharA1(FloodSelected) = xChar1
                'sCharA1(xLong2 + 1&) = xChar1
                FloodSelected = FloodSelected + 1&
                End If
              End If
            End If
          Next
        End If
    End Select
  End Function

Private Sub MenuEventCDDrive(ByVal CDDriveLetters As String, ByVal ReAdd As Boolean, ByVal RemoveNonRF As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xCaret As Long
  Dim xCaretOld As Long
  
  If Not (ReAdd And (xAutoDoing Or PlayerIsLocked)) Then
    MainForm_LockMe True
    SetAutoDoing True
    xPStateIconRefreshing = True
    MainForm_UpdateMainIcon
    DoEvents
    If Not LockWinUpd = 0& Then
      LockWindowUpdate xListHWnd(2)
      End If
    xCaret = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
    xCaretOld = xCaret
    InitResearchClear

    Do While Not xLong1 = PLCount
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_Depth) = 0& Then
'Debug.Print Hex(PLI(xLong1).DirDepthFlags), PLI(xLong1).Filename
        If Len(PLI(xLong1).Filename) = 2& Or Not ReAdd Then
'Debug.Print "IsRootOk"
          If RemoveNonRF Or (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = PLI_IsAuto Then
'Debug.Print "ShouldRemoveOk1"
            If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir And Mid(PLI(xLong1).Filename, 2&, 1&) = ":" And Not InStr(CDDriveLetters, UCase(Left(PLI(xLong1).Filename, 1&))) = 0& Then ' Mid(PLItem(xLong1), 3&) = CDDriveLetters Then
'Debug.Print "ShouldRemoveOk2"
#Else
      If (PLI(xLong1).DirDepthFlags And PLI_Depth) = 0& Then
        If Len(PLIFile(xLong1)) = 2& Or Not ReAdd Then
          If RemoveNonRF Or (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = PLI_IsAuto Then
            If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir And Mid(PLI(xLong1).Filename, 2&, 1&) And Not InStr(CDDriveLetters, UCase(Left(PLIFile(xLong1), 1&))) = 0& Then ' Mid(PLItem(xLong1), 3&) = CDDriveLetters Then
#End If
              Do
                'RemoveListItemMS
                RemoveFromSearch xLong2
                'PLRemoveShifted1 = xLong2
                'PLRemoveShifted2 = xLong1
                Call SendMessage(xListHWnd(2), &H182&, xLong2, 0&)
                If xCaret >= xLong2 Then xCaret = xCaret - 1&
                xLong1 = xLong1 + 1&
                If xLong1 Mod 128& = 0& Then
                  If LockWinUpd = 0& Then
                    DoEventsEx_SleepPeriodical
                    ElseIf LockWinUpd = 1& Then
                    DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                    End If
                  End If
                If Not (PLCurItem = 0& Or PLCurItem < xLong1) Then PLCurItem = PLCurItem - 1&
                If Not PlNextSong = "" And PLNext = xLong1 Then SetNextSong False
#If BoldDirs = 98 Then
                Loop Until (xLong1 = PLCount Or (PLI(xLong1).DirDepthFlags And PLI_Depth) = 0&)
              Else
              GoTo KeepOldInfo
              End If
            Else
            GoTo KeepOldInfo
            End If
#Else
                Loop Until (xLong1 = PLCount Or (PLIDF(xLong1) And PLI_Depth) = 0&)
              Else
              GoTo KeepOldInfo
              End If
            Else
            GoTo KeepOldInfo
            End If
#End If
          'PLRemoveShifted1 = &H7FFFFFFF
          Else
          GoTo KeepOldInfo
          End If
        Else
KeepOldInfo:
        If PLCurItem = xLong1 Then PLCurItem = xLong2
        If Not PlNextSong = "" And PLNext = xLong1 Then
          PLNext = xLong2
          End If
          #If BoldDirs = 98 Then
          PLI(xLong2) = PLI(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, xLong2, xLong2)
          #Else
          PLIFile(xLong2) = PLIFile(xLong1)
          PLICap(xLong2) = PLICap(xLong1)
          PLIDF(xLong2) = PLIDF(xLong1)
          PLIPxLg(xLong2) = PLIPxLg(xLong1)
          PLIPxSm(xLong2) = PLIPxSm(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, xLong2, xLong2)
          #End If
        xLong2 = xLong2 + 1&
        xLong1 = xLong1 + 1&
        End If
      Loop
    
    If Not (PLCount = 0& Or xCaret = xCaretOld) Then
      If xCaret > PLCount - 1& Then xCaret = PLCount - 1&
      If xCaret < 0& Then xCaret = 0&
      Call SendMessage(xListHWnd(2), &H19E&, xCaret, 0&)
      MainForm_CheckDir False
      End If
    
    If Not LockWinUpd = 0& Then
      LockWindowUpdate 0&
      End If
    PLCount = xLong2
      
    AbortReading = False
    If ReAdd Then 'And Not PLCount < PLCountMaxX Then
      'InitResearchClear
      For xLong1 = 1& To Len(CDDriveLetters)
        If PLCount < PLCountMaxX Then
          ReadDirectory Mid(CDDriveLetters, xLong1, 1&) & ":", 0&, True, 0&
          Else
          Exit For
          End If
        Next
      MainForm_LockMe False
      xPStateIconRefreshing = False
      MainForm_UpdateMainIcon
      RealPLAdd xLong2
'      MainForm_xBuildDirs
      SetAutoDoing False
      InitResearch False
      CheckReplay
      Else
      xPStateIconRefreshing = False
      InitResearch False
      MainForm_xBuildDirs
      'SetAutoDoing False
      End If
    End If
  End Sub

Public Sub ChangeVolumeMenu(ByVal Index As Long)
  Dim xLong1 As Long
  
  If Not PlayerIsLocked Then
    Select Case Index
      Case 0 'VolUp
        xLong1 = xSetVolume + 10&
      Case 1 'VolDown
        xLong1 = xSetVolume - 10&
      Case 3 To 13
        xLong1 = CLng(CInt((Index - 3) * 25.5))
      End Select
  
    If xLong1 > 255& Then
      xLong1 = 255&
      ElseIf xLong1 < 0& Then
      xLong1 = 0&
      End If
    DoVolume xLong1, True
    End If
  End Sub

Private Sub MenuEventFile(ByVal Index As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  Dim xType1 As OPENFILENAME
  'Dim xDC As Long
  'Dim xFont As Long

  If Not (PlayerIsLocked Or xAutoDoing) Then
    Select Case Index
      Case 0& 'addfiles
        If Not PLCount = PLCountMax Then
          xType1.hWndOwner = MainHWnd
          If xUsedTypesResort Then MainForm_ResortTypes xUsedTypes
          xType1.Filter = _
                   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
          xType1.Title = LoadResEx_String(1200&)
          xType1.InitialDirFile = xLastDirectory
          DialogTimer True
          xChar2 = GetOpenFileName_MultiDataString(xType1)
          DialogTimer False
          If Not (xChar2 = "" Or xAutoDoing Or PlayerIsLocked Or PLCount = PLCountMax) Then
            xLong2 = InStr(xChar2, vbNullChar)
            If InStr(xChar2, vbNullChar + vbNullChar) = xLong2 Then
              xChar2 = Left(xChar2, xLong2 - 1&)
              xLong2 = InStrRev(xChar2, "\")
              xChar3 = Left(xChar2, xLong2 - 1&)
              xChar2 = Mid(xChar2, xLong2 + 1&) & vbNullChar
              Else
              xLong2 = InStr(xChar2 & vbNullChar, vbNullChar)
              xChar3 = Left(xChar2, xLong2 - 1&)
              If Len(xChar3) = 3& Then xChar3 = Left(xChar3, 2&)
              xChar2 = Mid(xChar2, xLong2 + 1&)
              End If
            #If BoldDirs = 98 Then
              PLI(PLCount).Filename = xChar3
              PLI(PLCount).Caption = xChar3
              PLI(PLCount).DirDepthFlags = PLI_IsDir ' or 0&
              #Else
              PLIFile(PLCount) = xChar3
              PLICap(PLCount) = xChar3
              PLIDF(PLCount) = PLI_IsDir ' or 0&
              #End If
            PLCount = PLCount + 1&
            xLastDirectory = xChar3
            xChar3 = xChar3 & "\"
            xLong2 = 0&
            Do
              xLong1 = InStr(xChar2, vbNullChar)
              If xLong1 < 2& Then
                Exit Do
                Else
                xChar1 = Left(xChar2, xLong1 - 1&)
                xChar2 = Mid(xChar2, xLong1 + 1&)
                If AllowedItem(xChar1, False) = 1& Then
                  sCharA1(xLong2) = xChar1
                  xLong2 = xLong2 + 1&
                  If xLong2 = sCharA1Max Or PLCount + xLong2 = PLCountMax Then Exit Do
                  End If
                End If
              Loop
            If xLong2 = 0& Then
              PLCount = PLCount - 1&
              Else
              xLong3 = PLCount - 1&
              InitResearchClear
              If xSorting Then
                Call ArraySort_String_IC(sCharA1(), xLong2)
                End If
              'If Not LockWinUpd = 0& Then
              '  LockWindowUpdate xListHWnd(2)
              '  End If
              'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount - 1&)

              For xLong1 = 0& To xLong2 - 1&
                #If BoldDirs = 98 Then
                  PLI(PLCount).Filename = xChar3 & sCharA1(xLong1)
                  PLI(PLCount).Caption = sCharA1(xLong1)
                  PLI(PLCount).DirDepthFlags = 1&
                  'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
                  #Else
                  PLIFile(PLCount) = xChar3 & sCharA1(xLong1)
                  PLICap(PLCount) = sCharA1(xLong1)
                  PLIDF(PLCount) = 1&
                  'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
                  #End If
                PLCount = PLCount + 1&
                'If xLong1 Mod 128& = 0& Then
                '  If LockWinUpd = 0& Then
                '    DoEventsEx_SleepPeriodical
                '    ElseIf LockWinUpd = 1& Then
                '    DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                '    End If
                '  End If
                Next
              'If Not LockWinUpd = 0& Then
              '  LockWindowUpdate 0&
              '  End If
              If Not xLong3 = PLCount Then
                RealPLAdd xLong3
                End If
              'MainForm_xBuildDirs
              'SetAutoDoing True
              InitResearch False
              'SetAutoDoing False
              
              xMarkSong xLong3, False, False
              CheckReplay
              End If
            End If
          End If
      Case 1& 'adddir
        If PLCount < PLCountMaxX Then
          'SetAutoDoing True
          DialogTimer True
          xChar1 = CommonDLG_OpenPath(MainHWnd, LoadResEx_String(1210&), xLastDir)
          DialogTimer False
          'If Not xChar1 = "" Then
          If Not (xChar1 = "" Or xAutoDoing Or PlayerIsLocked Or PLCount = PLCountMax) Then
            SetAutoDoing True
            AddFDirNowSub xChar1
            End If
          SetAutoDoing False
          CheckReplay
          End If
      Case 6& 'new
        PLCount = 0&
        xRadioCount = 0&
        RadioForm_PrePreset
        PLCurItem = 0&
        ClearOverload
        If Not PlNextSong = "" Then SetNextSong False
        ClearPLEx
        xAutoDirCount = 0&
        MainForm_xBuildDirs
        MainForm_EnableApiMenu 411&, False
        MainForm_EnableApiMenu 412&, False
        MainForm_EnableApiMenu 413&, False
        MainForm_EnableApiMenu 414&, False
        xCurPlistFilename = AppBase(DefaultPLExtension)
        xRadioCount = 0&
        RadioForm_PrePreset
      Case 2&, 7& 'addpl / load
        'If Not (PLCount = 0 And xAutoDirCount = 0) Then
        'If AutoRefreshVisible Then
         ' ShowVBWindowX frmAutoRefresh, AutoRefreshVisible
        If Not xAutoDoing And (PLCount < PLCountMaxX Or Index = 7) Then
          xChar1 = CalaymLoadSingleFile(GetPLTypes(False), LoadResEx_String(1220&), m3uList, MainHWnd, True)
          If Not xChar1 = "" Then
            m3uList = SimpleDir(xChar1)
            If AllowedItem(xChar1, True) = 255& Then
              If Not ((PLCount = 0& And xAutoDirCount = 0& And xRadioCount = 0&) Or Not Index = 2& Or LCase(xChar1) = LCase(AppBase(DefaultPLExtension)) Or LCase(xChar1) = LCase(xCurPlistFilename)) Then
                SavePList AppBase(DefaultPLExtension), False
                End If
              If Not PlNextSong = "" Then SetNextSong False
              ''''''''''''''
              If Not Index = 2 Then
                PLCount = 0&
                xRadioCount = 0&
                RadioForm_PrePreset
                End If
              ''''''''''''''
              'xPStateIconRefreshing = True
              'ShowIcon 0&
              'DoEvents
              Call LoadPList(xChar1, Index = 2, False) ', False)
              'xPStateIconRefreshing = False
              'ShowIcon 0&
              End If
            End If
          End If
      Case 8& 'load default pl
        LoadPList AppBase(DefaultPLExtension), False, False
      Case 9& 'reload
        'If AutoRefreshVisible Then
        '  Call SetWindowPos(frmAutoRefresh.hwnd, 0&, 0&, 0&, 0&, 0&, &H3&)
        '  Else
          If Not PlNextSong = "" Then SetNextSong False
          PLCount = 0&
          xRadioCount = 0&
          RadioForm_PrePreset
          'xPStateIconRefreshing = True
          'ShowIcon 0&
          'DoEvents
'Stop
          Call LoadPList(xCurPlistFilename, False, False) ', False)
          'xPStateIconRefreshing = False
          'ShowIcon 0&
        '  End If
      Case 10& 'save
        If Not xNoSaveListMenu = 2& Then
          If xCurPlistFilenameSuggestNew Then GoTo AskNewFilename
          If Not SavePList(xCurPlistFilename, False) Then
            GoTo AskNewFilename
            End If
          End If
      Case 11& 'save as
        If Not xNoSaveListMenu = 2& Then
AskNewFilename:
          If xCurPlistFilenameSuggestNew Or LCase(xCurPlistFilename) = LCase(AppBase(DefaultPLExtension)) Then
            xChar1 = m3uList
            Else
            xChar1 = xCurPlistFilename
            End If
'Debug.Print xChar1
          xChar1 = CalaymSaveSingleFile(GetPLTypes(True), LoadResEx_String(1221&), xChar1, MainHWnd, True)
'Stop
          If Not xChar1 = "" Then
            m3uList = SimpleDir(xChar1)
            'If Not lcase(Right(xChar1, 4)) = "." + DefaultPLExtension Then
            '  xChar1 = xChar1 + ".m3u"
            '  End If
            xLong1 = AllowedItem(xChar1, False)
            If xLong1 = 255& Or xLong1 = -1& Then
              If SavePList(xChar1, True) Then
                xLong1 = InStrRev(xChar1, "\")
                xChar2 = Left(xChar1, xLong1)
                'xLong1 = Len(xChar2)
                End If
              End If
            End If
          End If
      Case 13& 'clear playlist
        'If Not xAutoDoing Then
          InitResearchClear
          If Not PlNextSong = "" Then SetNextSong False
          PLCurItem = 0&
          PLCount = 0&
          ClearOverload
          ClearPLEx
          InitResearch False
          'Erase PLItem, PLCap
          'xBuildDirs
          'End If
      End Select
    End If
  End Sub

Private Function AddFDirNowSubEx(ByVal xChar1 As String, ByVal ResetPL As Boolean) As Boolean
  SetAutoDoing True
  
  If ResetPL Then
    If Not PlNextSong = "" Then SetNextSong False
    PLCount = 0&
''''''''    aaa
    PLCurItem = 0&
    ClearOverload
    ClearPLEx
    End If
  
  If PLCount < PLCountMaxX Then
    AddFDirNowSubEx = AddFDirNowSub(xChar1)
    SetAutoDoing False
    If AddFDirNowSubEx Then InitResearch False
    CheckReplay
    Else
    SetAutoDoing False
    End If
  End Function

Private Function AddFDirNowSub(ByVal xChar1 As String) As Boolean
  Dim xLong1 As Long
  
  xLong1 = PLCount
  xLastDir = xChar1
  If Len(xChar1) = 3 Then xChar1 = Left(xChar1, 2&)
  xPStateIconRefreshing = True
  MainForm_UpdateMainIcon
  AbortReading = False
  MainForm_LockMe True
  DoEvents
  ReadDirectory xChar1, 0&, True, 0&
  If Not xLong1 = PLCount Then
    InitResearchClear
    RealPLAdd xLong1
    AddFDirNowSub = True
    End If
  MainForm_LockMe False
  If Not xLong1 = PLCount Then
    InitResearch False
    End If
  xPStateIconRefreshing = False
  MainForm_UpdateMainIcon
  End Function

Public Sub MainForm_OutputChange(ByVal hWndCaller As Long, ByVal FastInit As Boolean, ByVal UpdatePlayerIcon As Boolean)
  'Dim xLong3 As Long
  'Dim xBool1 As Boolean
  ''Dim xLong1 As Long
  'Dim xLong2 As Long
  
  'Do While (PlayerIsLocked Or xAutoDoing): DoEvents: Loop
  
  MainForm_LockMe True
  SetAutoDoing True
  If UpdatePlayerIcon Then
    xPStateIconSndcard = True
    MainForm_UpdateMainIcon
    End If
  MainForm_CheckDir True

  If FastInit Then
    If CurMusic = ctSSLayer_MusicTypeCDAudio Then
      'MainForm_ctConsoleA_AddLog "Mode 1"
      Call ResetSystem(False, False)
      Call Tear_WaitFor(2&)
      'MainForm_ctConsoleA_AddLog "wait"
      Call ctSSLayer_UpdateCDDA(CDDAUse, CDDAJitter)
      SetAutoDoing False
      Call ResetSystem(True, False)
      'MainForm_ctConsoleA_AddLog "done"
      Else
      'MainForm_ctConsoleA_AddLog "Mode 2"
      Call ResetSystem(False, True)
      'MainForm_ctConsoleA_AddLog "wait"
      Call Tear_WaitFor(2&)
      Call ctSSLayer_UpdateCDDA(CDDAUse, CDDAJitter)
      SetAutoDoing False
      Call ResetSystem(True, True)
      'MainForm_ctConsoleA_AddLog "done"
      End If
    Else
    Call ResetSystem(False, False)
    
    MainForm_DesireSpectrumState False, True
    If Not LEDMode = 0& Then
      LEDState = 2&
      End If
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  Closing sound system"
      #End If
  
    Call Tear_WaitFor(-2&)
    Call ctSSLayer_Close(0&)
    Call ctSSLayer_RefreshDeviceList(MainHWnd)
    xInitMe hWndCaller, False, False, False
    'if xInfoQuit then
    
    SetAutoDoing False
    Call ResetSystem(True, False)
    End If

  ' > LEDState = 0&
  If UpdatePlayerIcon Then
    xPStateIconSndcard = False
    MainForm_UpdateMainIcon
    End If
  
  DoEvents
  MainForm_LockMe False
  'SetAutoDoing False
  End Sub

#If PresetBinded Then
  Private Sub MenuEventPlaylistLoader(ByVal Index As Long)
    Dim xChar1 As String
    
    If Not (PlayerIsLocked Or xAutoDoing) Then
      If Index = 0 Then
        'save
        xChar1 = CalaymSaveSingleFile(LoadResEx_String(1655&) & vbNullChar & "*.cps", LoadResEx_String(1651&), PresetFile, MainHWnd, True)
        If Not xChar1 = "" Then
          If SavePreset(xChar1) Then
            PresetFile = SimpleDir(xChar1)
            End If
          End If
        Else
        'load; =2
        xChar1 = CalaymLoadSingleFile(LoadResEx_String(1655&) & vbNullChar & "*.cps" & vbNullChar & LoadResEx_String(1656&) & vbNullChar & "*.ini", LoadResEx_String(1650&), PresetFile, MainHWnd, True)
        If Not Len(xChar1) = 0& Then
          PresetFile = SimpleDir(xChar1)
          If AllowedItem(xChar1, True) = 253& Then
            Call MainForm_LoadSettings(xChar1)
            End If
          End If
        End If
      End If
    End Sub
  #End If

Public Sub SpektrumToggleEvent()
  'Dim xLong1 As Long

  If Not (PlayerIsLocked Or xAutoDoing) Then  ' Or vKioskMode) Then
    If xShowSpectrum Then
      xShowSpectrum = False
      MainForm_DesireSpectrumState False, True
      Else
      xShowSpectrum = True
      MainForm_DesireSpectrumState True, True
      End If
    If LEDMode = 0& Then
      Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Spectrum), -5& * CLng(xShowSpectrum))
      End If
    CheckApiMenu 302&, xShowSpectrum
    SetPanelSize
    End If
  End Sub

Public Sub MainForm_MenuEventPlaylist(ByVal Index As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String

  If Not (PlayerIsLocked Or xAutoDoing) Then
    Select Case Index
      Case 0& To 2&
        'If PLCount = 0 Or SendMessage(xListHWnd(2), &H190&, 0&, 0&) = 0 Or PlayerIsLocked Or xAutoDoing Then Exit Sub
        'If Not (PLCount = 0& Or SendMessage(xListHWnd(2), &H190&, 0&, 0&) = 0) Then
        '  If Asc(PLItem(SendMessage(xListHWnd(2), &H19F&, 0&, 0&))) = 1& Then
        If Not PLCount = 0& And GetListIndexMS(xListHWnd(2), xLong1) Then
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then
            If FileExists(PLI(xLong1).Filename) Then
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = 0& Then
            If FileExists(PLIFile(xLong1)) Then
#End If
              If Index = 2 And Not xPStateIconPlaying Then
                PLCurItem = xLong1 'SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
                Else
                If Not PlNextSong = "" Then SetNextSong False
                If xPStateIconPaused And Not Index = 2 Then
                  xPStateIconPlaying = False
                  xPStateIconPaused = False
                  xReAutoPlay = False
                  ReAutoPlayDR = False
                  MainForm_UpdateMainIcon
                  MainForm_LockMe True '''''''''''
                  MainForm_xEndSong False, False, False
                  MainForm_LockMe False ''''''''''''
                  If xRemoveSkippedSongs And Not (xNoRemove Or Not PlNextSong = "" And PLNext = PLCurItem) Then
                    MainForm_RemoverX False
                    xNoRemove = True
                    End If
                  End If
                If Not xPStateIconPlaying Then
                  PLCurItem = xLong1 'SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
                  MainForm_xPlayClick False
                  Else
                  'If (Index = 0 Or Index = 1 And Not xRemovePlayedSongs) And xRemoveSkippedSongs And xPStateIconPlaying And Not (xNoRemove Or PLCount = 0 Or Not PlNextSong = "" And PLNext = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)) Then
                  If (Index = 0 Or Index = 1 And Not xRemovePlayedSongs) And xRemoveSkippedSongs And xPStateIconPlaying And Not (xNoRemove Or PLCount = 0 Or Not PlNextSong = "" And PLNext = xLong1) Then
                    PlNextSong = Chr(255&)
                    PlNextSongCap = ""
                    PLNext = xLong1 'SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
                    MainForm_RemoverX False
                    PLCurItem = PLNext
                    PlNextSong = ""
                    Else
                    PLNext = xLong1 'SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
                    End If
                  Select Case Index
                    Case 0
                      PLCurItem = PLNext
                      MainForm_LockMe True
                      MainForm_xEndSong True, False, False
                      MainForm_LockMe False
                      MainForm_xPlayClick False
                    Case 1, 2
                      #If BoldDirs = 98 Then
                        PlNextSong = PLI(PLNext).Filename
                        PlNextSongCap = PLI(PLNext).Caption
                        #Else
                        PlNextSong = PLIFile(PLNext)
                        PlNextSongCap = PLICap(PLNext)
                        #End If
                      If Index = 1 Then
                        MainForm_CheckAfter True, True, True, False
                        Else
                        SetNextSong True
                        End If
                    End Select
                  End If
                End If
              Else
              CheckThisSong xLong1 'SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
              MainForm_LockMe False
              End If
            End If
          End If
      Case 4&, 400& 'find current
        xLong1 = xFindSong
        If Not GetListIndexMS(xListHWnd(2), xLong2) Then xLong2 = 0&
        'If SendMessage(xListHWnd(2), &H190&, 0&, 0&) = 1 Then
        '  xLong2 = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
        '  End If
        If Not (xLong1 = 0& Or xLong2 = xLong1) Then
          xMarkSong xLong1, False, False
          ElseIf Not Index = 400& Then
          If Not PlNextSong = "" Then
#If BoldDirs = 98 Then
            If PLI(PLNext).Filename = PlNextSong Then
#Else
            If PLIFile(PLNext) = PlNextSong Then
#End If
              xLong1 = PLNext
              Else
              xLong1 = xFindSong(PlNextSong)
              If Not xLong1 = 0& Then PLNext = xLong1
              End If
            If Not xLong1 = 0& Then
              xMarkSong xLong1, False, False
              End If
            Else
            If Not xLong1 = 0& Then
              Call SendMessage(xListHWnd(2), &H185&, 0&, xLong2)
              xMarkSong xLong2, False, False
              End If
            End If
          End If
      Case 5&, 6& 'find...
        If Not PLCount = 0& Then
          If Index = 6& And Not xFindNextString = "" Then
            If FindNext Then Exit Sub
            End If
          If CurrentFindHMem + CurrentFindHWnd = 0& Then
            If RegisteredFindMsgID = 0& Then
              RegisteredFindMsgID = RegisterWindowMessageA("commdlg_FindReplace")
              End If
            If Not RegisteredFindMsgID = 0& Then
              CurrentFindHMem = LocalAlloc(0&, 514&)
              If Not CurrentFindHMem = 0& Then
                Call RtlZeroMemory(CurrentFindType, 40&)
                CurrentFindType.lStructSize = 40&
                CurrentFindType.lpstrFindWhat = CurrentFindHMem
                'CurrentFindType.hInstance = App.hInstance
                CurrentFindType.wFindWhatLen = 514&
                'CurrentFindType.flags = &H1C000
                If CurrentFindInvert = -1& Then
                  'CurrentFindType.flags = &H10000
                  CurrentFindType.Flags = &H1000&
                  Else
                  'CurrentFindType.flags = &H10001
                  CurrentFindType.Flags = &H1001&
                  End If
                If CurrentFindMatchCase = vbBinaryCompare Then
                  CurrentFindType.Flags = CurrentFindType.Flags + 4&
                  End If
                  
                CurrentFindType.hWndOwner = MainHWnd
                xChar1 = xFindNextString + vbNullChar
                xLong1 = Len(xChar1)
                EnableWindow MainHWnd, 0&
                If IsUnicode Then
                  Call RtlMoveMemory(ByVal CurrentFindHMem, ByVal StrPtr(xChar1), xLong1 * 2&)
                  CurrentFindHWnd = FindTextW(CurrentFindType)
                  Else
                  Call RtlMoveMemory(ByVal CurrentFindHMem, ByVal xChar1, xLong1)
                  CurrentFindHWnd = FindTextA(CurrentFindType)
                  End If
                If CurrentFindHWnd = 0& Then
                  LocalFree CurrentFindHMem
                  CurrentFindHMem = 0&
                  EnableWindow MainHWnd, 1&
                  Else
                  DialogTimer True
                  End If
                End If
              End If
            'Do
            '  xChar1 = InputBox(LoadResEx_String(1441&), LoadResEx_String(1440&), xFindNextString)
            '  If Trim(xChar1) = "" Then
            '    Exit Do
            '    Else
            '    xFindNextString = xChar1
            '    If FindNext Then Exit Do
            '    End If
            ' Loop
           End If
         End If
      Case 13&, 14& 'move/merge at playlist end  /  merge in new playlist
        If PLCount > 1& And PLCount < PLCountMaxX And SendMessage(xListHWnd(2), &H190&, 0&, 0&) > 0& Then
          For xLong1 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
            If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
            If (PLIDF(xLong1) And PLI_IsDir) = 0& Then
#End If
              If SendMessage(xListHWnd(2), &H187&, xLong1, 0&) Then
#If BoldDirs = 98 Then
                sCharA1(xLong2) = PLI(xLong1).Filename
#Else
                sCharA1(xLong2) = PLIFile(xLong1)
#End If
'Debug.Print "Using: " + sCharA1(xLong2)
                xLong2 = xLong2 + 1&
                If xLong2 = sCharA1Max Then Exit For
                End If
              End If
            Next
          If Not xLong2 = 0& Then
            InitResearchClear
            'resetradiolist
            If Index = 14& Then
              PLCount = 0&
              PLCurItem = 0&
              If Not PlNextSong = "" Then SetNextSong False
              ClearPLEx
              End If
            GenCalaymStructure False, xLong2, xSorting
            'MainForm_xBuildDirs
            If Index = 14& Then
              CheckOldItems
              If Not PLCurItem = 0& Then
                xMarkSong PLCurItem, True, False
                End If
              End If
            InitResearch False
            End If
          End If
      End Select
    End If
  End Sub

Private Function xPlayItem(ByVal CanFade As Boolean, ByVal PlaySilent As Boolean, ByVal DoCrossFade As Boolean, ByVal NoSFXWait As Boolean, ByVal ResumePaused As Boolean, ByVal ManualURL As String) As Boolean
  'Dim xChar1 As String
  Dim xBool1 As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  #If BlockTest Then
    Dim xLong3 As Long
    #End If
  Dim PreJump As Long
  Static xLastOpenedLogged As String

  Dim TempMusic As Long
  'Dim xSongWillBeMidi As Long
  'Dim TempPointer As Long
  'Dim TempDrvByte As Byte
  Dim xFullMode As String
  Dim xCrossFadeTimeMN As Long
  Dim xCrossFadeTimeMT As Long
  Dim xCrossFadeTimeEN As Long
  
  Dim xBool2 As Boolean

  #If UseFileLogging Then
    If Not PlayerIsLocked Then
      MsgBox "Error 200510-2", vbCritical, "please report me"
      Stop
      'Me.Visible = False
      'DoEvents
      Exit Function
      End If
    #End If
  
  If Not xGenFading Then CanFade = False
  If ctSSLayer_MaxIDs = 2& Then CanFade = False
  'If ctSSLayer_MaxIDs < 5& Then DoCrossFade = False
  
  If Not Len(ManualURL) = 0& Then GoTo URLHack
  If PLCount = 0& Then
    xPlayItem = True
    If Not CurMusic = ctSSLayer_MusicTypeNone Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "!Stopped2"
        #End If
      MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem1(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
      xPStateIconPlaying = False
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem2(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
'      CurMusic = ctSSLayer_MusicTypeNone
      MainForm_UpdateMainIcon
      If Not PlNextSong = "" Then SetNextSong False
      End If
#If BoldDirs = 98 Then
    ElseIf (PLI(PLCurItem).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
    ElseIf (PLIDF(PLCurItem) And PLI_IsDir) = 0& Then
#End If
URLHack:
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem3(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
    If CurMusic = ctSSLayer_MusicTypeNone Then
      CanFade = False
      DoCrossFade = False
      ElseIf Not (DoCrossFade And xPStateIconPlaying) Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "!Stopped3"
        #End If
      MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False     'Not CanFade ' False '''''New001
      End If
    OldSongID = CurrentSongID
    xXFadeCurMusic = CurMusic
    xXFadeOldTime = PLCurSongLength
    DoEvents
    
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem4(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
    If xPStateIconPlaying Then
      #If UseFileLogging Then
        If Len(ManualURL) = 0& Then
          #If BoldDirs = 98 Then
            MainForm_ctConsoleA_AddLog "Playitem: " & PLI(PLCurItem).Filename
            #Else
            MainForm_ctConsoleA_AddLog "Playitem: " & PLIFile(PLCurItem)
            #End If
          Else
          MainForm_ctConsoleA_AddLog "PlayStream: " & RealURL(ManualURL)
          End If
        #End If
      
      xSongWillBeMidi = 0&
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem5(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
      
      If Len(ManualURL) = 0& Then
        #If BoldDirs = 98 Then
          xFullMode = GetExtension(PLI(PLCurItem).Filename)
          #Else
          xFullMode = GetExtension(PLIFile(PLCurItem))
          #End If
        If xFullMode = "cda" Then
          TempMusic = ctSSLayer_MusicTypeCDAudio
          xSongWillBeMidi = 2&
          ElseIf InStr(xSupportedFilesS, vbNullChar & xFullMode & vbNullChar) = 0& Then
          If Not InStr(xSupportedFilesM, vbNullChar & xFullMode & vbNullChar) = 0& And ctSSLayer_CanUseItem(&H9&) = 0& Then xSongWillBeMidi = 1&
          TempMusic = ctSSLayer_MusicTypeTracker
          Else
          TempMusic = ctSSLayer_MusicTypeStream
          End If
        If TempMusic = ctSSLayer_MusicTypeCDAudio Then
'          xChar1 = GetTrackNumber(Mid(PLItem(PLCurItem), 3&))
'          If Val(xChar1) = 0& Then
#If BoldDirs = 98 Then
          If Val(GetTrackNumber(PLI(PLCurItem).Filename)) = 0& Then
#Else
          If Val(GetTrackNumber(PLIFile(PLCurItem))) = 0& Then
#End If
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  invalid cd track id"
              #End If
            Exit Function
            End If
          'xChar1 = Mid(PLItem(PLCurItem), 3&, 1&) + xChar1
          'Else
          'xChar1 = Mid(PLItem(PLCurItem), 3&)
          End If
        Else
        TempMusic = ctSSLayer_MusicTypeStream
        'xChar1 = ManualURL
        xSongWillBeMidi = 3&
        End If


#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem6(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
      
      If xSongWillBeMidi = xSongIsMidi And DoCrossFade And Not xSongWillBeMidi = 0& Then
        If NoSFXWait Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "!Stopped1"
            #End If
          MainForm_xEndSong CanFade And Not xPStateIconPaused, True, False
          DoCrossFade = False
          Else
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  cannot crossfade from CD audio to CD audio, playing after current song"
            #End If
          EndSongEx
          xPlayItem = True
          Exit Function
          End If
        End If
      
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  loading & initializing..."
        #End If
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem7(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If







      If Len(ManualURL) = 0& Then
        xLoadingStream = 1&
        Else
        xLoadingStream = 2&
        xLoadURL = ManualURL
        End If
      MainForm_UpdateMainIcon
      If xClassicBlockingOnLoad Then DoEvents
      'UpdateAnimIcon






      Call Tear_WaitFor(xSongWillBeMidi)
'      Debug.Print "DoneWaiting"
      xLong2 = DoEventsEx_GetTickCount
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load1"
  #End If
      
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItem8(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
'Debug.Print NewSongID
      If Len(ManualURL) = 0& Then
        'xLoadingStream = 1&
        'MainForm_UpdateMainIcon
        ''UpdateAnimIcon
        #If BoldDirs = 98 Then
          SongPointer(NewSongID) = ctSSLayer_LoadSong(TempMusic, PLI(PLCurItem).Filename, -2& * (Not xClassicBlockingOnLoad) - CLng(CheckAccurate))
          #Else
          SongPointer(NewSongID) = ctSSLayer_LoadSong(TempMusic, PLIFile(PLCurItem), -2& * (Not xClassicBlockingOnLoad) - CLng(CheckAccurate))
          #End If
        Else
        'xLoadingStream = 2&
        ''xLoadURL = ManualURL
        ''UpdateAnimIcon
        'MainForm_UpdateMainIcon
        SongPointer(NewSongID) = ctSSLayer_LoadSong(ctSSLayer_MusicTypeInetStream, RealURL(ManualURL), -2& * (Not xClassicBlockingOnLoad))
        End If
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load2"
  #End If
      Call TearDown_Relocate(xLong2)
'      Debug.Print "DoneLoading"
      If SongPointer(NewSongID) Then
        Select Case ctSSLayer_LoadSongFinished(SongPointer(NewSongID))
          Case -1&
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  blockstate: error"
              #End If
            Call ctSSLayer_LoadSongAbort(SongPointer(NewSongID))
            SongPointer(NewSongID) = 0&
          Case 0&
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  blockstate: waiting..."
              #End If
            xLong1 = (GetTickCount And &H3FFFFFFF)
            #If BlockTest Then
              xLong3 = (GetTickCount And &H3FFFFFFF)
              #End If
#If BlockTimeOutHandle Then
            xLong2 = (GetTickCount And &H3FFFFFFF)
#End If
            Do
              DoEventsEx_Sleep 25&
              If DoEventsEx_IsTickCountElapsed(xLong1, DoEventsEx_SleepPeriodical_Interval) Then
                Call DoAnimateReadDir(1&)
                xLong1 = (GetTickCount And &H3FFFFFFF)
                Select Case ctSSLayer_LoadSongFinished(SongPointer(NewSongID))
                  Case -1&
                    #If UseFileLogging Then
                      MainForm_ctConsoleA_AddLog "  blockstate: posterror"
                      #End If
#If BlockTimeOutHandle Then
PostBlockTimeOut:
#End If
                    Call ctSSLayer_LoadSongAbort(SongPointer(NewSongID))
                    SongPointer(NewSongID) = 0&
                    Exit Do
                  Case 1&
                    
                    
                    #If UseFileLogging Then
                      MainForm_ctConsoleA_AddLog "  blockstate: success / nonblocked"
                      #End If
#If BlockTest Then
                    If DoEventsEx_IsTickCountElapsed(xLong3, 5000&) Then
                      Exit Do
                      End If
#Else
                    Exit Do
#End If
                  End Select
                End If
#If BlockTimeOutHandle Then
              If DoEventsEx_IsTickCountElapsed(xLong1, 30000&) Then
                MainForm_ctConsoleA_AddLog "  blockstate: timeout"
                GoTo PostBlockTimeOut
                End If
#End If
              Loop
#If UseFileLogging Then
          Case 1&
            MainForm_ctConsoleA_AddLog "  blockstate: success / nonblocked"
#End If
          End Select
        End If
      'xLoadingStream = 0&
      'UpdateAnimIcon
      'MainForm_UpdateMainIcon
      If Not SongPointer(NewSongID) = 0& Then
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load3"
  #End If
        
        xLong1 = ctSSLayer_SongLength(SongPointer(NewSongID))
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load4"
  #End If
        If TempMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
          For xCrossFadeTimeMN = xCrossFadeTimeM To 1& Step -1&
            If xLong1 > CrossFadeMinPatterns(xCrossFadeTimeMN) Then
              Exit For
              End If
            Next
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load4a"
  #End If
          xBool1 = (xLong1 < CrossFadeNewMinMinPatterns(xCrossFadeTimeMN) Or xLong1 < CrossFadeMinPatterns(xCrossFadeTimeMN) And Not NoSFXWait)
          If DoCrossFade Then
            xLong2 = xXFadeOldTime - ctSSLayer_GetPosition(SongPointer(OldSongID))
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load4b"
  #End If
            For xCrossFadeTimeEN = xCrossFadeTimeMN To 1& Step -1&
              If xLong2 > CrossFadeMinRemainPatterns(xCrossFadeTimeEN) Then
                Exit For
                End If
              Next
            xBool2 = (xLong2 < CrossFadeMinRemainPatterns(xCrossFadeTimeEN))
            End If
          'ElseIf (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
          'Debug.Print OldURLHack
          ''xBool1 = False
          ''xBool2 = False
          'xCrossFadeTimeMN = xCrossFadeTimeM
          Else
          For xCrossFadeTimeMN = xCrossFadeTimeM To 1& Step -1&
            If xLong1 > CrossFadeMinTime(xCrossFadeTimeMN) Then
              Exit For
              End If
            Next
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load4c"
  #End If
          xBool1 = (xLong1 < CrossFadeNewMinMinTime(xCrossFadeTimeMN) Or xLong1 < CrossFadeMinTime(xCrossFadeTimeMN) And Not NoSFXWait)
          If DoCrossFade Then
            If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
              xLong2 = 360000
              'Debug.Print "OldURLHack2"
              Else
              xLong2 = xXFadeOldTime - ctSSLayer_GetPosition(SongPointer(OldSongID))
              End If
#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "  load4d"
  #End If
            For xCrossFadeTimeEN = xCrossFadeTimeMN To 1& Step -1&
              If xLong2 > CrossFadeMinRemainTime(xCrossFadeTimeEN) Then
                Exit For
                End If
              Next
            xBool2 = (xLong2 < CrossFadeMinRemainTime(xCrossFadeTimeEN))
            End If
          End If
        If DoCrossFade Then
          xBool2 = TrackerIsLooping
          If xCrossFadeTimeMN > xCrossFadeTimeMC Then
            If xCrossFadeTimeEN > xCrossFadeTimeMC Then
              xCrossFadeTimeMT = xCrossFadeTimeMC
              Else
              xCrossFadeTimeMT = xCrossFadeTimeEN
              End If
            ElseIf xCrossFadeTimeEN > xCrossFadeTimeMN Then
            xCrossFadeTimeMT = xCrossFadeTimeMN
            Else
            xCrossFadeTimeMT = xCrossFadeTimeEN
            End If
          If xBool1 Or xBool2 Then
            If NoSFXWait Or xBool2 Or TrackerLoop Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "!Stopped4"
                #End If
              MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
              DoCrossFade = False
              Else
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "  new song too short to crossfade, playing after current song"
                #End If
              Call ctSSLayer_LoadSongAbort(SongPointer(NewSongID))
              EndSongEx
              xPlayItem = True
              GoTo HideAnimAndExit
              'Exit Function
              End If
            ElseIf Not (NoSFXWait Or xCrossFadeTimeMC = xCrossFadeTimeMT) Then
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  check new timings!"
              MainForm_ctConsoleA_AddLog "  Old  xFadeTime: " + CStr(xCrossFadeTimeMC + 2&)
              MainForm_ctConsoleA_AddLog "  New  xFadeTime: " + CStr(xCrossFadeTimeMN + 2&)
              MainForm_ctConsoleA_AddLog "  Fade left time: " + CStr(xCrossFadeTimeEN + 2&)
              MainForm_ctConsoleA_AddLog "  Used xFadeTime: " + CStr(xCrossFadeTimeMT + 2&)
              #End If
            If CurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
              xBool1 = (xLong2 > CrossFadeMaxPattern(xCrossFadeTimeMT))
              ElseIf Not CurMusic = ctSSLayer_MusicTypeTracker Or (ctSSLayer_CanUseItem(6&) And &H1&) = &H1& Then
              xBool1 = (xLong2 > CrossFadeMaxPos(xCrossFadeTimeMT))
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "LengthCur:              " + CStr(PLCurSongLength)
                MainForm_ctConsoleA_AddLog "LengthLeftCur:          " + CStr(PLCurSongLength - ctSSLayer_GetPosition(SongPointer(CurrentSongID)))
                MainForm_ctConsoleA_AddLog "CrossFadeMinPos:        " + CStr(CrossFadeMinPos(xCrossFadeTimeMT))
                MainForm_ctConsoleA_AddLog "CrossFadeMaxPos:        " + CStr(CrossFadeMaxPos(xCrossFadeTimeMT))
                MainForm_ctConsoleA_AddLog "CrossFadeMinRemainTime: " + CStr(CrossFadeMinRemainTime(xCrossFadeTimeMT))
                MainForm_ctConsoleA_AddLog "Result:                 " + CStr(xBool1)
                #End If
              End If
            If xBool1 Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "  new timings!"
                #End If
              xCrossFadeTimeMC = xCrossFadeTimeMN
              Call ctSSLayer_LoadSongAbort(SongPointer(NewSongID))
              'xSongIsMidi = xSongWasMidi
              EndSongEx
              SilentPlNext = False
              xPlayItem = True
              'Exit Function
              GoTo HideAnimAndExit
              End If
            
            'DoCrossFade = False
            'If Not PlaySilent Then xLong1 = xSetVolume
            End If
          End If

        
        #If BoldDirs = 98 Then
          If Not UCase(PLI(PLCurItem).Filename) = xLastOpenedLogged Then
            MainForm_ctConsoleA_AddSong PLI(PLCurItem).Filename
            xLastOpenedLogged = UCase(PLI(PLCurItem).Filename)
            End If
          #Else
          If Not UCase(PLIFile(PLCurItem)) = xLastOpenedLogged Then
            MainForm_ctConsoleA_AddSong PLIFile(PLCurItem)
            xLastOpenedLogged = UCase(PLIFile(PLCurItem))
            End If
          #End If
        
        'Call ctSSLayer_MoveSongID(False)
        'CurrentSongID = NewSongID
        'NewSongID = Tear_Create()
        If Len(ManualURL) = 0& Then
          PLCurSongLength = xLong1 ' ctSSLayer_SongLength(currentsongid)
          Else
          PLCurSongLength = 100&
          End If
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  starting playback..."
          #End If
        
        
        
        'prejump = 0&
        'If RandomPosOnTrackerSkipper And (xGenFading Or DoCrossFade) And (xTrackerSkipper(TempMusic) > 0& Or IntroModeActive) Then
        If Len(ManualURL) = 0& And (RandomPosOnTrackerSkipper And (xTrackerSkipper(TempMusic) > 0& Or (IntroModeActive And Not xRep = 0&))) Then
          If Not TempMusic = ctSSLayer_MusicTypeTracker Or (ctSSLayer_CanUseItem(6&) And 4&) = 4& Then
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  Randomizer:        "
              MainForm_ctConsoleA_AddLog "    Size:            " + CStr(PLCurSongLength)
              MainForm_ctConsoleA_AddLog "    Required (CS 0): " + CStr(CrossFadeMinTime(0))
              If IntroModeActive And Not xRep = 0& Then
                MainForm_ctConsoleA_AddLog "    IntroTime:       " + CStr(IntroModeTime * 1000&)
                Else
                MainForm_ctConsoleA_AddLog "    TrackerSkipper:  " + CStr(xTrackerSkipper(TempMusic) * 60000)
                End If
              #End If
            If IntroModeActive And Not xRep = 0& Then
              xLong1 = PLCurSongLength - (CrossFadeWaitSeek + IntroModeTime * 1000&)
              Else
              xLong1 = PLCurSongLength - (CrossFadeWaitSeek + xTrackerSkipper(TempMusic) * 60000)
              End If
            If xLong1 > CrossFadeMinTime(xCrossFadeTimeM) Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "    ok - still crossfade range"
                MainForm_ctConsoleA_AddLog "    MaxPos           " + CStr(CrossFadeMaxPos(xCrossFadeTimeM))
                MainForm_ctConsoleA_AddLog "    MinPos           " + CStr(CrossFadeMinPos(xCrossFadeTimeM))
                #End If
              
              #If UseFileLogging Then
                xLong1 = xLong1 - CrossFadeMinPos(xCrossFadeTimeM)
                'PreJump = CLng(Rnd * xLong1)
                PreJump = CLng((Rnd * 0.92 + 0.03) * xLong1)
                MainForm_ctConsoleA_AddLog "    CanSeekBetween   " + CStr(xLong1)
                MainForm_ctConsoleA_AddLog "    DefMin=0+ Delay  " + CStr(CrossFadeWaitSeek)
                MainForm_ctConsoleA_AddLog "    DefMax]]+ Delay  " + CStr(CrossFadeWaitSeek + xLong1)
                If IntroModeActive And Not xRep = 0& Then
                  MainForm_ctConsoleA_AddLog "      When timeout   " + CStr(CrossFadeWaitSeek + xLong1 + IntroModeTime * 1000&)
                  Else
                  MainForm_ctConsoleA_AddLog "      When timeout   " + CStr(CrossFadeWaitSeek + xLong1 + xTrackerSkipper(TempMusic) * 60000)
                  End If
                MainForm_ctConsoleA_AddLog "      MustNotExeed:  " + CStr(PLCurSongLength - CrossFadeMinPos(xCrossFadeTimeM))
                MainForm_ctConsoleA_AddLog "    Is: -----------> " + CStr(PreJump) + " /"
                MainForm_ctConsoleA_AddLog "                     " + CStr(xLong1) + " - " + CStr(PLCurSongLength)
                #Else
                'PreJump = CLng(Rnd * (xLong1 - CrossFadeMinPos(xCrossFadeTimeM)))
                PreJump = CLng((Rnd * 0.92 + 0.03) * (xLong1 - CrossFadeMinPos(xCrossFadeTimeM)))
                #End If
              End If
            End If
          End If
        
        'NewSongID = Tear_Create()
        If PlaySilent Or DoCrossFade Then
          'Call ctSSLayer_LoadSongPlay(NewSongID, 0&, (xRep = 0&), xModPanning, False)
          Call ctSSLayer_LoadSongPlay(SongPointer(NewSongID), 0&, (xRep = 0&), xModPanning, ResumePaused)
          Tear_Volume(NewSongID) = 0
          Else
          If ResumePaused Or PreJump = 0& Then
            Call ctSSLayer_LoadSongPlay(SongPointer(NewSongID), xSetVolume, (xRep = 0&), xModPanning, ResumePaused)
            Else
            Call ctSSLayer_LoadSongPlay(SongPointer(NewSongID), 0&, (xRep = 0&), xModPanning, False)
            ' only time based!
            Call ctSSLayer_SeekSong(SongPointer(NewSongID), CrossFadeWaitSeek + PreJump)
            Tear_Volume(NewSongID) = 0
            Call Tear_StartDropping(NewSongID, xSongWillBeMidi, 12&, 0&, SimpleFadingTotalTime, 1&)
            End If
          Tear_Volume(NewSongID) = 1
          End If
'Stop
        CurrentSongID = NewSongID
        CurMusic = TempMusic
        ClearOldSongInfo
        If TempMusic = ctSSLayer_MusicTypeCDAudio Then
          Tear_IsINet(CurrentSongID) = False
          #If BoldDirs = 98 Then
            PLCurSongDisplay = PLI(PLCurItem).Caption
            #Else
            PLCurSongDisplay = PLICap(PLCurItem)
            #End If
          ElseIf Len(ManualURL) = 0& Then
          Tear_IsINet(CurrentSongID) = False
          #If BoldDirs = 98 Then
            PLCurSongDisplay = PLI(PLCurItem).Caption
            #Else
            PLCurSongDisplay = PLICap(PLCurItem)
            #End If
          Else
          Tear_IsINet(CurrentSongID) = True
          'MainForm_EnableApiMenu 407&, (Not Len(PLCurFullURL) = 0&)
          'MainForm_EnableApiMenu 408&, (Not (Len(PLCurFullURL) = 0& And UrlsAllowed))
          'PLCurFullURL = ManualURL
          SenderURL = Mid(ManualURL, InStr(ManualURL + vbCr, vbCr) + 1&)
          PLCurSongDisplay = RealURL(SenderURL)
          If Len(PLCurSongDisplay) = 0& Then
            PLCurSongDisplay = RealURL(ManualURL) 'PLCurSongDisplay
            SenderURL = ""
            Else
            SenderURL = Mid(SenderURL, InStr(SenderURL + vbCr, vbCr) + 1&)
            SenderURL = RealURL(SenderURL)
            End If
          End If
        PLCurSongPos = 0&
        If Len(ManualURL) = 0& Then
          PLCurSongDisplaySpace = GetTimeFmtMax
          'GetTimeFmt
          #If BoldDirs = 98 Then
            PLCurSong = PLI(PLCurItem).Filename
            #Else
            PLCurSong = PLIFile(PLCurItem)
            #End If
          Tear_Filename(CurrentSongID) = PLCurSong
          Else
          PLCurSongDisplaySpace = PLCurSongDisplaySpacePerc
          PLCurFullURL = ManualURL
          MainForm_EnableApiMenu 407&, (Not Len(PLCurFullURL) = 0&)
          MainForm_EnableApiMenu 408&, (Not (Len(PLCurFullURL) = 0& And UrlsAllowed))
          Tear_Filename(CurrentSongID) = PLCurFullURL
          End If
        Tear_Caption(CurrentSongID) = PLCurSongDisplay
        
        If MP3GetID3Tag Or MP3GetFormat Then
          MainForm_ReadLate PLCurSong, False
          End If
        GetTimeFmt (xSongWillBeMidi = 3&)

        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "AddLastDir"
          #End If

        ResetPatternLog
        If Len(ManualURL) = 0& Then
          #If LoudestSongMethode Then
            xAddLastDir
            #Else
            xAddLastDir Not ResumePaused  'True
            #End If
          If xFollowCurSong And Not (PLCount = 0& Or PLCurItem < 0&) And PLCurItem < PLCount Then
            If SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 2 Then
              Call GetListIndexMS(xListHWnd(2), xLong1)
              'If Not PLCurItem = SendMessage(xListHWnd(2), &H19F&, 0&, 0&) Then
              If Not PLCurItem = xLong1 Then
                xMarkSong PLCurItem, False, False
                xFollowCurSongLast = DoEventsEx_GetTickCount
                If DoCrossFade Then
                  xFollowCurSongLastDiff = CrossFadeWaitSeek + CrossFadeTimeSingle(xCrossFadeTimeMT) + CrossFadeTimeDelay(xCrossFadeTimeMT)
                  Else
                  xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
                  End If
                End If
              End If
            End If
          End If
        If DoCrossFade Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  initializing crossfade tear"
            #End If
          If Not TempMusic = ctSSLayer_MusicTypeTracker Or (ctSSLayer_CanUseItem(6&) And 4&) = 4& Then
            xLong1 = CrossFadeWaitSeek + PreJump
            Else
            xLong1 = CrossFadeWaitSkipPattern
            End If
          If Tear_StepT(OldSongID) = 0& Then Tear_Volume(OldSongID) = 1&
          'Debug.Print "X: ", CrossFadeTimeSingle(xCrossFadeTimeMT), xCrossFadeTimeMT
          If Not Len(ManualURL) = 0& Then GoTo NoURLSeek
          If ctSSLayer_SeekSong(SongPointer(CurrentSongID), xLong1) Then
NoURLSeek:
            'MainForm_ctConsoleA_AddLog "  crossfade tear 1"
            Call Tear_StartDropping(CurrentSongID, xSongWillBeMidi, 10&, 0&, CrossFadeTimeSingle(xCrossFadeTimeMT), 1&)
            'MainForm_ctConsoleA_AddLog "  crossfade tear 2"
            Call Tear_StartDropping(OldSongID, xSongIsMidi, 11&, CrossFadeTimeDelay(xCrossFadeTimeMT), CrossFadeTimeSingle(xCrossFadeTimeMT), 0&)
            Else
            'MainForm_ctConsoleA_AddLog "  crossfade tear 1b"
            Call Tear_StartDropping(CurrentSongID, xSongWillBeMidi, 10&, 0&, CrossFadeWaitTime + CrossFadeTimeSingle(xCrossFadeTimeMT), 1&)
            'MainForm_ctConsoleA_AddLog "  crossfade tear 2b"
            Call Tear_StartDropping(OldSongID, xSongIsMidi, 11&, CrossFadeWaitTime + CrossFadeTimeDelay(xCrossFadeTimeMT), CrossFadeTimeSingle(xCrossFadeTimeMT), 0&)
            End If
          'Else
          
          End If
        'MainForm_ctConsoleA_AddLog "  crossfade tear 3"
        xSongIsMidi = xSongWillBeMidi
'        CheckIsRadio
        NewSongID = Tear_Create()
        PLCurSongPos = 0&
        PLCurSongLastPos = -1&
        xCrossFadeTimeMC = xCrossFadeTimeMN
        
        xPlayItem = True
        'MainForm_ctConsoleA_AddLog "  crossfade tear 4"
        MainForm_UpdateMainIcon
        'MainForm_ctConsoleA_AddLog "  crossfade tear 5"
        xEndSongTimerEventTime = DoEventsEx_GetTickCount
        LastPosTime = xEndSongTimerEventTime
        If NoSFXWait Then
          ForceLoudestTear = CurrentSongID
          If Not ResumePaused Then
            ShowIRI
            End If
          Else
          ForceLoudestTear = -1&
          End If
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  playItem finished"
          #End If
        Else
'        If Not xBool1 Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItemA(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
        #If UseFileLogging Then
          If Len(ManualURL) = 0& Then
            #If BoldDirs = 98 Then
              MainForm_ctConsoleA_AddLog "  loading failed (" & PLI(PLCurItem).Filename & ")"
              #Else
              MainForm_ctConsoleA_AddLog "  loading failed (" & PLIFile(PLCurItem) & ")"
              #End If
            Else
            MainForm_ctConsoleA_AddLog "  loading failed (" & RealURL(ManualURL) & ")"
            End If
          #End If
'          End If
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItemB(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
        If Len(ManualURL) = 0& Then
          CheckThisSong PLCurItem
          End If
        End If
HideAnimAndExit:
      xLoadingStream = 0&
      'UpdateAnimIcon
      MainForm_UpdateMainIcon
      Else
      #If UseFileLogging Then
        #If BoldDirs = 98 Then
          MainForm_ctConsoleA_AddLog "Current song: " & PLI(PLCurItem).Filename
          #Else
          MainForm_ctConsoleA_AddLog "Current song: " & PLIFile(PLCurItem)
          #End If
        #End If
      xPlayItem = True
      If xFollowCurSong And Not (PLCount = 0& Or PLCurItem < 0&) And PLCurItem < PLCount Then
        If SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 2 Then
          xMarkSong PLCurItem, False, False
          'xFollowCurSongLast = DoEventsEx_GetTickCount
          'xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
          End If
        End If
      End If
    End If
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xPlayItemC(xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  xPlayItem finished"
    #End If
  End Function

Private Sub ClearOldSongInfo()
  'If MP3GetFormat Then
  '  Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(-1), 0&)
  '  End If
  curTagText = ""
  SetWindowText xStaticIconsHWnd(xStaticIcons_Tags), ""
  SetFormatToolTip LoadResEx_String(1900&), ""
#If ProcCtl Then
  Call SendMessage(xStaticIconsHWnd(xStaticIcons_CurSongPos), 1026&, 0&, 0&)
#Else
  MainForm_EraseBackground xStaticIconsHWnd(xStaticIcons_CurSongPos)
#End If
  'MainForm_UpdateMainIcon
  'ePic(1).Line (0&, 0&)-(ePic1ScaleWidth, 10&), FLineColorX, BF
  MainForm_EnableApiMenu 408&, False
  End Sub

Private Sub MainForm_TrayIconHandling(x As Long)
  If Not xInfoQuit Then
    If x = 513 Or x = 516 Then
      If (x = 513 And xTrayIconLeft = 0& Or x = 516 And xTrayIconRight = 0&) Then
ShowPlayerHack:
        DoEvents
        DoEvents
        DoEvents
        ShowPlayer True
        ElseIf IsDialoging = 0& And Not (PlayerIsLocked Or xAutoDoing) Then
        #If UseFileLogging Then
          If Not (xTrayIconVisible Or xTrayIconIndicator) Then
            MainForm_ctConsoleA_AddLog "### Debug Info: InvSysIconClickEvent"
            Exit Sub
            End If
          #End If
        If vKioskMode Then
          If x = 513 And xTrayIconLeft = 6& Then
            MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(4), 0&)
            Else
            MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(2), 0&)
            End If
          Else
          If x = 513 And xTrayIconLeft = 4& Or _
             x = 516 And xTrayIconRight = 1& Then
            If x = 516 And xMenu(3) = 0& Then GoTo ShowPlayerHack
            Select Case xTrayIconLeft
              Case 0&
                If xTrayIconIndicator And xTrayIconVisible Then
                  Call SetMenuDefaultItem(xMenu(3), 2101&, False)
                  Else
                  Call SetMenuDefaultItem(xMenu(3), -1&, True)
                  End If
              Case 1&: Call SetMenuDefaultItem(xMenu(3), 2102&, False)
              Case 2&: Call SetMenuDefaultItem(xMenu(3), 2105&, False)
              Case 3&: Call SetMenuDefaultItem(xMenu(3), 2106&, False)
              Case Else: Call SetMenuDefaultItem(xMenu(3), -1&, True)
              End Select
            'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(3), 0&, 0&)
            MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(3), 0&)
            
            ElseIf x = 513 And xTrayIconLeft = 5& Or _
                   x = 516 And xTrayIconRight = 2& Then
            If x = 516 And xMenu(2) = 0& Then GoTo ShowPlayerHack
            Select Case xTrayIconLeft
              Case 0&
                If xTrayIconIndicator And xTrayIconVisible Then
                  Call SetMenuDefaultItem(xMenu(2), 2800&, True)
                  Else
                  Call SetMenuDefaultItem(xMenu(2), -1&, True)
                  End If
              Case 1&: Call SetMenuDefaultItem(xMenu(2), 2102&, True)
              Case 2&: Call SetMenuDefaultItem(xMenu(2), 2105&, True)
              Case 3&: Call SetMenuDefaultItem(xMenu(2), 2106&, True)
              Case Else: Call SetMenuDefaultItem(xMenu(2), -1&, True)
              End Select
            'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(2), 0&, 0&)
            MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(2), 0&)
            ElseIf x = 513 Then
            Select Case xTrayIconLeft
              Case 1 'play/pause
                If xPStateIconPaused Or Not xPStateIconPlaying Then
                  MainForm_IconsMouseUp 1&
                  Else
                  MainForm_IconsMouseUp 2&
                  End If
              Case 2 'playnext (skip)
                If xPStateIconPaused Or Not xPStateIconPlaying Then
                  MainForm_IconsMouseUp 1&
                  Else
                  MainForm_IconsMouseUp 4&
                  End If
              Case 3 'endsong
                If xPStateIconPaused Or Not xPStateIconPlaying Then
                  MainForm_IconsMouseUp 1&
                  Else
                  MainForm_IconsMouseUp 5&
                  End If
        '      Case 4 'show menu
        '      Case 5 'show minimenu
              Case 6 'show volume menu
                'Call SetMenuDefaultItem(xMenu(4), -1&, True)
                'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(4), 0&)
                If x = 516 And xMenu(3) = 0& Then GoTo ShowPlayerHack
              End Select
            End If
          End If
        ElseIf xAutoDoing And xLoadingStream = 0& And Not vKioskMode Then
        If xMenu(7) = 0& Then GoTo ShowPlayerHack
        MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(7), 0&)
        Else
        If xMenu(6) = 0& Then GoTo ShowPlayerHack
        MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(6), 0&)
        End If
      End If
    End If
  End Sub

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

  On Error GoTo 0



  If ShowIRWin Then
    If DoEventsEx_IsTickCountElapsed(ShowIRTicks, ShowIRTicksFull) Then
      MainForm_HideIRWin
      End If
    End If

If CPListMustDo Then
  If Not (PlayerIsLocked Or xAutoDoing) And DoEventsEx_IsTickCountElapsed(CPListTime, CPListDelay) Then
    'If InterfaceMode = Interface_Mode_search Then
      MainForm_TrySearch False 'True
    '  Else
    '  CPListMustDo = False
    '  End If
    End If
  End If

If Not xFindNtfy = 0& Then
  If WaitForSingleObject(xFindNtfy, 0&) = 0& Then
    Call MainForm_XMenuRefresher '(False)
    Call FindNextChangeNotification(xFindNtfy)
    End If
  End If

If xTimerIsEnabled And Not (xTimerEditing Or xTimerEvCount = 0& Or PlayerIsLocked Or xAutoDoing) Then
  GrabError
  If Timer \ 60& = xTimerEvTime(0&) And Not LastDoneTimer = xTimerEvTime(0&) + 1& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Timer event... (timer: " & CStr(Timer \ 60&) & ", xTimerEv(0&): " & CStr(xTimerEvTime(0)) & ", LastTimer: " & CStr(LastDoneTimer - 1&) & ")"
      #End If
    
    TimerListForm_xLocked = True
    If InterfaceMode = Interface_Mode_Timer Then TimerListForm_ApplyLockState
    
    Call ProcessTimer(xTimerEv(0&))
    
    If xTimerEvIR = 0& Then
      xTimerRemove 0&
      xTimerEvIR = -1&
      End If
    LastDoneTimer = xTimerEvTime(0) + 1&
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Timer event... (Lasttimer changed: " & CStr(LastDoneTimer - 1&)
      #End If
    
    If InterfaceMode = Interface_Mode_Timer Then
      TimerListForm_ListUpdate
      'frmTimer.mM.Enabled = True
      'frmTimer.mF.Enabled = True
      Else
      MainForm_SetTimerImage
      End If
    TimerListForm_xLocked = False
    If InterfaceMode = Interface_Mode_Timer Then
      TimerListForm_ApplyLockState
      'timerlistform_cleanbuttons
      End If
    Else
    If DoEventsEx_IsTickCountElapsed(xTimerTimeTimerTime, 40000) Then
      GrabError
      If Not LastDoneTimer = Timer \ 60& + 1& Then
        LastDoneTimer = Timer \ 60& + 1&
        If InterfaceMode = Interface_Mode_Timer Then
          TimerListForm_ListUpdate
          Else
          ResortTimers
          End If
        Else
        MainForm_SetTimerImage
        End If
      End If
    End If
  End If

#If TrayIconViaTimerCheck Then
  If xTrayIconIndicator Or xTrayIconVisible Then
    If DoEventsEx_IsTickCountElapsed(xTrayIconCrashUpdateTimer, xTrayIconCrashUpdateTimerDelay) Then
      #If UseFileLogging And DetailLog Then
        MainForm_ctConsoleA_AddLog "Refreshing systray icon (Explorer may have reloaded)"
        #End If
      #If TrayIconViaTimerCheck = 2 Then
        Call Shell_NotifyIcon(1&, SysTrayIcon)
        #Else
        Call Shell_NotifyIcon(2&, SysTrayIcon)
        Call Shell_NotifyIcon(0&, SysTrayIcon)
        #End If
      xTrayIconCrashUpdateTimer = DoEventsEx_GetTickCount
      End If
    End If
  #End If

'If Not (PlayerIsLocked Or xAutoDoing) And ((IntroModeActive And Not xRep = 0&) Or xTrackerSkipper(CurMusic) > 0&) And xTimerIsEnabled And xPStateIconPlaying And Not (xPStateIconPaused Or PLCount = 0& Or CurMusic = ctSSLayer_MusicTypeNone Or xRep = 0& Or SetupVisible) And xEndSongTimerEventTime > -1& Then
If Not ((CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Or PlayerIsLocked Or xAutoDoing) And ((IntroModeActive And Not xRep = 0&) Or xTrackerSkipper(CurMusic) > 0&) And xTimerIsEnabled And xPStateIconPlaying And Not (xPStateIconPaused Or PLCount = 0& Or CurMusic = ctSSLayer_MusicTypeNone Or xRep = 0&) And xEndSongTimerEventTime > -1& Then
  If (IntroModeActive And Not xRep = 0&) Then
    If DoEventsEx_IsTickCountElapsed(xEndSongTimerEventTime, IntroModeTime * 1000&) Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Timer event: Ending song (IntroMode)"
        #End If
      xEndSongTimerEventTime = -1&
      MainForm_CheckAfter True, False, True, False ' False , True
      GoTo FinishMark
      End If
    End If
    'Else
  If xTrackerSkipper(CurMusic) > 0& Then
    If DoEventsEx_IsTickCountElapsed(xEndSongTimerEventTime, xTrackerSkipper(CurMusic) * 60000) Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Timer event: Ending song (End song after ... mins)"
        #End If
      xEndSongTimerEventTime = -1&
      MainForm_CheckAfter True, False, False, True
      GoTo FinishMark
      End If
    End If
  End If




If Not (xAutoDoing Or PlayerIsLocked) Then
  If NotOutOfSession Then
    If OutOfSessionLeft Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "ReEntering session"
        #End If
      OutOfSessionLeft = False
      If xPStateIconPlaying And xPStateIconPaused And OutOfSessionHasPaused Then
        OutOfSessionHasPaused = False
        MainForm_xPauseClick
        End If
      OutOfSessionHasPaused = False
      End If
    ElseIf OutOfSessionPause And Not OutOfSessionLeft Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Leaving session"
      #End If
    OutOfSessionLeft = True
    If xPStateIconPlaying And Not xPStateIconPaused Then
      OutOfSessionHasPaused = True
      MainForm_xPauseClick
      Else
      OutOfSessionHasPaused = False
      End If
    End If
  
  If Not (xPStateIconPaused Or OutOfSessionHasPaused) And xAutoPlayback And xReAutoPlay Then
    If xPStateIconPlaying Then
      xAutoPlaybackLastRetryTime = DoEventsEx_GetTickCount
      ElseIf DoEventsEx_IsTickCountElapsed(xAutoPlaybackLastRetryTime, 60000) Then
      #If UseFileLogging Then
        If ReAutoPlayDR Then
          MainForm_ctConsoleA_AddLog "Timer event: Attempting automatic playback (Delayed autorefresh)"
          Else
          MainForm_ctConsoleA_AddLog "Timer event: Attempting automatic playback"
          End If
        #End If
      If PLCount = 0& Then
        If xAutoDirCount = 0& Then
          xAutoPlaybackLastRetryTime = DoEventsEx_GetTickCount
          ReAutoPlayDR = False
          GoTo FinishMark
          Else
          MainForm_xAutoRefreshNow False, True, False, False, False
          ReAutoPlayDR = False
          Exit Sub
          End If
        Else
        MainForm_IconsMouseUp 1&
        xAutoPlaybackLastRetryTime = DoEventsEx_GetTickCount
        ReAutoPlayDR = False
        GoTo FinishMark
        End If
      End If
    End If
  End If

If Not (xAutoDoing Or PlayerIsLocked) And ReAutoPlayDR Then
  If DoEventsEx_IsTickCountElapsed(ReAutoPlayDRTime, 60000) Then
    MainForm_xAutoRefreshNow False, False, False, False, False
    CheckReplay
    End If
  End If

If Not CurMusic = ctSSLayer_MusicTypeNone Then
  If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
    PLCurSongPos = ctSSLayer_GetPosition(SongPointer(CurrentSongID))
'    Stop
    If PLCurSongPos > 0& Or xPStateIconPaused Then
      LastPosTime = DoEventsEx_GetTickCount
      'updatebitrate xLong1
      MainForm_ReadLate "", True
      ElseIf DoEventsEx_IsTickCountElapsed(LastPosTime, LastPosSkipTimeNetStream) Then
      If Not StartWeb(PLCurFullURL, False) Then
        MainForm_CheckAfter False, False, False, False
        GoTo FinishMark
        'Else
        'PLCurSongPos = 0&
        End If
      End If
    'Debug.Print "NS: ", xLong1, xLong2
    If PLCurSongPos < 0& Then PLCurSongPos = 0&
    GetTimeFmt True
    Else
    xBool1 = xTimerIsEnabled And Not (PlayerIsLocked Or xAutoDoing Or xPStateIconPaused)
    PLCurSongPos = ctSSLayer_GetPosition(SongPointer(CurrentSongID))
    GetTimeFmt False
    If CurMusic = ctSSLayer_MusicTypeTracker Then
      If ctSSLayer_MusicHasFinished(SongPointer(CurrentSongID)) And xBool1 Then
        'ePic(1).Line (0&, 0&)-(ePic1ScaleWidth, 10&), FLineColor, BF
        MainForm_CheckAfter False, False, False, False
        GoTo FinishMark
        ElseIf (ctSSLayer_CanUseItem(6&) And &H1&) = &H1& Then
        ' d = 4+8+1: 1+8, but not 4!
        'If (ctSSLayer_CanUseItem(6&) And &H5&) = &H5& Then
        If (ctSSLayer_CanUseItem(6&) And &H4&) = &H4& Then
          xLong1 = ctSSLayer_MusicGetPattern(SongPointer(CurrentSongID))
          '''xLong2 = ctSSLayer_MusicGetPatterns(SongPointer(CurrentSongID))
          
          Else
          If xBool1 And xProcessMode = 1& And Not (SilentPlNext Or PLCount = 0& Or (xRep = 0& And xSongIsMidi)) _
            And PLCurSongLength > CrossFadeMinPatterns(xCrossFadeTimeMC) And PLCurSongLength - PLCurSongPos <= CrossFadeMinPattern(xCrossFadeTimeMC) And PLCurSongLength - PLCurSongPos >= CrossFadeMaxPattern(xCrossFadeTimeMC) _
            Then
            MainForm_CheckAfter True, False, False, False
            GoTo FinishMark
            Else
            xLong1 = PLCurSongPos
            'xLong2 = PLCurSongLength
            End If
          End If
        'If (ctSSLayer_CanUseItem(6&) And &HD&) = &H9& Then
        '  plcursongpos=
        
        
        If Not xLong1 = xModCurPattern Then
          'xChar1 = Chr(xLong1)
          xModCurPattern = xLong1
          
          If (Asc(Mid(xModPatterns, 1& + (xLong1 \ 8&), 1&)) And (2& ^ (xLong1 Mod 8&))) = 0& Then
          'If InStr(xModPatterns, xChar1) = 0& Then
            'xModPatterns = xModPatterns & xChar1
            Mid(xModPatterns, 1& + (xLong1 \ 8&), 1&) = Chr(Asc(Mid(xModPatterns, 1& + (xLong1 \ 8&), 1&)) + (2& ^ (xLong1 Mod 8&)))
            Else
            TrackerLoop = True
            If xProcessMode = 1& And xBool1 And Not (xRep = 0& Or xAutoDirCount = 0& And PLCount = 0&) Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "Tracker looping event, skipping song (bufferlength: " & CStr(Len(xChar1))
                #End If
              MainForm_CheckAfter True, False, False, False
              GoTo FinishMark
              End If
            End If
          End If

        If (ctSSLayer_CanUseItem(6&) And &HD&) = &H9& Then
          'pseudorealtimecheck for xfade!
          xLong2 = ctSSLayer_MusicTime(SongPointer(CurrentSongID))
          xLong1 = xLong1 * xLong2
          xLong2 = ctSSLayer_MusicGetPatterns(SongPointer(CurrentSongID)) * xLong2
          If xBool1 And xProcessMode = 1& And xLong2 > CrossFadeMinTime(xCrossFadeTimeMC) And Not (SilentPlNext Or PLCount = 0& Or (xRep = 0& And xSongIsMidi)) And _
            xLong2 - xLong1 <= CrossFadeMinPos(xCrossFadeTimeMC) And xLong2 - xLong1 > CrossFadeMaxPos(xCrossFadeTimeMC) Then
            MainForm_CheckAfter True, False, False, False
            GoTo FinishMark
            End If
          End If
        End If
      End If
      
      
  '    GetTimeFmt
      
      ' nontracker or tracker with realtimes
    If Not CurMusic = ctSSLayer_MusicTypeTracker Or (ctSSLayer_CanUseItem(6&) And 4&) = 4& Then
      If xBool1 Then
        If PLCurSongPos = PLCurSongLength Then
          MainForm_CheckAfter False, False, False, False
          GoTo FinishMark
          ElseIf PLCurSongPos = PLCurSongLastPos Then
          If DoEventsEx_IsTickCountElapsed(LastPosTime, LastPosSkipTime) Then
            MainForm_CheckAfter False, False, False, False
            GoTo FinishMark
            End If
          End If
        End If
      
      PLCurSongLastPos = PLCurSongPos
      LastPosTime = DoEventsEx_GetTickCount
      If xBool1 And xProcessMode = 1& And PLCurSongLength > CrossFadeMinTime(xCrossFadeTimeMC) And Not (SilentPlNext Or PLCount = 0& Or (xRep = 0& And xSongIsMidi)) And _
        PLCurSongLength - PLCurSongPos <= CrossFadeMinPos(xCrossFadeTimeMC) And PLCurSongLength - PLCurSongPos > CrossFadeMaxPos(xCrossFadeTimeMC) Then
        MainForm_CheckAfter True, False, False, False
        End If
      End If
  '    End If
    End If
  End If
  'GetTimeFmt
  
  

#If PersonalStuffABC = 1 Then
  If xDoABCStuff Then
    Call ABChat_Timer(CurMusic, PLCurSongLength)
    End If
  #End If

FinishMark:
If xAutoRefreshTimer(1) > 0& Then
  If DoEventsEx_IsTickCountElapsed(xAutoRefreshTimerEventTime(1), xAutoRefreshTimer(1) * 60000) Then
    CheckAutoDirs
    xAutoRefreshTimerEventTime(1) = DoEventsEx_GetTickCount
    End If
  End If
If xAutoRefreshTimer(0) > 0& Then
  If DoEventsEx_IsTickCountElapsed(xAutoRefreshTimerEventTime(0), xAutoRefreshTimer(0) * 60000) Then
    MainForm_xAutoRefreshNow False, False, False, False, False
    End If
  End If
'DoEvents
End Sub

Private Sub GetTimeFmt(ByVal IsIStream As Boolean)
  Dim xLong1 As Long
  Dim xChar1 As String
  'Dim xType1 As RECT
  
  If (CurMusic = ctSSLayer_MusicTypeStream And IsIStream) Then
    'xLong1 = -1&
    'xChar1 = "i"
    'MainForm_RedrawPosition
    
    xChar1 = " " + CStr(PLCurSongPos) + "% "  ' * (ePic1ScaleWidth - ePic1ScaleBonus)) + "% "
    xLong1 = Len(xChar1)
    MainForm_RedrawPosition
    ElseIf PLCurSongLength > 0& And Not (PLCurSongPos < 0& Or PLCurSongPos > PLCurSongLength) Or ctSSLayer_CanUseItem(6&) = &H2& Then
    If Not PLCurSongLength = 0& Then
      xLong1 = PLCurSongPos / PLCurSongLength * (ePic1ScaleWidth - ePic1ScaleBonus)
      
      
      MainForm_RedrawPosition
      
      'ePic(1).Line (0&, 0&)-(xLong1, 10&), FLineColor, BF
      'ePic(1).Line (xLong1 + 1&, 0&)-(ePic1ScaleWidth, 10&), FLineColorX, BF
      End If
    If CurMusic = ctSSLayer_MusicTypeTracker And ctSSLayer_CanUseItem(6&) < 2& Then
      ' nothing realtimes, or patterns
      xChar1 = " " & Right("0000000000" & CStr(PLCurSongPos + 1&), Int(Log(PLCurSongLength) / Log(10&)) + 1&) & "/" & CStr(PLCurSongLength)
      Else
      If Not CurMusic = ctSSLayer_MusicTypeTracker Or (ctSSLayer_CanUseItem(6&) And 4&) = 4& Then
        xLong1 = ((1& + CLng(xShowElapsed)) * PLCurSongLength + (CLng(xShowElapsed) * -2& - 1&) * PLCurSongPos) \ 1000&
        ElseIf (ctSSLayer_CanUseItem(6&) And 8&) = &H8& Then
        CurSongPatterns = ctSSLayer_MusicGetPatterns(SongPointer(CurrentSongID))
        If Not CurSongPatterns = 0& Then
          xLong1 = ((1& + CLng(xShowElapsed)) + (CLng(xShowElapsed) * -2& - 1&) * ctSSLayer_MusicGetPattern(SongPointer(CurrentSongID)) / CurSongPatterns) * ctSSLayer_MusicTime(SongPointer(CurrentSongID)) \ 1000&
          End If
        Else ' 2er
        xLong1 = ctSSLayer_MusicTime(SongPointer(CurrentSongID)) \ 1000&
        End If
      If (PLCurSongLength < 600000 And PLCurSongPos < 600000) Then
        xChar1 = " " + CStr(xLong1 \ 60&) + ":" + Right("00" + CStr(xLong1 Mod 60&), 2&)
        ElseIf (PLCurSongLength < 3600000 And PLCurSongPos < 3600000) Then
        xChar1 = " " + Right("00" & CStr(xLong1 \ 60&), 2&) + ":" + Right("00" + CStr(xLong1 Mod 60&), 2&)
        Else
        xChar1 = " " + Right("0000000000" + CStr(xLong1 \ 3600&), Int(Log(PLCurSongLength \ 3600000) / Log(10&)) + 1&) & ":" & Right("00" + CStr((xLong1 Mod 3600&) \ 60&), 2&) & ":" & Right("00" + CStr(xLong1 Mod 60&), 2&)
        End If
      End If
    ElseIf CurMusic = ctSSLayer_MusicTypeTracker And ctSSLayer_CanUseItem(6&) < 2& Then
    If PLCurSongLength > 0& Then
      xChar1 = " " & Right("????????", Int(Log(PLCurSongLength) / Log(10&)) + 1&) & "/" & CStr(PLCurSongLength)
      Else
      xChar1 = " ??/??"
      End If
    Else 'somethingtimebased
    'If PLCurSongLength > 0& Then
      If PLCurSongLength < 600000 Or CurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
        xChar1 = " ?:??"
        ElseIf PLCurSongLength < 3600000 Then
        xChar1 = " ??:??"
        Else
        xChar1 = " " & String(Int(Log(PLCurSongLength \ 3600000) / Log(10&)) + 1&, "?") & "??:??"
        End If
      'Else
      'xchar1 = " ?:??
      'End If
    End If
  xLong1 = Len(xChar1)
  If Not xLong1 = 0& Then
    PLCurSongDisplaySpaceStr = xChar1 + "  -  " + PLCurSongDisplay
    Call SelectObject(xSharedDC, hFontGFix)
    xLong1 = GetTextOutWidth(xSharedDC, Left(PLCurSongDisplaySpaceStr, xLong1 + 1&))
    PLCurSongDisplaySpaceStr = xChar1 + "  -  " + PLCurSongDisplay
    PLCurSongDisplaySpacePosOrg = PLCurSongDisplaySpace - xLong1
    Call MP3GetFormatChanged(True)
    End If
  End Sub

Private Function GetTimeFmtMax() As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  
  If PLCurSongLength > 0& Or ctSSLayer_CanUseItem(6&) = &H2& Then
    If CurMusic = ctSSLayer_MusicTypeTracker And ctSSLayer_CanUseItem(6&) < 2& Then
      xChar1 = String(Int(Log(PLCurSongLength) / Log(10&)) + 1&, "*") & "/" & CStr(PLCurSongLength)
      Else
      If PLCurSongLength < 600000 Then
        xChar1 = "*:**"
        ElseIf PLCurSongLength < 3600000 Then
        xChar1 = "**:**"
        Else
        xChar1 = String(Int(Log(PLCurSongLength \ 3600000) / Log(10&)) + 1&, "*") & ":**:**"
        End If
      End If
    ElseIf CurMusic = ctSSLayer_MusicTypeTracker And ctSSLayer_CanUseItem(6&) < 2& Then
    If PLCurSongLength > 0& Then
      xChar1 = String(Int(Log(PLCurSongLength) / Log(10&)) + 1&, "?") & "/" & CStr(PLCurSongLength)
      Else
      xChar1 = "??/??"
      End If
    Else 'somethingtimebased
      If PLCurSongLength < 600000 Or CurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
        xChar1 = "?:??"
        ElseIf PLCurSongLength < 3600000 Then
        xChar1 = "??:??"
        Else
        xChar1 = String(Int(Log(PLCurSongLength \ 3600000) / Log(10&)) + 1&, "?") & "??:??"
        End If
    End If
  
  xChar1 = " " + xChar1 + " " ' -  "
  
  Call SelectObject(xSharedDC, hFontGFix)
  For xLong1 = 48& To 57& '0& To 9&
    xChar2 = Replace(xChar1, "*", Chr(xLong1))
    xLong2 = GetTextOutWidth(xSharedDC, xChar2)
    If xLong2 > GetTimeFmtMax Then GetTimeFmtMax = xLong2
    Next
  'ePic_Paint 3
  End Function

Private Function GetTimeFmtMaxPerc() As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  
  'Call SelectObject(xSharedDC, hFontPLSm) done by our only caller
  xChar1 = " 100% " ' -  "
  GetTimeFmtMaxPerc = GetTextOutWidth(xSharedDC, xChar1)
  For xLong1 = 49& To 57& ' 1& To 9&
    xChar1 = " " + String(2&, Chr(xLong1)) + " "
    xLong2 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong2 > GetTimeFmtMaxPerc Then GetTimeFmtMaxPerc = xLong2
    Next
  'ePic_Paint 3
  End Function

Private Sub MainForm_PlayNext(ByVal UserDoes As Boolean, ByVal xStep As Long, ByVal DoCrossFade As Boolean, ByVal ForceFadeOut As Boolean)
Dim xLong1 As Long
Dim xLong2 As Long
Dim xChar1 As String
Dim xChar2 As String

If Not (PlayerIsLocked Or xAutoDoing) Then
  If xPStateIconPaused Then
    xPStateIconPaused = False
    xPStateIconPlaying = False
    
    'added here beta 43 to prevent fadout when paused and skipping
    MainForm_xEndSong False, False, False
    
    xReAutoPlay = False
    ReAutoPlayDR = False
    MainForm_UpdateMainIcon
    If Not PlNextSong = "" Then SetNextSong False
    DoCrossFade = False
    End If

  If PLCount = 0& And Not xAutoDirCount = 0& Then
    MainForm_xAutoRefreshNow False, False, False, True, False  'False
    End If
  If Not PLCount = 0& Then
    MainForm_LockMe True
    'If xRep = 2& Then PLCurItem = RandomSong
    If xRep = 0& And Not UserDoes And PlNextSong = "" Then
      xLong1 = PLCurItem
#If BoldDirs = 98 Then
      If Not LCase(PLI(PLCurItem).Filename) = LCase(PLCurSong) Then
#Else
      If Not LCase(PLIFile(PLCurItem)) = LCase(PLCurSong) Then
#End If
        PLCurItem = xFindSong
        If PLCurItem = 0 Then PLCurItem = xLong1
        End If
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext0"
  #End If
      If xPlayItem(UserDoes Or ForceFadeOut, False, DoCrossFade, UserDoes, False, "") Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext1"
  #End If
        MainForm_LockMe False
        Exit Sub
        End If
      PLCurItem = xLong1
      End If
    
    If xStep = -1& Then
      If Not PlNextSong = "" Then SetNextSong False
      Else
      xLong2 = PLCurItem
      xChar1 = PLCurSong
      xChar2 = PLCurSongDisplay
      If Not PlNextSong = "" Then
#If BoldDirs = 98 Then
        If PLI(PLNext).Filename = PlNextSong Then
#Else
        If PLIFile(PLNext) = PlNextSong Then
#End If
          SetNextSong False
          PLCurItem = PLNext
          'If xPlayItem(True, False, DoCrossFade, UserDoes, False) Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext2"
  #End If
          If xPlayItem(UserDoes Or ForceFadeOut, False, DoCrossFade, UserDoes, False, "") Then
            MainForm_LockMe False
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext3"
  #End If
            Exit Sub
            End If
          Else
          xLong1 = xFindSong(PlNextSong)
          SetNextSong False
          If Not xLong1 = 0 Then PLCurItem = xLong1
          'If xPlayItem(True, False, DoCrossFade, UserDoes, False) Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext4"
  #End If
          If xPlayItem(UserDoes Or ForceFadeOut, False, DoCrossFade, UserDoes, False, "") Then
            MainForm_LockMe False
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext5"
  #End If
            Exit Sub
            End If
          End If
        SetNextSong False
        PLCurItem = xLong2
        PLCurSong = xChar1
        PLCurSongDisplay = xChar2
        End If
      End If
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext6 (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
    xFindNextItem xStep, False, UserDoes Or ForceFadeOut, False, (DoCrossFade And Not ForceFadeOut)
    '''''' DoCrossfade is only used in CheckAfter, ProcessMode 1&, and userdoes is passed
    '''''' UserDoes (UserDoes or ForceFadeOut)  will be sent as CanFade to xFindNextItem
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "PlayNext7 (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
    MainForm_LockMe False
    End If
  End If
End Sub

Private Function xFindNextItem(ByVal xStep As Long, ByVal DontPlay As Boolean, ByVal CanFade As Boolean, ByVal FindPLOnly As Boolean, ByVal DoCrossFade As Boolean) As Long
Dim xLong1 As Long
Dim xLong2 As Long
Dim xBool1 As Boolean
'#If TryPreventReInit Then
  'Dim xBool1 As Boolean
 ' #End If

#If TryPreventReInit Then
Lb1:
  #End If

If Not PLCount = 0& Then
  If FindPLOnly Then
    Call GetListIndexMS(xListHWnd(2), xLong1)
    If xLong1 = -1& Then xLong1 = 0&
    End If
  xLastSkipDirection = xStep

  xLong2 = DoEventsEx_GetTickCount
  Do
    If xRep = 2 Then
      xLong1 = RandomSong
      ElseIf FindPLOnly Then
      xLong1 = (xLong1 + xStep + PLCount) Mod PLCount
      Else
      xLong1 = (PLCurItem + xStep + PLCount) Mod PLCount
      End If
    If FindPLOnly Then
      'xLong1 = (xLong1 + xStep + PLCount) Mod PLCount
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
      If (PLIDF(xLong1) And PLI_IsDir) = 0& Then
#End If
        xFindNextItem = xLong1
        Exit Do 'Function
        End If
      Else
      'xLong1 = (PLCurItem + xStep + PLCount) Mod PLCount
      If xLong1 = 0& And xProcessMode = 2& And xRep = 1& And Not CanFade Then
        If xPStateIconPlaying Then
'        If Not DoCrossFade Then
          MainForm_LockMe True
          MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
          MainForm_LockMe False
'MainForm_ctConsoleA_AddLog "xFindNextItem (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItem1"
  #End If
          xPStateIconPlaying = False
          MainForm_UpdateMainIcon
          End If
        If Not PlNextSong = "" Then SetNextSong False
        Exit Do 'Function
        Else
        PLCurItem = xLong1
        End If
      
#If BoldDirs = 98 Then
      If (PLI(PLCurItem).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
      If (PLIDF(PLCurItem) And PLI_IsDir) = 0& Then
#End If
        If Not DontPlay Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItemA (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
            If xPlayItem(CanFade, False, DoCrossFade, CanFade, False, "") Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItemB (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
            '''''' userdoes is same as canfade
            '''''' (actually, it canfage is (userdoes and forcefadeout), but if forcefadeout,
            '''''' docrossfade is now false to prevent crossfade
            '''''' sending canfade (userdoes) as NoSFXWait to prevent waiting for old song on crossfade
            Exit Do
            End If
          Else
          If xFollowCurSong And Not (PLCount = 0& Or PLCurItem < 0&) And PLCurItem < PLCount And SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 2& Then
            xMarkSong PLCurItem, False, False
            xFollowCurSongLast = DoEventsEx_GetTickCount
            xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
            End If
          Exit Do
          End If
        End If
      End If
    #If TryPreventReInit Then
      If DoEventsEx_IsTickCountElapsed(xLong2, CannotPlayAbortTime) Or xBool1 Then
        If Not (xPStateIconPlaying Or xBool1) Then
          If FSOUND_GetError = SndBusy Then
            OutputChange False
            xBool1 = True
            GoTo Lb1
            End If
          End If
        End If
      #Else
    If DoEventsEx_IsTickCountElapsed(xLong2, CannotPlayAbortTime) Then
    'If Abs(Timer * 100& - xLong2) > CannotPlayAbortTime Then
        #End If

      
'      If Not DoCrossFade Then
'        xPStateIconPlaying = False
'        ShowIcon 0&
'        If Not PlNextSong = "" Then ShowIcon 2&
'        End If
      If xBool1 Then
        If CurMusic = ctSSLayer_MusicTypeNone Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItem2"
  #End If
          xPStateIconPlaying = False
          MainForm_UpdateMainIcon
          End If
        If Not PlNextSong = "" Then SetNextSong False
        Exit Do
        Else
        xBool1 = True
        End If
      End If
    If PLCount = 0& Then
      '1.00 beta 52 (endinging with crossfade will not stop properly if playlist gets empty)
      'from:
      'If Not DoCrossFade Then
      'to:
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItemC (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
      If (CurMusic = ctSSLayer_MusicTypeNone) And Not DoCrossFade Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItem3"
  #End If
        xPStateIconPlaying = False
        MainForm_UpdateMainIcon
        End If
      If Not PlNextSong = "" Then SetNextSong False
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "xFindNextItemD (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
  #End If
      Exit Do 'Function
      End If
    Loop
  End If
End Function

Private Sub MainForm_xEndSong(ByVal CanFadeOut As Boolean, ByVal ForceFadeOut As Boolean, ByVal SlowFadeout As Boolean)
  TrackerLoop = False
  
#If UseFileLogging Then
  If CurMusic = ctSSLayer_MusicTypeNone Then
    MainForm_ctConsoleA_AddLog "### Error 6310: xEndSong without music"
    MsgBox "### Error 6310: xEndSong without music"
    
    #If InIDE Then
      #End If
      ' Has once occured when deleting the current track immediatelly before it ends (no crossfade)
  '    Exit Sub
      ' Exit sub deactivated (on this occassion, playback stopped and first playlist entry was selected (useing exit sub)
    Else
#Else
  If Not CurMusic = ctSSLayer_MusicTypeNone Then
#End If
    If CanFadeOut And (xGenFading Or ForceFadeOut) And Not xPStateIconPaused Then 'Or Not (Tear_StepT(CurrentSongID) = 0&) Then
      If SlowFadeout Then
        If Not QuitMode = 0& Then
          Tear_StartDropping CurrentSongID, xSongIsMidi, 1&, 0&, LongFadingTotalTimeShut, 0&
          Else
          Tear_StartDropping CurrentSongID, xSongIsMidi, 1&, 0&, LongFadingTotalTimeQuit, 0&
          End If
        Else
        Tear_StartDropping CurrentSongID, xSongIsMidi, 1&, 0&, SimpleFadingTotalTime, 0&
        End If
      Else
      AllTears_Types(Tear_Type(CurrentSongID)) = AllTears_Types(Tear_Type(CurrentSongID)) + 1&
      Call Tear_Dropped(CurrentSongID)
      Call ctSSLayer_UnloadSong(SongPointer(CurrentSongID))
      'SongPointer(CurrentSongID) = 0&
      End If
    
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Sound stopped"
      #End If
      
    End If

  'MainForm_EraseBackgroundEx xStaticIconsHWnd(xStaticIcons_CurSong)
  ClearOldSongInfo
  
  
  
  CurMusic = ctSSLayer_MusicTypeNone
  DrawWhiteDC xStaticIconsHWnd(xStaticIcons_CurSong), xListHWnd(1), 0&, True
  MainForm_RedrawPosition
  
  xPStateIconPaused = False
  xEndSongTimerEventTime = -1&
  MainForm_UpdateMainIcon
  
  End Sub

Private Sub xInitMe(ByVal hWndCaller As Long, ByVal FailSave As Boolean, ByVal SpecMode As Boolean, ByVal InitRun As Boolean)
  Dim xLong1 As Long
  'Dim xLong2 As Long
  'Dim xChar1 As String

  DoEvents
  If xNoSpektrumOnStartup Then
    PlayerPriority = 0&
    End If
  Do
    xLong1 = ctSSLayer_GetDeviceFromSSLID(xOutputSSLID)
    If xLong1 = -1& Then xLong1 = 0&
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Preparing sound system (" & GetDeviceName(xLong1) & ")"
      #End If
    
    If ctSSLayer_Init(xLong1, MainHWnd, CDDAUse, CDDAJitter) Then
    'If False Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  Device: " + GetDeviceName(xLong1)
        MainForm_PrintCDDARev
        #End If
      If Not PlayerPriority = 0& Then
        UpdatePriority PlayerPriority
        End If
      KeepMusicAliveTime = ctSSLayer_CanUseItem(&HC&)
      MainForm_DesireSpectrumState True, True
      If Not ctSSLayer_CanUseItem(&H10&) = 0& Then
        #If UseFileLogging Then
          If ctSSLayer_CanUseItem(&H10&) = 1& Then
            MainForm_ctConsoleA_AddLog "Activating endsong callback support (partially supported by sound system)"
            Else
            MainForm_ctConsoleA_AddLog "Activating endsong callback support"
            End If
          #End If
        Call ctSSLayer_SetEndCallback(xStaticIconsHWnd(xStaticIcons_ShowIR), &H111&, -2&)
        #If UseFileLogging Then
          Else
          MainForm_ctConsoleA_AddLog "No endsong callback support"
          #End If
        End If
      Exit Do
      Else
'lbl1:
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  Error starting system"
        #End If
      GenericMessageHandler_Param = 2101&
      GlobalXInitRun = InitRun
      Select Case LoadResEx_Dialog(hWndCaller, 2101&, 2090&, 2091&, AddressOf GenericMessageHandler)
      'If MaxDeviceCount > 1& Then
        'If InitRun Then
      '  End If
        'Case 1890 'retry
        'Case 1892 'next
        '  xOutputSSLID ;; xOutputMode = (xOutputMode + 1&) Mod MaxDeviceCount
        '  If SetupVisible Then
        '    frmSetup.UpdateSnds
        '    End If
        Case 2091 'quit!
          If InitRun Then
            xInfoQuit = True
            If xCmdSaveSet Then
              If FailSave Then
                MainForm_SaveSettings 1&, SpecMode
                Else
                MainForm_SaveSettings 0&, SpecMode
                End If
              End If
            ctSSLayer_UnLoadVB
            MainForm_UnloadP
            Exit Do
            End If
        End Select
      End If
    Loop
  End Sub

Private Function GetRealIndex(ByRef Index As Long, ByVal SongName As String) As Boolean
  Dim xLong1 As Long
  
  If PLCount = 0& Then
    Index = 0&
    Else
    If Not Index < PLCount Then Index = PLCount - 1&
    If Index < 0 Then
      Index = 0
      Else
#If BoldDirs = 98 Then
      If PLI(Index).Filename = SongName Then
#Else
      If PLIFile(Index) = SongName Then
#End If
        GetRealIndex = True
        Else
        xLong1 = xFindSong(SongName)
        If Not xLong1 = 0& Then
          Index = xLong1
          GetRealIndex = True
          End If
        End If
      End If
    End If
  End Function

Public Function GetRelativeFilename(ByVal PlayListPath As String, ByVal Filename As String) As String
  Dim xLong1 As Long
  'Dim xLong2 As Long
  
  If Left(Filename, 1&) = "\" Then
    GetRelativeFilename = Left(PlayListPath, 2&) & Filename
    ElseIf Not PlayListPath = "" Then
    Do While Left(Filename, 3&) = "..\"
      xLong1 = InStrRev(PlayListPath, "\") - 1&
      If xLong1 = -1& Then
        PlayListPath = ""
        Exit Do
        Else
        PlayListPath = Left(PlayListPath, xLong1)
        Filename = Mid(Filename, 4&)
        End If
      Loop
    If Not PlayListPath = "" Then
      GetRelativeFilename = PlayListPath & "\" & Filename
      End If
    End If
  If GetRelativeFilename = "" Then
    GetRelativeFilename = Filename
    End If
  End Function

Private Sub ReadDirectory(ByVal BaseDir As String, ByVal Spaces As Long, ByVal IsMainDir As Boolean, ByVal AsAutoRefresh As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  'Dim xLong5 As Long
  Dim xLong10 As Long
  Dim xLong11 As Long
  Dim xChar1 As String
  'Dim xChar2 As String
    
  #If CanEnumComputerShares Then
    'Static lpBuff As Long
    Dim cbBuff As Long
    Dim mvNetRes As sNETRESOURCE
    #End If
  
  
  Dim xChar3 As String
  Static xArrayCount As Long
  Dim ReadDirectory_xArraySCount As Long
  Dim xType1 As WIN32_FIND_DATA
  'Dim xReadCDTracks As Boolean
  
  xLong11 = xArrayCount
  
  #If BoldDirs = 98 Then
    PLI(PLCount).Filename = BaseDir
    #Else
    PLIFile(PLCount) = BaseDir
    #End If
  
  xChar1 = BaseDir
  If Not IsMainDir Then
    ''xReadCDTracks = (CompatMode > 35999) And ((xUsedTypes And xUsedTypeIDcda) = xUsedTypeIDcda)
    ''xReadCDTracks = (xUsedTypes And xUsedTypeIDcda) = xUsedTypeIDcda
    'xReadCDTracks = Not (InStr(xUsedTypes, vbNullChar & "cda" & vbNullChar) = 0&)
    'Else
    xChar1 = SimpleFile(xChar1)
    End If
  
  #If BoldDirs = 98 Then
    PLI(PLCount).Caption = xChar1
    PLI(PLCount).DirDepthFlags = Spaces Or PLI_IsDir Or AsAutoRefresh
    #Else
    PLICap(PLCount) = xChar1
    PLIDF(PLCount) = Spaces Or PLI_IsDir Or AsAutoRefresh
    #End If
  PLCount = PLCount + 1&
  xLong10 = PLCount
  Spaces = Spaces + 1&

#If CanEnumComputerShares Then
  If Left(BaseDir, 2&) = "\\" And InStr(3&, BaseDir, "\") = 0& Then
    'xChar1 = BaseDir
'    xLong1 = Len(BaseDir)
    With mvNetRes
      .dwScope = 2&
      .dwType = 1&
      .dwDisplayType = 2&
      .dwUsage = 2&
'      .lpRemoteName = LocalAlloc(&H40&, xLong1 + 1&)
      .lpRemoteName = LocalAllocFromString(BaseDir)
'      Call RtlMoveMemory(ByVal .lpRemoteName, ByVal BaseDir, xLong1)
      End With
    
    If Not mvNetRes.lpRemoteName = 0& Then
      If IsUnicode Then
        xLong2 = WNetOpenEnumW(&H2&, &H1&, 0&, mvNetRes, xLong1)
        Else
        xLong2 = WNetOpenEnumA(&H2&, &H1&, 0&, mvNetRes, xLong1)
        End If
      Call LocalFree(mvNetRes.lpRemoteName)
      If xLong2 = 0& Then
        'Call LocalFree(mvNetRes.lpRemoteName)
        'cbBuff = 1024&
        'lpBuff = LocalAlloc(&H40&, cbBuff)
        Do
          xLong2 = &HFFFFFFFF
          If IsUnicode Then
            cbBuff = ptrTempSizeXW
            xLong3 = WNetEnumResourceW(xLong1, xLong2, ptrTemp, cbBuff)
            Else
            cbBuff = ptrTempSizeX
            xLong3 = WNetEnumResourceA(xLong1, xLong2, ptrTemp, cbBuff)
            End If
          If xLong3 = 0& Then
            For xLong3 = 0& To xLong2 - 1&
              Call RtlMoveMemory(mvNetRes, ByVal (ptrTemp + xLong3 * sNETRESOURCE_Len), sNETRESOURCE_Len)
              xChar1 = LCase(Mid(PeekSA(mvNetRes.lpRemoteName), Len(BaseDir) + 2&))
              If Not (Left(xChar1, 1&) = "." Or (Left(xChar1, 1&) = "#" Or Left(xChar1, 1&) = "_") And IgnoreUnderscored) Then
                If xArrayCount < sCharA1Max Then
                  sCharA1(xArrayCount) = BaseDir & "\" & xChar1
                  xArrayCount = xArrayCount + 1&
                  End If
                End If
              Next
            End If
          Loop Until xLong2 = 0&
        Call WNetCloseEnum(xLong1)
        'LocalFree lpBuff
        'Else
        'Call LocalFree(mvNetRes.lpRemoteName)
        End If
      End If
  
  
    Else
#End If
    xLong1 = FindFirstFile(BaseDir & "\*.*", xType1, &H3F&)
    If Not xLong1 = -1& Then
      Do
        xChar1 = xType1.Filename
        If Not (Left(xChar1, 1&) = "." Or ((xType1.dwFileAttributes And 2&) = 2& Or Left(xChar1, 1&) = "#" Or Left(xChar1, 1&) = "_") And IgnoreUnderscored) Then
          If (xType1.dwFileAttributes And 16&) = 16& Then
            If (xType1.dwFileAttributes And 20&) = 16& And xArrayCount < sCharA1Max Then
              sCharA1(xArrayCount) = BaseDir & "\" & xChar1
              xArrayCount = xArrayCount + 1&
              End If
            ElseIf ReadDirectory_xArraySCount < ReadDirectory_xArraySMax Then
            If AllowedItemSongFast(xChar1, False) Then
              ReadDirectory_xArrayS(ReadDirectory_xArraySCount) = xChar1
              ReadDirectory_xArraySCount = ReadDirectory_xArraySCount + 1&
              'If xLong2 = 4& Then xReadCDTracks = False
              End If
            End If
          If ((ReadDirectory_xArraySCount + xArrayCount) Mod 64&) = 3& Then Call DoEventsEx_SleepPeriodicalLW(0&)
          'If ((ReadDirectory_xArraySCount + xArrayCount) Mod 64&) = 3& Then DoEventsEx_SleepPeriodical
          End If
        Loop Until FindNextFile(xLong1, xType1) = 0&
      Call FindClose(xLong1)
      End If
#If CanEnumComputerShares Then
    End If
#End If

'DoEventsEx_SleepPeriodical
Call DoEventsEx_SleepPeriodicalLW(0&)

'If xReadCDTracks Then


xChar3 = BaseDir & "\"

If xSorting Then
  Call ArraySort_String_IC(ReadDirectory_xArrayS(), ReadDirectory_xArraySCount)
  End If
For xLong1 = 0& To ReadDirectory_xArraySCount - 1&
  xChar1 = ReadDirectory_xArrayS(xLong1)
  #If BoldDirs = 98 Then
    PLI(PLCount).Filename = xChar3 & ReadDirectory_xArrayS(xLong1)
    PLI(PLCount).Caption = ReadDirectory_xArrayS(xLong1)
    PLI(PLCount).DirDepthFlags = Spaces Or AsAutoRefresh
    #Else
    PLIFile(PLCount) = xChar3 & ReadDirectory_xArrayS(xLong1)
    PLICap(PLCount) = ReadDirectory_xArrayS(xLong1)
    PLIDF(PLCount) = Spaces Or AsAutoRefresh
    #End If
  PLCount = PLCount + 1&
  If PLCount = PLCountMax Then Exit For
  If xLong1 Mod 32& = 0& Then
    Call DoEventsEx_SleepPeriodicalLW(0&)
    If AbortReading Then Exit For
    End If
  Next

'If xReadCDTracks And PLCount < PLCountMaxX Then
'  If Not InStr(CDDriveLetters, UCase(Left(BaseDir, 1&))) = 0 Then
'    'xLong2 = FSOUND_CD_GetNumTracks(CByte(Asc(Left(BaseDir, 1&))))
'    xLong2 = ctSSLayer_NumCDTracks(Left(BaseDir, 1&))
'    If xLong2 > 1& Then
'      PLItem(PLCount) = vbNullChar & Chr(AsAutoRefresh) & BaseDir
'      PLCap(PLCount) = Space(2&) & Chr(187&) & " " & LoadResex_String(28&) 'xChar1
'      PLCount = PLCount + 1&
'      For xLong2 = 2& To xLong2
'        xChar1 = Right("00" & CStr(xLong2), 2&)
'        PLItem(PLCount) = Chr(1&) & Chr(AsAutoRefresh) & BaseDir & "\Track" & xChar1 & ".cda"
'        PLCap(PLCount) = Space(6&) + Replace(LoadResex_String(29&), "$", xChar1)
'        'PLCap (PLCount)
'        'ReadDirectory_xArrayS(ReadDirectory_xArraySCount) = Chr(xLong2) & xChar1
'        'ReadDirectory_xArraySCount = ReadDirectory_xArraySCount + 1&
'        PLCount = PLCount + 1&
'        If PLCount = PLCountMax Then Exit For
'        Next
'      End If
'    End If
'  End If



'DoEvents
Call DoEventsEx_SleepPeriodicalLW(0&)

If Not AbortReading And PLCount < PLCountMaxX Then
  If xSorting Then
    Call ArraySort_String_StartPos_IC(sCharA1(), xArrayCount - xLong11, xLong11)
    End If
  For xLong1 = xLong11 To xArrayCount - 1&
'    xLong5 = xLong1
    'DoEvents
    Call DoEventsEx_SleepPeriodicalLW(0&)
    If AbortReading Or PLCount > PLCountMaxX2 Then Exit For
    ReadDirectory sCharA1(xLong1), Spaces, 0&, AsAutoRefresh
    Next
  End If

If xLong10 = PLCount Then
  PLCount = PLCount - 1&
  End If

xArrayCount = xLong11
'If xlong11 = 0& Then
'  Erase sCharA1()
'  End If

'End If
End Sub

Public Sub DoVolume(ByVal VolumeToSet As Long, ByVal ApplyVolume As Boolean)
  Static xOldVolume As Long
  
  If ApplyVolume Then
    xSetVolume = VolumeToSet
    MainForm_DrawVolFaderA xSetVolume, 0&
    'CheckApiMenu 345& + xOldVolume, False
    xOldVolume = CInt(VolumeToSet / 25.5)
    Call RadioApiMenu(345&, 355&, 345& + xOldVolume)
    'CheckApiMenu 345& + xOldVolume, True
    
    If Not vKioskMode Or vCanVolume Then
      MainForm_EnableApiMenu 341&, (Not xSetVolume = 255&)
      MainForm_EnableApiMenu 342&, (Not xSetVolume = 0&)
      End If
    
    
    #If UseFileLogging And DetailLog Then
      MainForm_ctConsoleA_AddLog "Volume: " & CStr(xSetVolume) & " (" & CInt(xSetVolume / 2.55) & "%)"
      #End If
    PlayerIsLocked = False
    End If
  
  If Tear_StepT(CurrentSongID) = 0& And Not CurMusic = ctSSLayer_MusicTypeNone Then
    'Tear_Volume(CurrentSongID) = VolumeToSet
    Call ctSSLayer_SetVolume(SongPointer(CurrentSongID), VolumeToSet)
    End If
  
  SetToolTip xStaticIcons_Volume, Replace(Replace(Replace(LoadResEx_String(1150&), "$V", CStr(CInt(xSetVolume * 100 / 255))), "$C", GetKeyNameText(MapVirtualKey(&H11&, 0&) * &H10000)), "$S", GetKeyNameText(MapVirtualKey(&H10&, 0&) * &H10000)), xVolTTText
  End Sub


Public Sub MainForm_ApplyRepMode()
  'CheckApiMenu 110&, (xRep = 0&)
  'CheckApiMenu 111&, (xRep = 1&)
  'CheckApiMenu 112&, (xRep = 2&)
  Call RadioApiMenu(110&, 112&, 110& + xRep)
  
  MainForm_EnableApiMenu 322&, (Not xRep = 0&)
  MainForm_EnableApiMenu 119&, (Not xRep = 0&)
  'mainform_EnableApiMenu 331&, (Not xRep = 0&)
  'mainform_EnableApiMenu 332&, (Not xRep = 0&)
  
  'CheckApiMenu 301&, IntroModeActive
  CheckApiMenu 119&, IntroModeActive
  
  If CurMusic = ctSSLayer_MusicTypeTracker And Not xSongIsMidi Then
    Call ctSSLayer_UpdateLooping(SongPointer(CurrentSongID), ((xRep = 0&) And PlNextSong = ""))
    End If
  
  'eImage(22).Caption = LoadResEx_String(1160& + xRep)
  If IntroModeActive Then
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_RepMode, DynButtonsResXRep, &H10000000 + xRep * &H1000000)
    Else
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_RepMode, DynButtonsResXRep, xRep * &H1000000)
    End If
  SetToolTip xStaticIcons_RepMode, Replace(LoadResEx_String(1160&), "$", LoadResEx_String(1162& + xRep * 2& - CLng(IntroModeActive))), xModeTTText
  End Sub

Private Sub xMarkSong(ByVal ItemID As Long, ByVal SkipUnmarking As Boolean, ByVal NoListMarkX As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long

  If Not (PLCount = 0& Or xAutoDoing) And ItemID < PLCount Then
    If Not SkipUnmarking Then
      Call SendMessage(xListHWnd(2), &H185, 0&, ByVal -1&)
      End If

    xLong1 = SendMessage(xListHWnd(2), LB_GETTOPINDEX, 0&, 0&)
    xLong2 = lPlayList_Height \ SendMessage(xListHWnd(2), LB_GETITEMHEIGHT, 0&, 0&)
    If ItemID < xLong1 Or ItemID > xLong1 + xLong2 Then
      xLong2 = ItemID - xLong2 \ 2&
      If xLong2 < 0& Then xLong2 = 0&
      If Not xLong2 = xLong1 Then
        Call SendMessage(xListHWnd(2), &H197&, xLong2, 0&)
        End If
      ElseIf ItemID < xLong1 + Fix(xLong2 / 8&) Then
      xLong2 = ItemID - Fix(xLong2 / 8&)
      If xLong2 < 0& Then xLong2 = 0&
      If Not xLong2 = xLong1 Then
        Call SendMessage(xListHWnd(2), &H197&, xLong2, 0&)
        End If
      ElseIf ItemID > xLong1 + Fix(xLong2 * 7& / 8&) Then
      xLong2 = ItemID - Fix(xLong2 * 7& / 8&)
      If xLong2 < 0& Then xLong2 = 0&
      If Not xLong2 = xLong1 Then
        Call SendMessage(xListHWnd(2), &H197&, xLong2, 0&)
        End If
      End If
  
    Call SendMessage(xListHWnd(2), &H19E&, ItemID, 0&)
    Call SendMessage(xListHWnd(2), &H185&, 1&, ItemID)
    If Not NoListMarkX Then
      MainForm_CheckDir False
      End If
    End If
  End Sub

Private Function xFindSong(Optional ByVal SongNameA As String) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long

  If SongNameA = "" Then SongNameA = PLCurSong
  SongNameA = UCase(SongNameA)
  If Not PLCurItem < PLCount Then PLCurItem = 0&
#If BoldDirs = 98 Then
  If UCase(PLI(PLCurItem).Filename) = SongNameA And (PLI(PLCurItem).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
  If UCase(PLIFile(PLCurItem)) = SongNameA And (PLIDF(PLCurItem) And PLI_IsDir) = 0& Then
#End If
    xFindSong = PLCurItem
    ElseIf Not PLCount = 0& Then
'    Debug.Print "SearchFor: >" + SongNameA + "<"
    xLong2 = Len(SongNameA)
    For xLong1 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
      If Len(PLI(xLong1).Filename) = xLong2 Then
'        Debug.Print "Search: LenOK >" + PLIFile(xLong1) + "<"
        If UCase(PLI(xLong1).Filename) = SongNameA Then
'          Debug.Print "Search: Found >" + PLIFile(xLong1) + "<"
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then
'            Debug.Print "Search: DirFLOk >" + PLIFile(xLong1) + "<"
#Else
      If Len(PLIFile(xLong1)) = xLong2 Then
'        Debug.Print "Search: LenOK >" + PLIFile(xLong1) + "<"
        If UCase(PLIFile(xLong1)) = SongNameA Then
'          Debug.Print "Search: Found >" + PLIFile(xLong1) + "<"
          If (PLIDF(xLong1) And PLI_IsDir) = 0& Then
'            Debug.Print "Search: DirFLOk >" + PLIFile(xLong1) + "<"
#End If
            xFindSong = xLong1
            Exit For
            End If
          End If
        End If
      Next
    End If
  End Function

Public Sub MainForm_xAutoRefreshNow(ByVal xSkipFindSong As Boolean, ByVal ForceNow As Boolean, ByVal NoPListAdding As Boolean, ByVal NoRemove As Boolean, ByVal ReSetAutoDirsAPI As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  ReAutoPlayDR = False
  If Not xAutoDoing Or ForceNow Then
  '  Do While xAutoDoing: DoEvents: Loop
    MainForm_LockMe True
    
    If Not NoPListAdding Then
      InitResearchClear
      End If
    If xAutoDirCount = 0& Then
      If ForceNow And Not NoRemove Then
        SetAutoDoing True
        xRemoveAutoRefresh PLI_IsAuto, NoPListAdding, False
        SetAutoDoing False
        End If
      If Not NoPListAdding Then
        MainForm_xBuildDirs
        End If
      MainForm_LockMe False
      If Not NoPListAdding Then
        InitResearch False
        End If
      
      
      
      
      Else
    
      xPStateIconRefreshing = True
      MainForm_UpdateMainIcon
      DoEvents
      xLong1 = 0&
      SetAutoDoing True
      If Not NoPListAdding Then
        ClearPLEx
        End If
      If Not NoRemove Then
        xRemoveAutoRefresh 0&, True, True
        End If
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Performing autorefresh"
        #End If
    
    
      AbortReading = False
      If ReSetAutoDirsAPI Then MainForm_SetAutoDirs False
      For xLong1 = 0& To xAutoDirCount - 1&
        'AbortReading = False
        ReadDirectory xAutoDir(xLong1), 0&, True, PLI_IsAuto
        If AbortReading Then Exit For '''''''
        Next
      If ReSetAutoDirsAPI Then MainForm_SetAutoDirs True
      '''''''''''''''''''If PLCount > 0 Then CheckEmptyNodes True
      'If AbortReading Then CheckEmptyNodes True, -1&
    
      If Not NoPListAdding Then
        RealPLAdd 0&
        End If
    
      xPStateIconRefreshing = False
      MainForm_UpdateMainIcon
      SetAutoDoing False
      
      If Not (xSkipFindSong Or NoPListAdding) Then
        xLong1 = xFindSong
        If Not xLong1 = 0& Then
          PLCurItem = xLong1
          xLong2 = xFollowCurSongLastDiff
          xMarkSong xLong1, False, False
          xFollowCurSongLast = DoEventsEx_GetTickCount
          If xLong1 < xFollowCurSongLastDiffDef Then
            xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
            Else
            xFollowCurSongLastDiff = xLong2
            End If
          End If
        End If
      MainForm_LockMe False
      If Not NoPListAdding Then
        InitResearch False
        CheckReplay
        End If
      End If
    
    CheckOldItems
    xAutoRefreshTimerEventTime(0) = DoEventsEx_GetTickCount
    xAutoRefreshTimerEventTime(1) = xAutoRefreshTimerEventTime(0)
    End If
  End Sub

Private Sub xRemoveAutoRefresh(ByVal WhatToRemove As Long, ByVal NoUpdating As Boolean, ByVal NoCheckOldItems As Boolean)
  Dim xLong1 As Long
'Dim xLong2 As Long
'Dim xChar1 As String
  Dim xCaret As Long
  Dim xCaretOld As Long

  ReAutoPlayDR = False
  
  If WhatToRemove = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Removing Non-Refresh songs"
      #End If
    Else
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Removing AutoRefresh songs"
      #End If
    End If
  
  xLong1 = PLCount
  PLCount = 0&
  If NoUpdating Then
    For xLong1 = 0& To xLong1 - 1&
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = WhatToRemove Then
        PLI(PLCount) = PLI(xLong1)
        Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
#Else
      If (PLIDF(xLong1) And PLI_IsAuto) = WhatToRemove Then
        PLIFile(PLCount) = PLIFile(xLong1)
        PLICap(PLCount) = PLICap(xLong1)
        PLIDF(PLCount) = PLIDF(xLong1)
        PLIPxLg(PLCount) = PLIPxLg(xLong1)
        PLIPxSm(PLCount) = PLIPxSm(xLong1)
        Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
#End If
        PLCount = PLCount + 1&
        End If
      Next
    Else
    InitResearchClear
    If Not LockWinUpd = 0& Then
      LockWindowUpdate xListHWnd(2)
      End If
    xCaret = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
    xCaretOld = xCaret
    For xLong1 = 0& To xLong1 - 1&
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = WhatToRemove Then
#Else
      If (PLIDF(xLong1) And PLI_IsAuto) = WhatToRemove Then
#End If
        'RemoveListItemMS
        'PLRemoveShifted1 = PLCount
        'PLRemoveShifted2 = xLong1
        Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
        If xCaret >= PLCount Then xCaret = xCaret - 1&
        Else
        #If BoldDirs = 98 Then
          PLI(PLCount) = PLI(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
          #Else
          PLIFile(PLCount) = PLIFile(xLong1)
          PLICap(PLCount) = PLICap(xLong1)
          PLIDF(PLCount) = PLIDF(xLong1)
          PLIPxLg(PLCount) = PLIPxLg(xLong1)
          PLIPxSm(PLCount) = PLIPxSm(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
          #End If
        PLCount = PLCount + 1&
        End If
      #If LockWinUpd = 0 Then
        If xLong1 Mod 128& = 0& Then Call DoEventsEx_SleepPeriodicalLW(0&)
        #End If
      'If xLong1 Mod 128& = 0& Then
      '  If LockWinUpd = 0& Then
      '    DoEventsEx_SleepPeriodical
      '    ElseIf LockWinUpd = 1& Then
      '    DoEventsEx_SleepPeriodicalLW xListHWnd(2)
      '    End If
      '  End If
      Next
    'PLRemoveShifted1 = &H7FFFFFFF
    If Not (PLCount = 0& Or xCaret = xCaretOld) Then
      If xCaret > PLCount - 1& Then xCaret = PLCount - 1&
      If xCaret < 0& Then xCaret = 0&
      Call SendMessage(xListHWnd(2), &H19E&, xCaret, 0&)
      End If
    If Not LockWinUpd = 0& Then
      LockWindowUpdate 0&
      End If
    InitResearch False
    End If
  If Not NoCheckOldItems Then CheckOldItems
  End Sub

Private Sub CheckThisSong(ByVal FileID As Long)  '(ByVal FileID As Long) ' Always PLCurItem
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xChar1 As String
  'Dim xChar2 As String
  'Dim xBool1 As Boolean
  'Dim FileID As Long
  
  'FileID = PLCurItem

  #If BoldDirs = 98 Then
    xChar1 = PLI(FileID).Filename
    #Else
    xChar1 = PLIFile(FileID)
    #End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Checking file: " & xChar1
    #End If
  
  If Not FileExists(xChar1) Then
#If BoldDirs = 98 Then
    If (PLI(FileID).DirDepthFlags And PLI_IsAuto) = PLI_IsAuto Then
#Else
    If (PLIDF(FileID) And PLI_IsAuto) = PLI_IsAuto Then
#End If
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  not found. is autorefresh"
        #End If
      xPStateIconRefreshing = True
      MainForm_UpdateMainIcon
      MainForm_xAutoRefreshNow False, True, False, False, True
      MainForm_LockMe True
      If PLCount = 0& And CurMusic = ctSSLayer_MusicTypeNone Then
        xPStateIconPlaying = False
        End If
      xPStateIconRefreshing = False
      MainForm_UpdateMainIcon
      Else
      If xRemoveMissingSongs Then
        If Not xAutoDoing Then
          'InitResearch false
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  not found. removing"
            #End If
          xLong4 = FileID
          #If BoldDirs = 98 Then
            xLong1 = (PLI(FileID).DirDepthFlags And PLI_Depth) - 1&
            #Else
            xLong1 = (PLI(FileID).DirDepthFlags And PLI_Depth)
            #End If
          xLong2 = FileID
'DoEventsEx_Sleep 5000
          Do
            xLong2 = xLong2 - 1&
'            Exit Do
#If BoldDirs = 98 Then
            If (PLI(xLong2).DirDepthFlags And PLI_Depth) = xLong1 Then
#Else
            If (PLI(xLong2).DirDepthFlags And PLI_Depth) = xLong1 Then
#End If
              #If BoldDirs = 98 Then
                xLong3 = GetFileAttributes(PLI(xLong2).Filename)
                #Else
                xLong3 = GetFileAttributes(PLIFilename(xLong2))
                #End If
              
              If xLong1 = 0& Then
                #If UseFileLogging Then
                  MainForm_ctConsoleA_AddLog "ExtRemoving 0"
                  #End If
                If xLong3 = -1& Or (xLong3 And 16&) = 0& Then
                  xLong4 = xLong2
                  End If
                Exit Do
                Else
                If xLong3 = -1& Or (xLong3 And 16&) = 0& Then
                  #If UseFileLogging Then
                    MainForm_ctConsoleA_AddLog "ExtRemoving -"
                    #End If
                  xLong1 = xLong1 - 1&
                  xLong4 = xLong2
                  Else
                  #If UseFileLogging Then
                    MainForm_ctConsoleA_AddLog "ExtRemoving \"
                    #End If
                  xLong1 = xLong1 + 1&
                  Exit Do
                  End If
                End If
              End If
            Loop
          If xLong4 = FileID Then
            xLong3 = 1&
            Else
            xLong3 = PLCount - 1&
            For xLong2 = FileID + 1& To PLCount - 2&
#If BoldDirs = 98 Then
              If (PLI(xLong2).DirDepthFlags And PLI_Depth) <= xLong1 Then
#Else
              If (PLI(xLong2).DirDepthFlags And PLI_Depth) <= xLong1 Then
#End If
                xLong3 = xLong2 - 1&
                Exit For
                End If
              Next
'Debug.Print "RemoveThis!", xLong4, xLong3
            xLong3 = xLong3 - xLong4 + 1&
            End If
          
'Debug.Print "RemoveThis!", xLong4, xLong3
          
          If xLong3 = 1& Then
            RemoveFromSearch xLong4
            Else
            InitResearchClear
            End If
          For xLong1 = xLong4 To PLCount - xLong3 - 1&
            #If BoldDirs = 98 Then
              PLI(xLong1) = PLI(xLong1 + xLong3)
              Call SendMessage(xListHWnd(2), &H19A&, xLong1 + xLong3, xLong1)
              #Else
              PLIFile(xLong1) = PLIFile(xLong1 + xLong3)
              PLICap(xLong1) = PLICap(xLong1 + xLong3)
              PLIDF(xLong1) = PLIDF(xLong1 + xLong3)
              PLIPxLg(xLong1) = PLIPxLg(xLong1 + xLong3)
              PLIPxSm(xLong1) = PLIPxSm(xLong1 + xLong3)
              Call SendMessage(xListHWnd(2), &H19A&, xLong1 + xLong3, xLong1)
              #End If
            Next
          For xLong1 = xLong4 + xLong3 - 1& To xLong4 Step -1&
            RemoveListItemMS xListHWnd(2), xLong1
            Next
          PLCount = PLCount - xLong3
          'FileID = FileID - xlong3
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "BeforeCheckThisSong: " & BoolChar(SendMessage(xListHWnd(2), LB_GETCOUNT, 0&, 0&) = PLCount)
            #End If
          If Not PLCurItem = 0& Then
            If PLCurItem >= xLong4 + xLong3 Then
              PLCurItem = PLCurItem - xLong3
              ElseIf PLCurItem >= xLong4 Then
              PLCurItem = xLong4 - 1&
              End If
'            Stop
            If PLCurItem < 0& Then PLCurItem = 0&
            End If
          If Not PlNextSong = "" Then
            If PLNext >= xLong4 + xLong3 Then
              PLNext = PLNext - xLong3
              ElseIf PLNext >= xLong4 Then
              SetNextSong False
              End If
            If PLNext < 0& Then PLNext = 0&
            End If
DoneLargeRemoving:
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "BeforeCheckEmptyNodes: " & BoolChar(SendMessage(xListHWnd(2), LB_GETCOUNT, 0&, 0&) = PLCount)
            #End If
          If xLong3 = 1& Then
            CheckEmptyNodes False, xLong4 ' -1&
            Else
            CheckEmptyNodes False, -1&
            InitResearch False
            End If
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "AfterCheckEmptyNodes: " & BoolChar(SendMessage(xListHWnd(2), LB_GETCOUNT, 0&, 0&) = PLCount)
            #End If
'If PLCount < 3& Then Stop
          '1.00 beta 52 (endinging with crossfade will not stop properly if playlist gets empty)
          'from:
          'If PLCount = 0& Then
          'to:
          If PLCount = 0& And CurMusic = ctSSLayer_MusicTypeNone Then
            xPStateIconPlaying = False
            MainForm_UpdateMainIcon
            'Exit Sub
            End If
          #If UseFileLogging Then
            Else
            MainForm_ctConsoleA_AddLog "  not found. cannot remove currently"
            #End If
          End If
        #If UseFileLogging Then
          Else
          MainForm_ctConsoleA_AddLog "  not found."
          #End If
        End If
      End If
    End If
  
  
  End Sub

Private Sub xDeleteFileNow(ByVal DeleteCurrentSong As Boolean)
  Dim xType1 As SHFILEOPSTRUCT
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  'Dim xLong4 As Long
  'Dim xLong5 As Long
  'Dim xLong6 As Long
  'Dim xLong7 As Long
  'Dim xLong8 As Long
  'Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  Dim xChar4 As String
  Dim xBool1 As Boolean
  Dim xBool2 As Boolean
  Dim xCaret As Long
  Dim xCaretOld As Long

  'If xDelMode And Not (xAutoDoing Or xSecureMode Or (xFollowCurSong And Not DoEventsEx_IsTickCountElapsed(xFollowCurSongLast, xFollowCurSongLastDiff))) Then
  'If Not DoEventsEx_IsTickCountElapsed(xFollowCurSongLast, xFollowCurSongLastDiff) Then
    'ElseIf xDelMode And Not (xAutoDoing Or xSecureMode Or (xFollowCurSong And Not DoEventsEx_IsTickCountElapsed(xFollowCurSongLast, xFollowCurSongLastDiff))) Then
  If xDelMode And Not (PlayerIsLocked Or xAutoDoing Or xSecureMode) And DoEventsEx_IsTickCountElapsed(xFollowCurSongLast, xFollowCurSongLastDiff) Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Deleting files"
      MainForm_ctConsoleA_AddLog "  Preparing to delete files"
      #End If

'''    xTimerIsEnabled = False
    MainForm_LockMe True
    'SetAutoDoing True
'            xPStateIconRefreshing = True
'            ShowIcon 0&
    xChar2 = UCase(PLCurSong)
    xChar3 = UCase(PlNextSong)
  
    If DeleteCurrentSong Then
      If Not (AllTears_TypesCount > 2& Or PLCurSong = "" Or CurMusic = ctSSLayer_MusicTypeNone) And FileExists(PLCurSong) Then
        xBool1 = True
        xChar4 = vbNullChar & PLCurSong
        sCharA1(0) = UCase(PLCurSong)
        xLong1 = 1&
        If xChar2 = xChar3 Then
          SetNextSong False
          End If
        End If
      Else
      For xLong2 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
        If (PLI(xLong2).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
        If (PLIDF(xLong2) And PLI_IsDir) = 0& Then
#End If
          If SendMessage(xListHWnd(2), &H187&, xLong2, 0&) Then
#If BoldDirs = 98 Then
            sCharA1(xLong1) = PLI(xLong2).Filename
            If Not SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) = xLong1 Then
#Else
            sCharA1(xLong1) = PLIFile(xLong2)
            If Not SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) = xLong1 Then
#End If
              MsgBox "Playlist is out of sync, please report this error." & vbCr & "Deleting is aborted", vbCritical + vbOKOnly, "Error"
              MainForm_LockMe False
              Exit Sub
              End If
            For xLong3 = 0& To xLong1 - 1&
              If sCharA1(xLong3) = UCase(sCharA1(xLong1)) Then
                sCharA1(xLong1) = ""
                Exit For
                End If
              Next
            If Not sCharA1(xLong1) = "" Then
              xChar4 = xChar4 + vbNullChar + sCharA1(xLong1)
              sCharA1(xLong1) = UCase(sCharA1(xLong1))
              If xChar2 = sCharA1(xLong1) Then xBool1 = True
              If xChar3 = sCharA1(xLong1) And Not xChar3 = "" Then
                SetNextSong False
                xChar3 = ""
                End If
              xLong1 = xLong1 + 1&
              End If
            End If
          End If
        Next
      If (CurMusic = ctSSLayer_MusicTypeNone) Then xBool1 = False
      End If
  
#If UseFileLogging Then
    If xLong1 = 0& Then
      MainForm_ctConsoleA_AddLog "  no files found"
      Else
#Else
    If Not xLong1 = 0& Then
#End If
      xPStateIconDeleting = True
      MainForm_UpdateMainIcon
      
      
      xForceAnimationNow = True
      If xBool1 Or CurMusic = ctSSLayer_MusicTypeNone Then 'And xPStateIconPlaying Then
        If xBool1 Then Call StoreResume(False, 500&)
        Call Tear_WaitFor(-2&)
        Else
        Call Tear_WaitFor(-3&)
        End If
      
      xType1.wFunc = &H3&
      xType1.fFlags = &H40& ' &HC0&
      
      xType1.From = Mid(xChar4, 2&) & vbNullChar & vbNullChar ' One should be enough?
      Call DoEventsEx_SleepPeriodicalLW(0&)
      
      
      
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  Deleting from medium"
        #End If


      Call SHFileOperation(xType1)
      Call DoEventsEx_SleepPeriodicalLW(0&)
      
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  returned " & CStr(xType1.fAnyOperationsAborted)
        #End If
      
      xForceAnimationNow = False
      
      xLong2 = xLong1 - 1&
      xLong1 = 0&
      For xLong2 = 0& To xLong2
        If Not FileExists(sCharA1(xLong2)) Then
          sCharA1(xLong1) = sCharA1(xLong2)
          xLong1 = xLong1 + 1&
          End If
        Next
      If Not xLong1 = 0& Then
        xLong2 = PLCount - 1&
        PLCount = 0&
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  Removing from list"
          #End If

        SetAutoDoing True
        
        
        xChar4 = UCase(PLCurSong)
        xCaret = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
        xCaretOld = xCaret
        For xLong2 = 0& To xLong2
          #If BoldDirs = 98 Then
            xChar3 = UCase(PLI(xLong2).Filename)
            #Else
            xChar3 = UCase(PLIFile(xLong2))
            #End If
          If PLCurItem = xLong2 Then PLCurItem = PLCount ': xBool2 = True
          'If UCase(PLCurSong) = xChar3 Then xBool2 = True
          If xChar3 = "" Then
            #If BoldDirs = 98 Then
              PLI(PLCount) = PLI(xLong2)
              Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
              #Else
              PLIFile(PLCount) = PLIFile(xLong2)
              PLICap(PLCount) = PLICap(xLong2)
              PLIDF(PLCount) = PLIDF(xLong2)
              PLIPxLg(PLCount) = PLIPxLg(xLong2)
              PLIPxSm(PLCount) = PLIPxSm(xLong2)
              Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
              #End If
            If PLNext = xLong2 Then PLNext = PLCount
            PLCount = PLCount + 1&
            Else
            For xLong3 = 0& To xLong1 - 1&
              If Len(xChar3) = Len(sCharA1(xLong3)) Then
                If xChar3 = sCharA1(xLong3) Then
                  If xChar4 = xChar3 Then xBool2 = True
                  xChar3 = ""
                  If PLNext = xLong2 And Not PlNextSong = "" Then
                    SetNextSong False
                    End If
                  'RemoveListItemMS
                  RemoveFromSearch PLCount
                  'PLRemoveShifted1 = PLCount
                  'PLRemoveShifted2 = xLong2
                  Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&) 'PLCount + 1
                  If xCaret >= PLCount Then xCaret = xCaret - 1&
                  Exit For
                  End If
                End If
              If xLong3 Mod 128& = 0& Then Call DoEventsEx_SleepPeriodicalLW(0&)
              Next
            'PLRemoveShifted1 = &H7FFFFFFF

            If Not xChar3 = "" Then
              #If BoldDirs = 98 Then
                PLI(PLCount) = PLI(xLong2)
                Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
                #Else
                PLIFile(PLCount) = PLIFile(xLong2)
                PLICap(PLCount) = PLICap(xLong2)
                PLIDF(PLCount) = PLIDF(xLong2)
                PLIPxLg(PLCount) = PLIPxLg(xLong2)
                PLIPxSm(PLCount) = PLIPxSm(xLong2)
                Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
                #End If
              If PLNext = xLong2 Then PLNext = PLCount
              PLCount = PLCount + 1&
              End If
            End If
          Next
        
        If Not (PLCount = 0& Or xCaret = xCaretOld) Then
          If xCaret > PLCount - 1& Then xCaret = PLCount - 1&
          If xCaret < 0& Then xCaret = 0&
          Call SendMessage(xListHWnd(2), &H19E&, xCaret, 0&)
          End If
        
        CheckEmptyNodes False, -1&
        SetAutoDoing False
        
        MainForm_xBuildDirs
        CheckOldItems
        End If

      xPStateIconDeleting = False
      MainForm_UpdateMainIcon




      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  post processing"
        #End If
      If xBool1 Then
        If xBool2 Then
          Call StoreResume(True, 1&)
          Else
          Call StoreResume(True, 0&)
          End If
        End If
      End If
    MainForm_LockMe False
    End If
  End Sub

Private Sub MainForm_xSelDirNow()
  Dim xLong1 As Long
  Dim xLong2 As Long

  If Not PLCount = 0& Then
    xLong1 = SendMessage(xListHWnd(0), &H188&, 0&, 0&)
    If Not xLong1 = -1& Then
      xLong2 = xDirPath(xLong1)
      If xLong1 < PLCount And PLCount > 1& Then
        xMarkSong PLCount - 1&, True, True
        xMarkSong xLong2, False, True
        MainForm_CheckDir False, True
  '      eLabel(8).Caption = eDropDown(0).List(xLong1)
        End If
      End If
    End If
  End Sub

Private Sub MainForm_UpdateMainIcon()
  Dim xLong1 As Long
  Static xLong2 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Static xLastToolTip As String
  
  If xPStateIconPlaying Then
    If xPStateIconPaused Then
      xLong1 = 2&
      Else
      xLong1 = 1&
      End If
    End If
  
  If Not xLong2 = xLong1 Then
    Select Case xLong2
      Case 1&
        'eImage(1).ForeColor = &H80000008
        'eImage(1).BackColor = &H80000005
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def1, DynButtonsResX, &H0)
        CheckApiMenu 102, False
      Case 2&
        'eImage(2).ForeColor = &H80000008
        'eImage(2).BackColor = &H80000005
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def2, DynButtonsResX, &H1000000)
        CheckApiMenu 103&, False
      End Select
    xLong2 = xLong1
    Select Case xLong2
      Case 1&
        'eImage(1).ForeColor = &H8000000E
        'eImage(1).BackColor = &H8000000D
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def1, DynButtonsResX, &H10000000)
        CheckApiMenu 102&, True
      Case 2&
        'eImage(2).ForeColor = &H8000000E
        'eImage(2).BackColor = &H8000000D
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def2, DynButtonsResX, &H11000000)
        CheckApiMenu 103&, True
      End Select
    End If
    
' 0-2                 -> 0-2
' --- subicon ---
' 5=refres            -> 15
' 4=quit              -> 11
' 3=startup           -> 10
' -> caption / icon
' -> max 0-19 (0-4,5-9), icon x 10 (1000 - 1190)
'                          largeicon: ID-10 -> 1200 - 1290
' ID 18 reserved -> (x)fade
' ID 19 reserved -> overloaded
' string x, extra tooltip (10-19) -> (20-29)


' 9 -> over!
'    If Not IsDialoging = 0& Then xLong1 = 11&
  
  If xPStateIconSndcard Then
    If xPStateIconPlugging Then
      xLong1 = 12&
      Else
      xLong1 = 4&
      End If
    ElseIf xPStateIconQuit Then
    xLong1 = 5&
    ElseIf Not xLoadingStream = 0& Then
    xLong1 = 9& + xLoadingStream '10/11
    ElseIf xPStateIconRefreshing Then
    xLong1 = 7&
    ElseIf xPStateIconDeleting Then
    xLong1 = 6&
#If TrayXFadeIcon Then
    ElseIf AllTears_TypesCount > 2& And Not CurMusic = ctSSLayer_MusicTypeNone Then
    xLong1 = 8&
#End If
    ElseIf xOldWarnOverload Then
    xLong1 = 9&
    ElseIf xPStateIconStartup And xLong1 < 3& Then
    xLong1 = 3&
    End If
  
    ' Refreshing can occur when trying to resume playback after deleting songs (and playlist is now empty)
  
  If xLong1 = 10& Then
    #If BoldDirs = 98 Then
      xChar2 = PLI(PLCurItem).Caption
      #Else
      xChar2 = PLICap(PLCurItem)
      #End If
    ElseIf xLong1 = 11& Then
    xChar2 = RealURL(Mid(xLoadURL, InStr(xLoadURL + vbCr, vbCr) + 1&))
    If Len(xChar2) = 0& Then
      xChar2 = RealURL(xLoadURL)
      End If
    Else
    xChar2 = PLCurSongDisplay
    End If
  
  
  'If xLong1 = 10& Then
  '  xChar1 = Replace(LoadResEx_String(1000& + xLong1 * 2&), "$", PLCurSongDisplay)
  '  ElseIf xLong1 = 11& Then
  '  xChar1 = Replace(LoadResEx_String(1000& + xLong1 * 2&), "$", PLCurSongDisplay)
  '  Else
    xChar1 = Replace(LoadResEx_String(1000& + xLong1 * 2&), "$", xChar2)
  '  End If
    
  
  'If Not IsDialoging = 0& Then xLong1 = 4&
  
  
  If Not xOldIconState = xLong1 Then
    Call SetWindowText(MainHWnd, xChar1)
    xLastToolTip = xChar1
    Call LoadResEx_WindowIcon(MainHWnd, 1000& + xLong1 * 50&, VarPtr(SysTrayIcon))
    
    SysTrayIcon.ToolTip = xChar1 '+ vbNullChar
    'SysTrayIcon.hIcon = xCurrentMainIcon
    
'Debug.Print "SetIcon:", xLong1
    
    If xLong1 > 2& And Not xLong1 = 5& Then
      DoAnimateSetIcon 1030& + xLong1 * 50&
      Else
'#If TrayXFadeIcon Then
'      If AllTears_TypesCount > 2& And Not CurMusic = ctSSLayer_MusicTypeNone Then
'        DoAnimateSetIcon 1430&
'        ElseIf xOldWarnOverload Then
'#Else
'      If xOldWarnOverload Then
'#End If
'        DoAnimateSetIcon 1480&
'        Else
        DoAnimateSetIcon -1&
'        End If
      End If
    xOldIconState = xLong1
    
    
    If xTrayIconVisible Or xTrayIconEnabled Then
      Call Shell_NotifyIcon(1&, SysTrayIcon)
      End If
    ElseIf Not xLastToolTip = xChar1 Then
    Call SetWindowText(MainHWnd, xChar1)
    xLastToolTip = xChar1
    SysTrayIcon.ToolTip = xChar1 '+ vbNullChar
    If xTrayIconVisible Or xTrayIconEnabled Then
      Call Shell_NotifyIcon(1&, SysTrayIcon)
      End If
    End If
  
  'xChar1 = Replace(LoadResEx_String(1000& + xLong1 * 2&), "$", PLCurSongDisplay)
  SetToolTip xStaticIcons_Anim, Replace(LoadResEx_String(1001& + xLong1 * 2&), "$", xChar2), ""
  
  If Not (LEDMode = 0& Or LEDState = &H2&) Then
    If xPStateIconPlaying Then
      If xPStateIconPaused Then
        LEDState = &H20&
        Else
        LEDState = 0&
        End If
      Else
      LEDState = &H10&
      End If
    If xPStateIconDeleting Then
      LEDState = LEDState + &H200&
      ElseIf xPStateIconRefreshing Then
      LEDState = LEDState + &H100&
      End If
    If LEDState = &H10& Then
      If PLCount = 0& Then
        LEDState = &H3&
        End If
      End If
    If LEDState = 0& And Not xShowSpectrum Then
      LEDState = 1&
      Call InfoProcKb(0&, 0&, 0&, 0&)
      LEDState = 0&
      End If
    End If
  End Sub

Public Sub MainForm_TearIconChance()
  #If TrayXFadeIcon Then
    If Not ShowIR_IconHWnd2 = 0& Then
      If AllTears_TypesCount > 2& And Not CurMusic = ctSSLayer_MusicTypeNone Then
        ShowIR_IconHWnd = ShowIR_IconHWnd2
        Else
        ShowIR_IconHWnd = ShowIR_IconHWnd1
        End If
      If ShowIRWin Then
        ShowIR_Do True
        'If Not (ShowIR_IconHWnd1 = 0& And ShowIR_IconHWnd2 = 0&) Then
        '  Call SendMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H14&, 0&, 0&)
        '  Call SendMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &HF&, 0&, 0&)
        '  End If
        End If
      End If
    MainForm_UpdateMainIcon
    #Else
    Stop
    'If xOldIconState < 12& Then
    '  If AllTears_TypesCount > 2& And Not CurMusic = ctSSLayer_MusicTypeNone Then
    '    DoAnimateSetIcon 1055&
    '    ElseIf xOldWarnOverload Then
    '    DoAnimateSetIcon 1480&
    '    Else
    '    DoAnimateSetIcon -1&
    '    End If
    '  End If
    UpdateAnimIcon
    'MainForm_UpdateMainIcon?
    #End If
  End Sub

Private Sub MainForm_SaveSettings(ByVal WriteFailSave As Long, ByVal xSpectrum As Boolean)
  Dim xLong1 As Long
  Dim hFile As Long
  'Call GetMyWindowEx

  hFile = TxtFile_WriteCreate(AppBase("ini"))
  If Not hFile = 0& Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Saving settings"
      #End If
    TxtFile_WriteLine hFile, "AllowFileDeleting=" + BoolChar(xDelMode)
    TxtFile_WriteLine hFile, "AutoPlay=" + BoolChar(xAutoPlayback)
    TxtFile_WriteLine hFile, "AutoPauseOnUserSwitch=" + BoolChar(OutOfSessionPause)
    TxtFile_WriteLine hFile, "BlockIDEWhileLoading=" + BoolChar(xClassicBlockingOnLoad)
    TxtFile_WriteLine hFile, "CDDA=" + BoolChar(CDDAUse)
    TxtFile_WriteLine hFile, "CDDAJitterCorrection=" & BoolChar(CDDAJitter)
    TxtFile_WriteLine hFile, "CheckAccurateStreams=" + BoolChar(CheckAccurate)
    TxtFile_WriteLine hFile, "CheckSongsOnPlaylistLoad=" + BoolChar(RemoveMissingOnPLLoad)
    TxtFile_WriteLine hFile, "Controls=" & MergeControls
    TxtFile_WriteLine hFile, "CrossfadeTime=" & CStr(xCrossFadeTimeM + xFadeConsts1)
    TxtFile_WriteLine hFile, "DirectoryList=" & CStr(xDirList)
    TxtFile_WriteLine hFile, "DisableWindowsSounds=" & CStr(SoundSchemePointerMode)
    TxtFile_WriteLine hFile, "ElapsedTime=" & BoolChar(xShowElapsed)
    TxtFile_WriteLine hFile, "EmptyPlaylistStartup=" + CStr(xOnEmptyPLStartup)
    TxtFile_WriteLine hFile, "EndSongMode=" & CStr(xProcessMode)
    TxtFile_WriteLine hFile, "EndStreams=" & CStr(xTrackerSkipper(ctSSLayer_MusicTypeStream))
    TxtFile_WriteLine hFile, "EndTrackers=" & CStr(xTrackerSkipper(ctSSLayer_MusicTypeTracker))
    TxtFile_WriteLine hFile, "EndCDAudio=" & CStr(xTrackerSkipper(ctSSLayer_MusicTypeCDAudio))
    TxtFile_WriteLine hFile, "Fading=" & BoolChar(xGenFading)
    TxtFile_WriteLine hFile, "FailSave=" & CStr(WriteFailSave)
    TxtFile_WriteLine hFile, "FollowSong=" & BoolChar(xFollowCurSong)
    TxtFile_WriteLine hFile, "GetFileInfos=" & BoolChar(MP3GetID3Tag)
    TxtFile_WriteLine hFile, "GetFileFormat=" & BoolChar(MP3GetFormat)
    TxtFile_WriteLine hFile, "GlobalPlayKeys=" & BoolChar(GlobalKeys)
    TxtFile_WriteLine hFile, "IgnoreUnderscored=" & BoolChar(IgnoreUnderscored)
    If IntroModeActive Then
      TxtFile_WriteLine hFile, "IntroMode=" & CStr(IntroModeTime)
      Else
      TxtFile_WriteLine hFile, "IntroMode=" & CStr(-IntroModeTime)
      End If
    TxtFile_WriteLine hFile, "NoVerWarn=" & xNoVerWarnDLL
    TxtFile_WriteLine hFile, "OutputDevice=" & xOutputSSLID
    TxtFile_WriteLine hFile, "PlayerWindowPos=" & CStr(xMWinPos.Left) & "," & _
                             CStr(xMWinPos.Top) & "," & _
                             CStr(xMWinPos.Right) & "," & _
                             CStr(xMWinPos.Bottom)
    TxtFile_WriteLine hFile, "PlayerWindowState=" + CStr(StartUpWindowMode)
    TxtFile_WriteLine hFile, "PlaylistDoubleClick=" & CStr(PlDClickAction)
    TxtFile_WriteLine hFile, "PlaylistUpdateInterval=" & CStr(LockWinUpd)
    TxtFile_WriteLine hFile, "Priority=" & CStr(PlayerPriority)
    TxtFile_WriteLine hFile, "RadioAutoClose=" & BoolChar(xRadioAutoClose)
#If UseIRPlugins Then
    TxtFile_WriteLine hFile, "RadioWindowPos=" & CStr(xMWinPosx(xMWindowID_IRadio).Left) & "," & _
                             CStr(xMWinPosx(xMWindowID_IRadio).Top) & "," & _
                             CStr(xMWinPosx(xMWindowID_IRadio).Right) & "," & _
                             CStr(xMWinPosx(xMWindowID_IRadio).Bottom)
#End If
    TxtFile_WriteLine hFile, "RandomSeek=" + BoolChar(RandomPosOnTrackerSkipper)
    TxtFile_WriteLine hFile, "RegisterFolders=" & CStr(-CLng(RegFolderMode(0)) - 2& * CLng(RegFolderMode(1)) - 4& * CLng(RegFolderMode(2) - 8& * CLng(RegFolderMode(3))))
    TxtFile_WriteLine hFile, "RemoveMissingSongs=" + BoolChar(xRemoveMissingSongs)
    TxtFile_WriteLine hFile, "RemovePlayedSongs=" + BoolChar(xRemovePlayedSongs)
    TxtFile_WriteLine hFile, "RemoveSkippedSongs=" + BoolChar(xRemoveSkippedSongs)
    TxtFile_WriteLine hFile, "RepeatMode=" + CStr(xRep)
    TxtFile_WriteLine hFile, "SearchAutoClose=" + BoolChar(xSearchAutoClose)
    TxtFile_WriteLine hFile, "SearchAutoPlay=" + BoolChar(xSearchAutoPlay)
    TxtFile_WriteLine hFile, "ShowFullPath=" + BoolChar(ShowFullPath)
    TxtFile_WriteLine hFile, "ShowFullTitle=" + BoolChar(ShowFullTitle)
    TxtFile_WriteLine hFile, "Sorting=" + BoolChar(xSorting)
    If xShowSpectrum Or xSpectrum Then xLong1 = 1&
    If xDeluxeSpectrum Then xLong1 = xLong1 Or 2&
    TxtFile_WriteLine hFile, "Spectrum=" + CStr(xLong1)
'    TxtFile_WriteLine hfile, "TagsFont=" + GatherFont(1&)
    TxtFile_WriteLine hFile, "TitlesPosition=" + CStr(xTitlesPos)
    TxtFile_WriteLine hFile, "TitlesWhenMinimized=" + CStr(xTitlesWhenMinimized)
    TxtFile_WriteLine hFile, "TrackerPanSeperation=" + CStr(xModPanning)
    TxtFile_WriteLine hFile, "TrayIcon=" + BoolChar(xTrayIconVisible)
    TxtFile_WriteLine hFile, "TrayLeftClick=" + CStr(xTrayIconLeft)
    TxtFile_WriteLine hFile, "TrayMinimize=" + BoolChar(xTrayIconEnabled)
    TxtFile_WriteLine hFile, "TrayRightClick=" + CStr(xTrayIconRight)
    TxtFile_WriteLine hFile, "TutorialShown=" + BoolChar(FirstTimeHelp)
    TxtFile_WriteLine hFile, "UsedFileTypes=" + PrettyFileTypes
    TxtFile_WriteLine hFile, "UsePlaylistSettings=" + BoolChar(UsePLSettings)
    TxtFile_WriteLine hFile, "Volume=" + CStr(xSetVolume)
    Call TxtFile_WriteClose(hFile)
#If UseFileLogging Then
    Else
    MainForm_ctConsoleA_AddLog "Error saving configuration"
#End If
    End If
  End Sub

Private Function RandomSong() As Long
Dim xLong1 As Long

If Not PLCount = 0& Then
  If xRemovePlayedSongs And xRemoveSkippedSongs Then ' Or PLCount < 4& * RandomizerPattern Then
    GrabError
    Do
      RandomSong = Int(Timer * 100& + Rnd * PLCount) Mod PLCount
      'If Not Left(PLItem(RandomSong), 1&) = Chr(1&) Then Exit Do
      #If BoldDirs = 98 Then
        If (PLI(RandomSong).DirDepthFlags And PLI_IsDir) = 0& Then Exit Do
        #Else
        If (PLIDF(RandomSong) And PLI_IsDir) = 0& Then Exit Do
        #End If
      Loop
    Else
    xLong1 = PLCount \ RandomizerPattern
    Do
      RandomSong = Int(Rnd * (xLong1 + 1&))
      RndPLPattern(RandomSong) = (RndPLPattern(RandomSong) + ConstByte_1) Mod RandomizerPattern
      RandomSong = RandomSong * RandomizerPattern + RndPatternAlias(RndPLPattern(RandomSong))
      If RandomSong < PLCount Then
        #If BoldDirs = 98 Then
          If (PLI(RandomSong).DirDepthFlags And PLI_IsDir) = 0& Then Exit Do
          #Else
          If (PLIDF(RandomSong) And PLI_IsDir) = 0& Then Exit Do
          #End If
        End If
      Loop
    End If
  End If
End Function

Private Sub MainForm_CheckAfter(ByVal CanFade As Boolean, ByVal NoStopSong As Boolean, ByVal UserDoes As Boolean, ByVal EnforceUserDoes As Boolean)
Dim xLong1 As Long

#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "Postprocessing song"
  #End If
xTimerIsEnabled = False
'If xRemovePlayedSongs And Not (PLCount = 0& Or xRep = 0& And Not UserDoes Or Not PlNextSong = "" And PLNext = PLCurItem) Then
If xRemovePlayedSongs And Not (xRep = 0& And Not UserDoes Or Not PlNextSong = "" And PLNext = PLCurItem) Then
  MainForm_RemoverX False
  End If

If PLCount = 0& And Not xAutoDirCount = 0& Then
  MainForm_xAutoRefreshNow False, False, False, True, False
  End If

#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "CheckAfter (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", xCurMusic=" + CStr(CurMusic) + ")"
  #End If

If PLCount = 0& Then
  #If PlayLoosingLog Then
    MainForm_ctConsoleA_AddLog "CheckAfterX"
    #End If
  MainForm_LockMe True
  If Not CurMusic = ctSSLayer_MusicTypeNone Then
    MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
    End If
  xPStateIconPlaying = False
  xPStateIconPaused = False
  'xReAutoPlay = False
  'ReAutoPlayDR = False
  MainForm_UpdateMainIcon
  MainForm_LockMe False

  Else
  If xProcessMode = 0& And Not (CanFade Or UserDoes Or xPStateIconPaused) And xRep = 0& And xPStateIconPlaying And PlNextSong = "" Then
#If PlayLoosingLog Then
  MainForm_ctConsoleA_AddLog "CheckAfter0"
  #End If
    xLong1 = xFindSong
    If Not xLong1 = 0& Then
      PLCurItem = xLong1
      MainForm_xPlayClick Not CanFade
      If Not SendMessage(xListHWnd(2), &H190&, 0&, 0&) > 1 Then
        xMarkSong xLong1, False, False
        xFollowCurSongLast = DoEventsEx_GetTickCount
        xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
        End If
      xTimerIsEnabled = True
      Exit Sub
      End If
    End If
  
  If EnforceUserDoes Then UserDoes = True
  
  If xProcessMode = 1& And CanFade Then
    #If PlayLoosingLog Then
      MainForm_ctConsoleA_AddLog "CheckAfter1pre (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
      #End If
    MainForm_PlayNext UserDoes, 1&, CanFade, False
    #If PlayLoosingLog Then
      MainForm_ctConsoleA_AddLog "CheckAfter1post (xPStateIconPlaying=" + CStr(xPStateIconPlaying) + ", CurMusic=" + CStr(CurMusic) + ")"
      #End If
    ElseIf xProcessMode = 3& And Not NoStopSong Then
    MainForm_LockMe True
    xReAutoPlay = False
    ReAutoPlayDR = False
    xPStateIconPlaying = False
    MainForm_UpdateMainIcon
    MainForm_LockMe False
    MainForm_PlayNext False, 1&, False, False
    If Not CurMusic = ctSSLayer_MusicTypeNone Then
      MainForm_LockMe True
      MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
      MainForm_LockMe False
      End If
    'ElseIf xProcessMode = 2& And Not (xRep = 0& Or UserDoes) Then
    'ElseIf xProcessMode = 2& And Not xRep = 0& Then
    ElseIf xProcessMode = 2& Then
    #If PlayLoosingLog Then
      MainForm_ctConsoleA_AddLog "CheckAfter2"
      #End If
    MainForm_LockMe True
    If Not CurMusic = ctSSLayer_MusicTypeNone Then
      MainForm_xEndSong CanFade And Not xPStateIconPaused, False, False
      End If
    'eImage(3&).ForeColor = &H8000000E
    'eImage(3&).BackColor = &H8000000D
    If Not vKioskMode Then
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H12000000)
      End If
    Call Tear_WaitFor(-2&)
    DoEventsEx 2500&
    'eImage(3&).ForeColor = &H80000008
    'eImage(3&).BackColor = &H80000005
    If Not vKioskMode Then
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H2000000)
      End If
    MainForm_LockMe False
    'PlayNext UserDoes, 1&, False, False
    MainForm_PlayNext False, 1&, False, False
    Else
    #If PlayLoosingLog Then
      MainForm_ctConsoleA_AddLog "CheckAfter3"
      #End If
    xPStateIconPaused = False
    MainForm_UpdateMainIcon
    MainForm_PlayNext False, 1&, False, CanFade
    End If
  End If

xTimerIsEnabled = True
End Sub

Private Sub MainForm_xPlayClick(ByVal NoFadeOut As Boolean)
If Not (PlayerIsLocked Or xAutoDoing) Then
  'If Tear_Stept(CurrentSongID) = 0& Then
    
    MainForm_LockMe True
    xReAutoPlay = True
    'ShowIcon 0&
    If (Tear_StepT(CurrentSongID) Or xGenFading) And xPStateIconPlaying And Not (CurMusic = ctSSLayer_MusicTypeNone Or NoFadeOut) Then
      If Not (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
        If Tear_StepT(CurrentSongID) = 0& Then Tear_Volume(CurrentSongID) = 1&
        Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 3&, 0&, SimpleFadingTotalTime, 0&)
        'ShowIcon 0& ''
        'DoFading -1&
        End If
      MainForm_LockMe False
      Exit Sub
      End If
    xPStateIconPlaying = True
    MainForm_UpdateMainIcon
    If CurMusic = ctSSLayer_MusicTypeNone Then
      If PLCount = 0& And Not xAutoDirCount = 0& Then
        MainForm_xAutoRefreshNow False, False, False, True, False
        End If
      If PLCount = 0& Then
        xPStateIconPlaying = False
        MainForm_UpdateMainIcon
        Else
        If Not (xPlayItem(False, False, False, True, False, "")) Then
          'If Not DontTryOtherSong Then
            xFindNextItem 1&, False, False, False, False
          '  End If
          End If
        End If
      ElseIf Not (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
      MainForm_RestartEx
      End If
    
    
#If ProcCtl Then
    Call SendMessage(xStaticIconsHWnd(xStaticIcons_CurSongPos), 1026&, 0&, 0&)
#Else
    MainForm_EraseBackground xStaticIconsHWnd(xStaticIcons_CurSongPos)
#End If
    'ePic(1).Line (0&, 0&)-(ePic1ScaleWidth, 10&), FLineColorX, BF
    xEndSongTimerEventTime = DoEventsEx_GetTickCount
    MainForm_LockMe False
    'Else
    'If Then
    '  Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 3&, 0&, SimpleFadingTotalTime, 0&)
    '  Else
    '  Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 3&, 0&, SimpleFadingTotalTime, 0&)
    '  End If
    'End If
  End If
End Sub

Public Sub xTrackSeekA(ByVal xStep As Long)
  Dim xLong1 As Long

  If Not (PlayerIsLocked Or CurMusic = ctSSLayer_MusicTypeNone) And DoEventsEx_IsTickCountElapsed(xGUIClickDelayTimer, 200&) Then
  'If Not (PlayerIsLocked Or Abs(Timer * 100 - xGUIClickDelayTimer) < 20 Or CurMusic = ctSSLayer_MusicTypeNone) Then
    xGUIClickDelayTimer = DoEventsEx_GetTickCount
  
    xLong1 = ctSSLayer_GetPosition(SongPointer(CurrentSongID))
    If CurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
      xLong1 = xLong1 + xStep
      Else
      xLong1 = xLong1 + xStep * PLCurSongLength \ 16
      End If
    If xLong1 > PLCurSongLength Then xLong1 = PLCurSongLength
    If xLong1 < 0& Then xLong1 = 0&
    If xLong1 < PLCurSongLength Then
      Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), xLong1)
      If CurMusic = ctSSLayer_MusicTypeTracker Then
        'xModCurPattern = -1&
        'xModPatterns = ""
        ResetPatternLog
        End If
      End If
    End If
  End Sub

#If LoudestSongMethode Then
Private Sub xAddLastDir()
#Else
Private Sub xAddLastDir(ByRef canShowIR As Boolean)
#End If
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  
  #If LoudestSongMethode Then
    #Else
    If canShowIR Then
      ShowIR
      End If
    #End If
  
  #If PersonalStuffABC Then
    #If BoldDirs = 98 Then
      ABChat_SetSong PLI(PLCurItem).Caption
      #Else
      ABChat_SetSong PLICap(PLCurItem)
      #End If
    #End If
  
  If Not xLastPathCount = 0& Then
#If BoldDirs = 98 Then
    If UCase(xLastPath(xLastPathCount - 1&)) = UCase(PLI(PLCurItem).Filename) Then
#Else
    If UCase(xLastPath(xLastPathCount - 1&)) = UCase(PLIFile(PLCurItem)) Then
#End If
      Exit Sub
      End If
    End If
  #If BoldDirs = 98 Then
    xChar1 = PLI(PLCurItem).Caption
  #Else
    xChar1 = PLICap(PLCurItem)
    #End If
  
  Call SelectObject(xSharedDC, hFontGFix)
  xLong2 = GetTextOutWidth(xSharedDC, xChar1)
  
  If xLastPathCount = xLastPathMax Then
    xLastPathCurMaxWidth = 0&
'    xLong2 = xLastPathLen(0&)
    For xLong1 = 0& To xLastPathMax - 2&
      xLastPath(xLong1) = xLastPath(xLong1 + 1&)
      xLastPathCap(xLong1) = xLastPathCap(xLong1 + 1&)
      xLastPathTime(xLong1) = xLastPathTime(xLong1 + 1&)
      xLastPathLen(xLong1) = xLastPathLen(xLong1 + 1&)
      Call SendMessage(xListHWnd(1&), &H19A&, xLong1 + 1&, xLong1)
      If xLastPathLen(xLong1) > xLastPathCurMaxWidth Then xLastPathCurMaxWidth = xLastPathLen(xLong1)
      Next
    Call SendMessage(xListHWnd(1), &H182&, 0&, 0&)
    xLastPathCount = xLastPathMaxX
    End If
  
  #If BoldDirs = 98 Then
    xLastPath(xLastPathCount) = PLI(PLCurItem).Filename
    xLastPathCap(xLastPathCount) = PLI(PLCurItem).Caption
    #Else
    xLastPath(xLastPathCount) = PLIFile(PLCurItem)
    xLastPathCap(xLastPathCount) = PLICap(PLCurItem)
    #End If
  xLastPathTime(xLastPathCount) = SPTimeShort()
  xLastPathLen(xLastPathCount) = xLong2
  If xLong2 > xLastPathCurMaxWidth Then xLastPathCurMaxWidth = xLong2
  Call SendMessage(xListHWnd(1&), &H180&, 0&, xLastPathCount)

  Call SendMessage(xListHWnd(1), &H194&, (32& + xLastPathTimeSpace + xLastPathCurMaxWidth), 0&)
  xLastPathCount = xLastPathCount + 1&
  
  If SendMessage(xListHWnd(1), &H188&, 0&, 0&) = (xLastPathCount - 2&) Then 'Or xLastPathCount = 1& Then
    Call SendMessage(xListHWnd(1), &H186&, xLastPathCount - 1&, 0&)
    End If
  End Sub

Private Function MainForm_xLastDirNow(ByVal Force As Boolean) As Boolean
Dim xLong1 As Long
Dim xLong2 As Long
Dim xChar1 As String

'Me.Visible = False
'DoEvents

If Not PLCount = 0& Then
  xLong2 = SendMessage(xListHWnd(1), &H188&, 0&, 0&)
  If Not xLong2 = -1& Then
    xLong1 = xFindSong(xLastPath(xLong2))
    If Not xLong1 = 0& And xLong1 < PLCount And PLCount > 1& Then
      xMarkSong PLCount - 1&, True, False
      xMarkSong xLong1, False, False
      MainForm_xLastDirNow = True
      Exit Function
      End If
    End If
  End If

If Force And Not vKioskMode Then
  xChar1 = LCase(xLastPath(xLong2))
  For xLong1 = 0& To xLastPathCount - 1&
    If LCase(xLastPath(xLong1)) = xChar1 Then
      If AllowedItem(xLastPath(xLong2), True) = 1& Then
        MainForm_xLastDirNow = True
        AddLastSongsToPL
        MainForm_xLastDirNow False
        End If
      Exit Function
      End If
    Next
  If AllowedItem(xLastPath(xLong2), True) = 1& Then
    #If BoldDirs = 98 Then
      PLI(PLCount).Filename = SimpleDir(xLastPath(xLong2))
      PLI(PLCount).Caption = PLI(PLCount).Filename
      PLI(PLCount).DirDepthFlags = PLI_IsDir 'or 0
      PLCount = PLCount + 1&
      PLI(PLCount).Filename = xLastPath(xLong2)
      PLI(PLCount).Caption = xLastPathCap(xLong2)
      PLI(PLCount).DirDepthFlags = 1&
      #Else
      PLIFile(PLCount) = SimpleDir(xLastPath(xLong2))
      PLICap(PLCount) = PLIFile(PLCount)
      PLIDF(PLCount) = PLI_IsDir 'or 0
      PLCount = PLCount + 1&
      PLIFile(PLCount) = xLastPath(xLong2)
      PLICap(PLCount) = xLastPathCap(xLong2)
      PLIDF(PLCount) = 1&
      #End If
    PLCount = PLCount + 1&
    RealPLAdd PLCount - 2&
    End If
  End If

End Function

Private Sub MainForm_xLastDirNot()
  Dim xLong1 As Long
  
  For xLong1 = 0& To xLastPathCount - 1& 'eDropDown(1).ListCount - 1&
    If xLastPath(xLong1) = PLCurSong Then
      Call SendMessage(xListHWnd(1), &H186, xLong1, 0&)
      Exit For
      End If
    Next
  End Sub

Public Sub MainForm_ShowTray(ByVal NoVisible As Boolean)
Dim xLong1 As Long
Dim xLong2 As Long

#If UseFileLogging Then
  MainForm_ctConsoleA_AddLog "Performing system tray icon changes"
  #End If

If Not xTrayIconOld = -1& Then
  If xTrayIconIndicator And xTrayIconEnabled Then
    Call ShowWindow(MainHWnd, 7&)
    Call ShowWindow(MainHWnd, 0&)
    Else
    xTrayIconIndicator = False
    'Me.Visible = True
    'Call ShowWindow(MainHWnd, 9&)
    Call ShowWindow(MainHWnd, 5&)
    DoEvents
    End If
  If xTrayIconVisible Then xLong1 = 2&
  If xTrayIconIndicator Then
    xLong1 = xLong1 + 1&
    End If
  If Not xLong1 = xTrayIconOld Then
    If (xLong1 = 0& Or xLong1 = 2&) And (xTrayIconOld = 1& Or xTrayIconOld = 3&) And Not NoVisible Then
      xLong2 = GetLastActivePopup(MainHWnd)
      If IsIconic(MainHWnd) Then
        If xLong2 = 0& Then
          Call ShowWindow(MainHWnd, 9&)
          Call SetForegroundWindow(MainHWnd)
          Else
          Call ShowWindow(MainHWnd, 9&)
          Call ShowWindow(xLong2, 9&)
          Call SetForegroundWindow(xLong2)
          End If
        Else
        If xLong2 = 0& Then
          Call ShowWindow(MainHWnd, 5&)
          Call SetForegroundWindow(MainHWnd)
          Else
          Call ShowWindow(MainHWnd, 5&)
          Call ShowWindow(xLong2, 5&)
          Call SetForegroundWindow(xLong2)
          End If
        End If
      'Call SetForegroundWindow(mainhwnd)
      MainForm_DesireSpectrumState True, False
      End If
    If xTrayIconOld = 0& Then
      Call Shell_NotifyIcon(0&, SysTrayIcon)
      #If TrayIconViaTimerCheck Then
        xTrayIconCrashUpdateTimer = DoEventsEx_GetTickCount
        #End If
      End If
    If xLong1 = 0& Then
      Call Shell_NotifyIcon(2&, SysTrayIcon)
      #If TrayIconViaTimerCheck Then
        xTrayIconCrashUpdateTimer = 0&
        #End If
      End If
    xTrayIconOld = xLong1
    End If
  End If
End Sub

#If LoudestSongMethode Then
Public Sub ShowIRI()
#Else
Private Sub ShowIRI()
#End If
  #If FlatCastCF17 Then
    xFCI.Text0 = PLCurSongDisplay
    #End If
  
  If (xTitlesWhenMinimized = 2& Or xTitlesWhenMinimized = 1& And (IsIconic(MainHWnd) Or xTrayIconIndicator)) And Not (xPStateIconSndcard Or xPStateIconQuit) Then
    ShowIR_Do False
    ElseIf ShowIRWin Then
    ShowIR_Do True
    End If
  End Sub

Public Sub MainForm_SetTimerImage()
'Dim xChar1 As String
'Dim xLong1 As Long

If xTimerEvCount = 0& Then
  'SetToolTip xStaticIcons_TimerInd, LoadResEx_String(2650&), ExtKbdEx(KbdC_OTimerEdit)
  SetToolTip xStaticIcons_TimerInd, LoadResEx_String(2650&), Replace(Replace(Replace(Replace(LoadResEx_String(2652&) _
               , "$W", ExtKbdEx(KbdC_OTimerEdit)) _
               , "$L", ExtKbdEx(KbdC_OTimerImport)) _
               , "$S", ExtKbdEx(KbdC_OTimerExport)) _
               , "$C", ExtKbdEx(KbdC_OTimerClear))
  
  Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_TimerInd, xTimerIconBMP, 0&)
  'Call LoadResPictureExA(2610& + WhiteBackGrnd * 2&, eImage(21))
  CheckApiMenu 441&, False
  MainForm_EnableApiMenu 443&, False
  MainForm_EnableApiMenu 444&, False
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Timer disabled"
    #End If
  Else
  xTimerTimeTimerTime = DoEventsEx_GetTickCount
  'Call LoadResPictureExA(2620&, eImage(21))
  Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_TimerInd, xTimerIconBMP, &H10000000)
  CheckApiMenu 441&, True
  MainForm_EnableApiMenu 443&, (Not (vKioskMode Or xSecureMode))
  MainForm_EnableApiMenu 444&, (Not vKioskMode)
  #If UseFileLogging And TimeTestDebug Then
    MainForm_ctConsoleA_AddLog "Timer changed: " & xTimerDisplay(0&, 2&)
    #End If
'  SetToolTip xStaticIcons_TimerInd, xTimerDisplay(0&, 2&), ExtKbdEx(KbdC_OTimerEdit)
  SetToolTip xStaticIcons_TimerInd, xTimerDisplay(0&, 2&), Replace(Replace(Replace(Replace(LoadResEx_String(2652&) _
               , "$W", ExtKbdEx(KbdC_OTimerEdit)) _
               , "$L", ExtKbdEx(KbdC_OTimerImport)) _
               , "$S", ExtKbdEx(KbdC_OTimerExport)) _
               , "$C", ExtKbdEx(KbdC_OTimerClear))
  End If
End Sub

Private Function CheckReplay() As Boolean
If (xAutoPlayback And xReAutoPlay Or ReAutoPlayDR) And Not (xPStateIconPaused Or xPStateIconPlaying Or PLCount = 0& Or PlayerIsLocked Or xAutoDoing Or OutOfSessionHasPaused) Then
  If xRep = 2& Then PLCurItem = RandomSong
  MainForm_IconsMouseUp 1&
  CheckReplay = xPStateIconPlaying
  End If
xAutoPlaybackLastRetryTime = DoEventsEx_GetTickCount
End Function

Public Sub MainForm_ApplyProcessMode()
  'CheckApiMenu 330&, (xProcessMode = 1&)
  'CheckApiMenu 331&, (xProcessMode = 2&)
  'CheckApiMenu 332&, (xProcessMode = 3&)
  Call RadioApiMenu(330&, 333&, 330& + xProcessMode)
  
  If SoundSchemePointerMode = 2& Or SoundSchemePointerMode = 1& And xProcessMode = 2& Then
    'shouldquiet
    If SoundSchemePointer = 0& Then
      SoundSchemePointer = ctSoundSchemeSwitcher_ReadScheme(".Current", "", DeleteFileName)
      If Not SoundSchemePointer = 0& Then
        Call ctSoundSchemeSwitcher_WriteScheme(SoundSchemePointer, CalaymBackupSchemeREGName, LoadResEx_String(3579&))
        End If
      End If
    ElseIf Not SoundSchemePointer = 0& Then
    'shouldactivate
    MainForm_RestoreScheme
    End If
  TestRestoreReg False
  End Sub

Private Function FilteredCMD(ByVal xParam As String) As String
Dim xLong1 As Long
Dim xLong2 As Long

Do
  xParam = Trim(xParam)
  If xParam = "" Then Exit Do
  Select Case Left(xParam, 1&)
    Case "/"
      xLong1 = InStr(xParam & " ", " ")
      xLong2 = InStr(xParam & Chr(34&), Chr(34&))
      If xLong2 < xLong1 Then
        xLong1 = InStr(xLong2 + 1&, xParam, Chr(34&))
        If xLong1 = 0& Then
          xParam = ""
          Else
          xParam = Left(xParam, xLong2 - 1&) & Mid(xParam, xLong1 + 1&)
          End If
        Else
        xParam = Mid(xParam, xLong1 + 1&)
        End If
    Case Else
      If Left(xParam, 1&) = Chr(34&) Then
        xParam = Mid(xParam, 2&)
        xLong1 = InStr(xParam, Chr(34&))
        Else
        xLong1 = InStr(xParam & " ", " ")
        End If
      If Not xLong1 = 0& Then
        xParam = Left(xParam, xLong1 - 1&)
        End If
      Exit Do
    End Select
  Loop
FilteredCMD = xParam
End Function

Private Sub DirListEvent(ByVal Index As Long)
Select Case Index
  Case 0 ', 1
    xDirList = -xDirList
    MainForm_ToggleDirList True
  Case 2
    If Not xDirList = xDirListMax Then
      xDirList = xDirList + 1&
      MainForm_EnableApiMenu 427&, (Not xDirList = xDirListMax)
      MainForm_EnableApiMenu 428&, True
      MainForm_ResizePlayer False
      End If
  Case 3
    If Not xDirList = xDirListMin Then
      xDirList = xDirList - 1&
      MainForm_EnableApiMenu 427&, True
      MainForm_EnableApiMenu 428&, Not xDirList = xDirListMin
      MainForm_ResizePlayer False
      End If
  End Select
'xDirList = True
End Sub

Public Sub AutoRefreshHandler(ByVal Index As Long)
  Dim xLong1 As Long

  If Not (xAutoDirCount = 0& Or PlayerIsLocked Or xAutoDoing) Then 'Or vKioskMode) Then
    SetAutoDoing True
    If Index = 4 And xPStateIconPlaying Then
      xLong1 = xFindSong()
#If BoldDirs = 98 Then
      If xLong1 = 0& Or (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = PLI_IsAuto Then
#Else
      If xLong1 = 0& Or (PLIDF(xLong1) And PLI_IsAuto) = PLI_IsAuto Then
#End If
        xPStateIconPlaying = False
        MainForm_LockMe True
        MainForm_xEndSong Not xPStateIconPaused, False, False
        MainForm_LockMe False
        End If
      End If
      
    If Index = 2& Then
      xRemoveAutoRefresh PLI_IsAuto, True, False
      Else
      xRemoveAutoRefresh 0&, True, False
      End If
    ClearOverload
    ClearPLEx
    RealPLAdd 0&
    SetAutoDoing False
    InitResearch False
    xLong1 = xFindSong
    If Not xLong1 = 0& Then
      PLCurItem = xLong1
      xMarkSong xLong1, True, False
      End If
    If Index = 4 Then
      ReAutoPlayDRTime = DoEventsEx_GetTickCount
      ReAutoPlayDR = True
      End If
    End If
  End Sub

Private Sub AddLastSongsToPL()
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Dim xLong3 As Long
  
  'Me.Visible = False
  'DoEvents
  
  SetAutoDoing True
  xPStateIconRefreshing = True
  MainForm_UpdateMainIcon
  For xLong1 = 0& To xLastPathCount - 1&
    If AllowedItem(xLastPath(xLong1), True) = 1& Then
      sCharA1(xLong2) = xLastPath(xLong1)
      xLong2 = xLong2 + 1&
      End If
    Next
  If Not xLong2 = 0& Then
    GenCalaymStructure False, xLong2, False
    'MainForm_xBuildDirs
    'For xLong1 = xLong2 To PLCount - 1&
    '  Call SendMessage_String(xListHWnd(2), &H180&, 0&,  PLCap(xLong1))
    '  Next
    SetAutoDoing False
    xMarkSong xLong2, False, False
    xPStateIconRefreshing = False
    MainForm_UpdateMainIcon
    SetAutoDoing False
    CheckReplay
    End If
  End Sub

Private Sub TimerMenuEvent(ByVal Index As Long)
  Dim xChar1 As String
  
  Select Case Index
    Case 0&
      ToggleInterface Interface_Mode_Timer
    Case 2&
      xChar1 = CalaymLoadSingleFile(LoadResEx_String(1605&) + vbNullChar + "*.cpt", LoadResEx_String(1600&), TimerDir, MainHWnd, True)
      If Not Len(xChar1) = 0& Then
        TimerDir = SimpleDir(xChar1)
        'LoadTimers xChar1
        MainForm_LoadTimers MainHWnd, xChar1, False
        End If
    Case 3&
      MainForm_SaveTimers MainHWnd, "", False
    Case 5&
      If InterfaceMode = Interface_Mode_Timer Then
        TimerListForm_TrySCommandEx MainHWnd, 3&
        Else
        
        If Not xTimerEvIR = -1& Then
          If CalaymMessageBox(MainHWnd, LoadResEx_String(1619&), LoadResEx_String(1618&), 68&) = vbNo Then
            xTimerEvIR = -1&
            End If
          End If
        If xTimerEvIR = -1& Then
          xTimerEvCount = 0&
          'Erase xTimerEv
          'Erase xTimerEvTime
          xTimerEvIR = -1&
          MainForm_EnableApiMenu 443&, False
          MainForm_EnableApiMenu 444&, False
          Else
          xTimerEv(0&) = xTimerEv(xTimerEvIR)
          xTimerEvTime(0&) = xTimerEvTime(xTimerEvIR)
          xTimerEvCount = 1&
          xTimerEvIR = 0&
          End If
        MainForm_SetTimerImage
        End If
    End Select
  End Sub

Public Sub MainForm_LockMe(ByVal vBool1 As Boolean)
  Dim xLong1 As Long
  'Dim xLong2 As Long
  
  PlayerIsLocked = vBool1
  For xLong1 = 0& To GetMenuItemCount(xMenu(0)) - 1&
    EnableMenuItem xMenu(0), xLong1, &H400& - 3& * vBool1
    Next
  
  Select Case InterfaceMode
    'Case Interface_Mode_Search
      'xLong2 = 1& + CLng(vBool1)
      'For xLong1 = 0& To Interface_Control_Search_Results
      '  If Not Interface_Control(xLong1) = 0& Then Call EnableWindow(Interface_Control(xLong1), xLong2)
      '  Next
    Case Interface_Mode_Timer
      TimerListForm_ApplyLockState
      'For xLong1 = 0& To GetMenuItemCount(TimerListForm_xMenu) - 1&
      '  EnableMenuItem TimerListForm_xMenu, xLong1, &H400& - 3& * vBool1
      '  Next
      DrawMenuBar MainHWnd
    Case Interface_Mode_Radio
      If PlayerIsLocked Then
        Call EnableWindow(Interface_Control(Interface_Control_Radio_Go), 0&)
        Call EnableWindow(Interface_Control(Interface_Control_Radio_Play), 0&)
        Else
        Call EnableWindow(Interface_Control(Interface_Control_Radio_Go), 1&)
        RadioForm_EnableButtons
        End If
    Case Else
      DrawMenuBar MainHWnd
    End Select
  End Sub

Private Sub MainForm_CheckDir(ByVal Force As Boolean, Optional ByVal DontMarkPath As Boolean)
  Static xLong1 As Long
  Static xLong2 As Long
  Static xLong3 As Long
  
'If Not (PlayerIsLocked Or xAutoDoing) Then
If Not xAutoDoing Then
  
  'xLong3 = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
  Call GetListIndexMS(xListHWnd(2), xLong3)
  If xLong3 = -1& Then xLong3 = 0&
  If Not xDirCount = 0& And (Force Or xLong3 < xLong1 Or xLong3 > xLong2) Then
    For xLong1 = 1& To xDirCount
      If xDirPath(xLong1) > xLong3 Then Exit For
      Next
    xLong2 = xDirPath(xLong1) - 1&
    If xLong1 > xDirCount Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Playlist index overdrive error (notification)"
        #End If
      xLong1 = xDirCount
      End If
    If Not DontMarkPath Then
      Call SendMessage(xListHWnd(0), &H186, xLong1 - 1&, 0&)
      End If
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "SetDirCap"
      #End If
    #If BoldDirs = 98 Then
      MainForm_SetDirCaption " " & PLI(xDirPath(xLong1 - 1&)).Filename
      #Else
      MainForm_SetDirCaption " " & PLIFile(xDirPath(xLong1 - 1&))
      #End If
    xLong1 = xDirPath(xLong1 - 1&)
#If UseFileLogging Then
    Else
    MainForm_ctConsoleA_AddLog "LeaveDirCap"
#End If
    End If
End If
  End Sub

Public Sub xTimerRemove(ByVal Index As Long)
  For Index = Index To xTimerEvCount - 1&
    xTimerEvTime(Index) = xTimerEvTime(Index + 1&)
    xTimerEv(Index) = xTimerEv(Index + 1&)
    Next
  xTimerEvCount = xTimerEvCount - 1&
  End Sub

Public Sub MainForm_LoadTimers(ByVal hWndOwner As Long, ByVal xChar1 As String, ByVal Silent As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xBool1 As Boolean
  Dim xChar2 As String
  Dim hFile As Long
  
  If Not TimerListForm_xLocked Then
    If AllowedItem(xChar1, True) = 254& Then
      hFile = TxtFile_ReadOpen(xChar1)
      If Not hFile = 0& Then
        If TxtFile_ReadLine(hFile, xChar1) Then
          TimerForm_PrePreset
          If xChar1 = xTimerIDString Then
            If Not (xTimerEvIR = -1& Or Silent) Then
              If Not CalaymMessageBox(hWndOwner, LoadResEx_String(1610&), LoadResEx_String(1600&), 68&) = vbYes Then
                xTimerEvIR = -1&
                End If
              End If
            If xTimerEvIR = -1& Then
              xTimerEvCount = 0&
              'Erase xTimerEv
              'Erase xTimerEvTime
              Else
              xTimerEv(0&) = xTimerEv(xTimerEvIR)
              xTimerEvTime(0&) = xTimerEvTime(xTimerEvIR)
              xTimerEvCount = 1&
              xTimerEvIR = 0&
              End If
      '      xTimerEvIR = -1
            'mainform_EnableApiMenu 443&, False
            'mainform_EnableApiMenu 444&, False
            If InterfaceMode = Interface_Mode_Timer Then
              Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H184&, 0&, 0&)
              For xLong1 = 0& To Interface_Control_Timer_RemoveAll
                Call EnableWindow(Interface_Control(xLong1), 0&)
                Next
              End If
            Do While TxtFile_ReadLine(hFile, xChar1)
              xLong1 = InStr(xChar1, ",")
              If Not xLong1 = 0& Then
                xChar2 = Trim(Left(xChar1, xLong1 - 1&))
                Select Case Left(xChar2, 1&)
                  Case "+", "-"
                    xChar2 = Trim(Mid(xChar2, 2&))
                  End Select
                If Len(xChar2) = 5& And Mid(xChar2, 3&, 1&) = ":" Then
                  xLong2 = Val(Left(xChar2, 2&))
                  xLong3 = Val(Right(xChar2, 2&))
                  If xLong2 > -1& And xLong2 < 24& And xLong2 > -1& And xLong3 < 60& Then
                    xLong2 = xLong2 * 60& + xLong3
                    GrabError
                    Select Case Left(Trim(xChar1), 1&)
                      Case "+"
                        xTimerEvTime(xTimerEvCount) = (Timer \ 60& + xLong2) Mod 1440&
                      Case "-"
                        xTimerEvTime(xTimerEvCount) = (Timer \ 60& - xLong2 + 1440&) Mod 1440&
                        'xTimerEvTime(xTimerEvCount) = (xTimerEvTime(xTimerEvCount) + 1440&) Mod 1440&
                      Case Else
                        xTimerEvTime(xTimerEvCount) = xLong2
                      End Select
                    xTimerEv(xTimerEvCount) = Trim(Mid(xChar1, xLong1 + 1&))
                    xBool1 = True
                    
                    #If UseFileLogging Then
                      MainForm_ctConsoleA_AddLog "Importing timer: " + xTimerEv(xTimerEvCount)
                      #End If
                    
                    Call xTimerDisplay(xTimerEvCount, 0&, , xBool1)
                    
                    If xBool1 Then
                      xTimerEvCount = xTimerEvCount + 1&
                      #If UseFileLogging Then
                        MainForm_ctConsoleA_AddLog "  ok"
                        #End If
                      If xTimerEvCount = xTimerEvMax And xTimerEvIR = -1& Or xTimerEvCount = xTimerEvMax + 1& Then
                        #If UseFileLogging Then
                          MainForm_ctConsoleA_AddLog "  (maximum reached)"
                          #End If
                        Exit Do
                        End If
        #If UseFileLogging Then
                      Else
                      MainForm_ctConsoleA_AddLog "  failed"
        #End If
                      End If
                    End If
                  End If
                End If
              Loop
            If InterfaceMode = Interface_Mode_Timer Then
              TimerListForm_ListUpdate
              Call EnableWindow(Interface_Control(Interface_Control_Timer_RemoveAll), 1&)
              Else
              ResortTimers
              End If
            End If
          End If
        Call TxtFile_ReadClose(hFile)
        End If
      End If
    End If
'Rs1:
'  On Error GoTo 0
'  Exit Sub
'
'Er1:
'  Resume Rs1
  End Sub

Private Sub MainForm_RemoverX(ByVal PrevSkip As Boolean)
  Dim xLong2 As Long
  Dim xLong3 As Long
  
  'Do While xAutoDoing: DoEvents: Loop
  If xAutoDoing Then MsgBox "Error 24.23": Stop: Exit Sub
  xLong2 = xFindSong
  If Not xLong2 = 0 Then
    RemoveFromSearch xLong2
    'InitResearch false
    RemoveListItemMS xListHWnd(2), xLong2
    For xLong3 = xLong2 To PLCount - 2&
      #If BoldDirs = 98 Then
        PLI(xLong3) = PLI(xLong3 + 1&)
        Call SendMessage(xListHWnd(2), &H19A&, xLong3, xLong3)
        #Else
        PLIFile(xLong3) = PLIFile(xLong3 + 1&)
        PLICap(xLong3) = PLICap(xLong3 + 1&)
        PLIDF(xLong3) = PLIDF(xLong3 + 1&)
        PLIPxLg(xLong3) = PLIPxLg(xLong3 + 1&)
        PLIPxSm(xLong3) = PLIPxSm(xLong3 + 1&)
        Call SendMessage(xListHWnd(2), &H19A&, xLong3, xLong3)
        #End If
      Next
    PLCount = PLCount - 1&
    If Not (PLCurItem = 0 Or PLCurItem < xLong2 Or PrevSkip) Then PLCurItem = PLCurItem - 1&
    If Not PlNextSong = "" Then
      If PLNext > xLong2 Then
        PLNext = PLNext - 1&
        Else
        If PLNext = xLong2 Then
          SetNextSong False
          End If
        End If
      End If
    'If Not xLong2 = 0& Then xLong2 = 0&
    CheckEmptyNodes False, xLong2 '- 1&
    End If
  If PLCount = 0& And Not xAutoDirCount = 0& Then
    MainForm_xAutoRefreshNow False, False, False, False, False
    End If
  End Sub

Private Sub SetAutoDoing(ByVal xBool1 As Boolean)
  If xBool1 Xor xAutoDoing Then
    xAutoDoing = xBool1
    MainForm_EnableApiMenu 419&, xAutoDoing And Not vKioskMode
    If xBool1 Then
      MainForm_SetDirCaption ""
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "ClearDirCap due SetAutoDoing"
        #End If
      If xDirList < 0& Then
        If Not IsWindowVisible(xListHWnd(0)) = 0& Then
          ShowWindow xListHWnd(0), 0&
          'DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
          End If
        End If
      Else
      If Not (xHiddenAutoDoing Or xPStateIconSndcard Or xPStateIconStartup Or AllTears_TypesCount > AnimateForTearCountX) Then
        Call DoAnimateReadDir(0&)
        End If
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "WantDirCap from SetAutoDoing (force)"
        If PLClearCounter < PLCount Then PLClearCounter = PLCount
        #End If
      MainForm_CheckDir True
      End If
    xBool1 = Not (xBool1 Or xDirCount < 2&)
    EnableWindow xListHWnd(0), -CLng(xBool1)
    If Not AutoRefreshApplyHWnd = 0& Then
      EnableWindow AutoRefreshApplyHWnd, 1& - CLng(xAutoDoing)
      End If
    End If
  End Sub

Public Sub MainForm_SaveTimers(ByVal xHWnd As Long, ByVal Filename As String, ByVal Silent As Boolean)
  Dim xLong1 As Long
  Dim xBool1 As Boolean
  Dim hFile As Long
  
  If Not xTimerEvCount = 0& Then
    If xTimerEvCount = 1& And Not xTimerEvIR = -1& Then
      If Silent Then
        Exit Sub
        Else
        If CalaymMessageBox(xHWnd, LoadResEx_String(1612&), LoadResEx_String(1601&), 68&) = vbYes Then
          xBool1 = True
          Else 'If xTimerEvCount = 1& Then
          Exit Sub
          End If
        End If
      End If
    If Len(Filename) = 0& Then
      Filename = CalaymSaveSingleFile(LoadResEx_String(1605&) + vbNullChar + "*.cpt", LoadResEx_String(1601&), TimerDir, MainHWnd, True)
      End If
    If Not Len(Filename) = 0& Then
      TimerDir = SimpleDir(Filename)
      If Not (xTimerEvIR = -1& Or xBool1 Or Silent) Then
        xBool1 = (CalaymMessageBox(xHWnd, LoadResEx_String(1611&), LoadResEx_String(1601&), 68&) = vbYes)
        End If
      hFile = TxtFile_WriteCreate(Filename)
      If Not hFile = 0& Then
        Call TxtFile_WriteLine(hFile, xTimerIDString)
        For xLong1 = 0 To xTimerEvCount - 1
          If Not xLong1 = xTimerEvIR Or xBool1 Then
            TxtFile_WriteLine hFile, Space(1) & Right("0" + CStr(Fix(xTimerEvTime(xLong1) / 60&)), 2&) & ":" & Right("0" + CStr(Fix(xTimerEvTime(xLong1) Mod 60&)), 2&) & ", " & xTimerEv(xLong1)
            End If
          Next
        Call TxtFile_WriteClose(hFile)
        End If
      End If
    End If
  End Sub

Private Sub MainForm_ToggleDirList(ByVal UpdateWin As Boolean)
  If xDirList > 0& Then
    CheckApiMenu 426&, True
    MainForm_EnableApiMenu 427&, (xDirList > 0& And Not xDirList = xDirListMax) ' True
    MainForm_EnableApiMenu 428&, (xDirList > 0& And Not xDirList = xDirListMin) 'True
    ShowWindow xListHWnd(0), 0&
    Call SetWindowLong(xListHWnd(0), -16, &H46000100)
    Call SetWindowLong(xListHWnd(0), -20, &H200&)
    ShowWindow xStaticIconsHWnd(xStaticIcons_DirList), 0&
    Call SetWindowPos(xListHWnd(0&), 0&, 0&, 0&, 0&, 0&, &H335&)
    ShowWindow xListHWnd(0), 5&
    Else
    CheckApiMenu 426&, False
    MainForm_EnableApiMenu 427&, False
    MainForm_EnableApiMenu 428&, False
    ShowWindow xListHWnd(0), 0&
    Call SetWindowLong(xListHWnd(0), -16, &H46000100)
    Call SetWindowLong(xListHWnd(0), -20, &H20201)
    Call SetWindowPos(xListHWnd(0&), 0&, 17&, 0&, 0&, 0&, &H335&)
    ShowWindow xStaticIconsHWnd(xStaticIcons_DirList), 5&
    'Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_BAR), 0&, 0&, 0&, 0&, 0&, &H337&)
    'ShowWindow xStaticIconsHWnd(xStaticIcons_BAR), 0&
    'ShowWindow xStaticIconsHWnd(xStaticIcons_BAR), 5&
    'DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
    End If


  If UpdateWin Then
    MainForm_ResizePlayer False
    MainForm_xBuildDirs
    End If
  End Sub

Private Function DStr2(ByVal xLong1 As Integer) As String
  DStr2 = Right("0" + CStr(xLong1), 2&)
  End Function

#If UseFileLogging Then
  Private Function DStr4(ByVal xLong1 As Integer) As String
    DStr4 = Right("000" + CStr(xLong1), 4&)
    End Function
  
  Private Function DStr3(ByVal xLong1 As Integer) As String
    DStr3 = Right("00" + CStr(xLong1), 3&)
    End Function
  
  Public Sub MainForm_ctConsoleA_AddLog(ByVal MsgString As String)
    Call GetSystemTime(sSystemTime)
    With sSystemTime
      MsgString = DStr4(.wYear) + "-" + DStr2(.wMonth) + "-" + DStr2(.wDay) + ", " + DStr2(.wHour) + ":" + DStr2(.wMinute) + ":" + DStr2(.wSecond) + "." + DStr3(.wMilliseconds) + " " + MsgString
      End With
    If LogFileDoLog Then
      LogFileDoLog = AppendToTextFileEx(LogFileLog, MsgString)
      End If
    #If UseFileLoggingIDE Then
      Debug.Print MsgString
      #End If
    #If UseFileLoggingKernel Then 'And UseFileLogging Then
      If IsUnicode Then
        Call OutputDebugStringW(StrPtr(MsgString))
        Else
        Call OutputDebugStringA(MsgString)
        End If
      #End If
    End Sub
  #End If

Private Sub MainForm_ctConsoleA_AddSong(ByVal MsgSong As String)
  MsgSong = SPTimeShort + " " + MsgSong
  If LogFileDo Then
    If LogFileMulti Then
      With sSystemTime
        LogFileDo = AppendToTextFileEx(LogFile + "_" + Right("000" + CStr(.wYear), 4&) + "." + DStr2(.wMonth) + "." + DStr2(.wDay) + ".log", MsgSong)
        End With
      Else
      LogFileDo = AppendToTextFileEx(LogFile, MsgSong)
      End If
    End If
  #If UseFileLogging Then
    #Else
    #If UseFileLoggingIDE Then
      Debug.Print MsgSong
      #End If
    #End If
  End Sub

Private Function SPTimeShort() As String
  Call GetSystemTime(sSystemTime)
  With sSystemTime
    SPTimeShort = DStr2(.wHour) + ":" + DStr2(.wMinute) + ":" + DStr2(.wSecond)
    End With
  End Function

Public Sub MainForm_SetGlobalKeys(ByVal UseThem As Boolean)
  Dim xLong1 As Long
  
  If UseThem Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Registering global keys"
      #End If
     
     For xLong1 = KbdSplitN To KbdCountX
      If Not xKey(xLong1) = 0& Then
        #If NewKeyMode Then
          If NonRealKey(xKey(xLong1)) Then
            xKeyReg(xLong1 - KbdSplitN) = 1&
            ElseIf UnassignedKey(xKey(xLong1)) Then
            'xKeyReg(xLong1 - KbdSplitN) = RegisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1 - KbdSplitN, ((xKey(xLong1) And &H100&) \ &H40& + (xKey(xLong1) And &H200& \ &H100&)), (xKey(xLong1) And &HFF&))
            xKeyReg(xLong1 - KbdSplitN) = RegisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1 - KbdSplitN, ((xKey(xLong1) And &H100&) \ &H40&), (xKey(xLong1) And &HFF&))
            Else
            xKeyReg(xLong1 - KbdSplitN) = RegisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1 - KbdSplitN, &H3 + ((xKey(xLong1) And &H100) \ &H40&), (xKey(xLong1) And &HFF&))
            End If
#If UseFileLogging Then
          If xKeyReg(xLong1 - KbdSplitN) = 0& Then
            MainForm_ctConsoleA_AddLog "Could not regKey: " + CStr(xLong1) + ", " + Hex(xKey(xLong1)) + " (" + CStr(UnassignedKey(xKey(xLong1))) + ")"
            End If
  #End If
          #Else
          Call RegisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1 - KbdSplitN, &H3 + ((xKey(xLong1) And &H100) \ &H40&), (xKey(xLong1) And &HFF&))
          #End If
        End If
      Next
    Else
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Unregistering global keys"
      #End If
    'DoEvents
    For xLong1 = 0& To KbdCountX_Minus_KbdSplitN
      #If NewKeyMode Then
        If Not xKeyReg(xLong1) = 0& Then
          If Not NonRealKey(xKey(xLong1)) Then
            'ElseIf UnassignedKey(xKey(xLong1)) Then
#If ShowRegKeyStuff = 2 Then
            If UnregisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1) = 0& Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "Could not UnRegKey: " + CStr(xLong1) + ", " + Hex(xKey(xLong1))
                #Else
                Debug.Print "Could not UnRegKey: ", xLong1
                #End If
              End If
#Else
            Call UnregisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1)
#End If
            End If
          xKeyReg(xLong1) = 0&
          End If
        #Else
        If Not xKey(KbdSplitN + xLong1) = 0& Then
          Call UnregisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1)
          End If
        #End If
      Next
    
#If ShowRegKeyStuff Then
    For xLong1 = 0& To 1000&
      If Not UnregisterHotKey(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1) = 0& Then
        Debug.Print "PostUnReg:", xLong1
        End If
      Next
#End If
    End If
  
  End Sub

'Private Function ExtKbd(ByVal xLoadResID As Long, Index As Long) As String
'  ExtKbd = LoadResex_String(xLoadResID)
'  'xLoadResID = (xKey(Index) And &H300&) / &H100&
'  If Index > KbdSplit Then
'    xLoadResID = 4&
'    Else
'    xLoadResID = (xKey(Index) And &H300&) / &H100&
'    End If
'  Index = xKey(Index) And &HFF&
'  If Not Index = 0& Then
'    If xLoadResID = 0& Then
'      ExtKbd = ExtKbd & vbTab & GetKeyCaption(Index)
'      Else
'      ExtKbd = ExtKbd & vbTab & Replace(lkcombi(xLoadResID-1&), "$", GetKeyCaption(Index))
'      End If
'    End If
'  End Function

Private Function ExtKbdEx(ByVal Index As Long) As String
  Dim xLong1 As Long
  
  If Index < KbdSplitN Then
    xLong1 = ((xKey(Index) And &H300&) \ &H100&)
    ElseIf UnassignedKey(xKey(Index)) Then
    xLong1 = ((xKey(Index) And &H100&) \ &H100&)
    Else
    xLong1 = 4& + ((xKey(Index) And &H100&) \ &H100&)
    End If
  Index = (xKey(Index) And &HFF&)
  'if index
  If Index = 0& Then
    ExtKbdEx = LoadResEx_String(3990&)
    Else
    If xLong1 = 0& Then
      ExtKbdEx = GetKeyCaption(Index)
      Else
      'ExtKbdEx = Replace(lkcombi(xLong1-1&), "$", GetKeyCaption(Index))
      ExtKbdEx = lkCombi(xLong1 - 1&) & GetKeyCaption(Index)
      End If
    End If
  End Function

Private Function MergeControls() As String
Dim xLong1 As Long

For xLong1 = 0& To KbdCountX
  If ValidSetupKey(xLong1) Then
'If Not (xLong1 = KbdC_OTimerRefresh Or xLong1 = KbdC_OPresetRefr Or xLong1 = KbdC_FRefreshPLs) Then
    If xKey(xLong1) = 0& Then
      MergeControls = MergeControls & ","
      Else
      'MergeControls = MergeControls & Right("000" & Hex(xKey(xLong1)), 3&) & ","
      MergeControls = MergeControls & Hex(xKey(xLong1)) & ","
      End If
'End If
    End If
  Next
xLong1 = Len(MergeControls)
Do
  If Right(MergeControls, 1&) = "," Then
    xLong1 = xLong1 - 1&
    MergeControls = Left(MergeControls, xLong1)
    Else
    Exit Do
    End If
  Loop
MergeControls = KeyBoardVersionID & MergeControls
'MergeControls = Mid(MergeControls, 2&)
End Function

Public Sub MainForm_UpdateMenus()
  Dim xLong1 As Long


  'If xCmdQuitMode = 2 Then
  '  Else
  
  If xHasNextSong Then
    If Not vKioskMode Then
      SetToolTip xStaticIcons_Def3, LoadResEx_String(1127&), ExtKbdEx(KbdC_CStop)
      SetToolTip xStaticIcons_Def5, Replace(LoadResEx_String(1137&), "$", PlNextSongCap), ExtKbdEx(KbdC_CEnd)
      End If
    'mPMenu(4).Caption = ExtKbd(1421&, KbdC_PFindCurrent)
    ElseIf Not vKioskMode Then
    SetToolTip xStaticIcons_Def3, LoadResEx_String(1125&), ExtKbdEx(KbdC_CStop)
    SetToolTip xStaticIcons_Def5, LoadResEx_String(1135&), ExtKbdEx(KbdC_CEnd)
    'mPMenu(4).Caption = ExtKbd(1420&, KbdC_PFindCurrent)
    End If


  UpdateMenuCaption 10&, KbdC_FAddFileS, -1&
  UpdateMenuCaption 11&, KbdC_FAddDir, -1&
  UpdateMenuCaption 12&, KbdC_FAddPList, -1&
  'UpdateMenuCaption 20& / 21... KbdC_FRefreshCD
  UpdateMenuCaption 30&, KbdC_FNewPL, -1&
  UpdateMenuCaption 31&, KbdC_FLoadPL, -1&
  'UpdateMenuCaption 32&,KbdC_FLoadPLMenu
  UpdateMenuCaption 36&, KbdC_FLoadPLDefault, -1&
  UpdateMenuCaption 34&, KbdC_FReloadPL, -1&
  UpdateMenuCaption 40&, KbdC_FSavePL, -1&
  UpdateMenuCaption 41&, KbdC_FSavePLas, -1&
  UpdateMenuCaption 50&, KbdC_FClearPL, -1&
  UpdateMenuCaption 60&, KbdC_FQuitSD, KbdC_GQuit
  
  UpdateMenuCaption 101&, KbdC_CPrev, KbdC_GPrev
  If xKey(KbdC_GPlay) = 0& Then
    UpdateMenuCaption 102&, KbdC_CPlay, KbdC_GPlayPause
    Else
    UpdateMenuCaption 102&, KbdC_CPlay, KbdC_GPlay
    End If
  If xKey(KbdC_CPause) = 0& Then
    UpdateMenuCaption 103&, KbdC_CPause, KbdC_GPlayPause
    Else
    UpdateMenuCaption 103&, KbdC_CPause, KbdC_GPause
    End If
  UpdateMenuCaption 104&, KbdC_CStop, KbdC_GStop
  UpdateMenuCaption 105&, KbdC_CNext, KbdC_GNext
  UpdateMenuCaption 106&, KbdC_CEnd, KbdC_GEnd
  UpdateMenuCaption 110&, KbdC_CRepSingle, KbdC_GRepSingle
  UpdateMenuCaption 111&, KbdC_CRepAll, KbdC_GRepAll
  UpdateMenuCaption 112&, KbdC_CRepRnd, KbdC_GRepRnd
  UpdateMenuCaption 113&, KbdC_CRepToggle, KbdC_GRepToggle
  UpdateMenuCaption 119&, KbdC_CIntroMode, KbdC_GIntroMode
  UpdateMenuCaption 120&, KbdC_CSeekB, KbdC_GSeekB
  UpdateMenuCaption 121&, KbdC_CSeekF, KbdC_GSeekF
    UpdateMenuCaption 131&, KbdC_CCSSelFile, 0&
    UpdateMenuCaption 132&, KbdC_CCSRemFile, KbdC_GRemFile
    UpdateMenuCaption 133&, KbdC_CCSDelFile, KbdC_GDelFile
    UpdateMenuCaption 134&, KbdC_CCSExplorer, KbdC_GExplorer
  
  
  
  UpdateMenuCaption 201&, KbdC_PPlaySkip, -1&
  UpdateMenuCaption 202&, KbdC_PPlayEnd, -1&
  UpdateMenuCaption 203&, KbdC_PPlayNext, -1&
  UpdateMenuCaption 210&, KbdC_PFindCurrent, -1&
  UpdateMenuCaption 211&, KbdC_PFind, -1&
  UpdateMenuCaption 212&, KbdC_PFindNext, -1&
  UpdateMenuCaption 213&, KbdC_PFindQuick, -1&
  UpdateMenuCaption 214&, KbdC_PSelRnd, -1&
  UpdateMenuCaption 220&, KbdC_PInvSel, -1&
  UpdateMenuCaption 221&, KbdC_PRemSongs, -1&
  UpdateMenuCaption 222&, KbdC_PRem, -1&
  UpdateMenuCaption 223&, KbdC_PCopyPLEnd, -1&
  UpdateMenuCaption 224&, KbdC_PCrop, -1&
  UpdateMenuCaption 230&, KbdC_PDelFiles, -1&
  UpdateMenuCaption 250&, KbdC_PExplorer, -1&

'  UpdateMenuCaption 301&, KbdC_OFading, 0&
  UpdateMenuCaption 301&, KbdC_OFading, -1&
  UpdateMenuCaption 302&, KbdC_OSpektrum, -1&
  UpdateMenuCaption 303&, KbdC_OFileInfos, -1&
  UpdateMenuCaption 310&, KbdC_OFollowSong, -1&
  UpdateMenuCaption 320&, KbdC_ORemMissing, -1&
  UpdateMenuCaption 321&, KbdC_ORemSkipped, -1&
  UpdateMenuCaption 322&, KbdC_ORemPlayed, -1&
  UpdateMenuCaption 330&, KbdC_ONoEndMode, -1&
  UpdateMenuCaption 331&, KbdC_OXFade, -1&
  UpdateMenuCaption 332&, KbdC_OMDMode, -1&
  UpdateMenuCaption 333&, KbdC_OStopEach, -1&
  UpdateMenuCaption 339&, KbdC_OEndModeTggle, -1&
    UpdateMenuCaption 341&, KbdC_CVolUp, KbdC_GVolUp
    UpdateMenuCaption 342&, KbdC_CVolDown, KbdC_GVolDown
    For xLong1 = 0& To 10&
      'mCVolume(xLong1 + 3&).Caption = ExtKbd(1560& + xLong1, xLong1 + KbdC_CVolMute)
      UpdateMenuCaption 345& + xLong1, KbdC_CVolMute + xLong1, -1&
      Next
  UpdateMenuCaption 360&, KbdC_CMoreSetts, -1&
  
  UpdateMenuCaption 406&, KbdC_OInternetRadio, KbdC_GIRadio
  UpdateMenuCaption 407&, KbdC_OInternetRadioRe, KbdC_GIRadioRe
  UpdateMenuCaption 408&, KbdC_OInternetRadioHP, KbdC_GIRadioHP
  
  UpdateMenuCaption 411&, KbdC_OAutoRF, KbdC_GAutoRF
  UpdateMenuCaption 412&, KbdC_OAutoRFAlt, KbdC_GAutoRFAlt
  UpdateMenuCaption 413&, KbdC_OAutoRFRem, -1&
  UpdateMenuCaption 414&, KbdC_OAutoRFRemI, -1&
  UpdateMenuCaption 415&, KbdC_OAutoRFSetup, -1&
  UpdateMenuCaption 419&, 0&, KbdC_GAbortReading 'unused!
  UpdateMenuCaption 420&, KbdC_PGotoDir, -1&
      UpdateMenuCaption 426&, KbdC_ODirToggle, -1&
      UpdateMenuCaption 427&, KbdC_ODirInc, -1&
      UpdateMenuCaption 428&, KbdC_ODirDec, -1&
  UpdateMenuCaption 421&, KbdC_CPrevSongs, -1&
  'mTMenu(10).Caption = ExtKbd(1640&, KbdC_OTimerMenu)
      UpdateMenuCaption 441&, KbdC_OTimerEdit, -1&
      UpdateMenuCaption 442&, KbdC_OTimerImport, -1&
      UpdateMenuCaption 443&, KbdC_OTimerExport, -1&
      UpdateMenuCaption 444&, KbdC_OTimerClear, -1&
  'mTMenu(11).Caption = ExtKbd(1645&, KbdC_OTimerClear)
  UpdateMenuCaption 450&, KbdC_OResetSnd, KbdC_GResetSND
  UpdateMenuCaption 455&, KbdC_OVolCtrl, KbdC_GVolCtrl
  'mTMenu(15).Caption = ExtKbd(1670&, KbdC_OPresetMenu)
      UpdateMenuCaption 461&, KbdC_OPresetSave, -1&
      UpdateMenuCaption 462&, KbdC_OPresetLoad, -1&
  UpdateMenuCaption 470&, KbdC_OSaveSetts, -1&
  'UpdateMenuCaption 330&, KbdC_CVolMenu, KbdC_GShowVolMenu

  'UpdateMenuCaption 500&, KbdC_IInfoMenu, 0&
    UpdateMenuCaption 501&, KbdC_IHelp, -1&
    UpdateMenuCaption 502&, KbdC_IAbout, -1&
  
  
  
  
  UpdateMenuCaption 800&, -1&, KbdC_GShowPlayer
  UpdateMenuCaption 810&, -1&, KbdC_GShowTitle 'Unused!
  
  
  
  
'  On Error Resume Next
  If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
    ResetStreamTT
    End If

'  If InterfaceMode = Interface_Mode_none Then
    SetToolTip xStaticIcons_DirList, LoadResEx_String(1105&), _
                                   Replace(Replace(Replace(Replace(LoadResEx_String(1106&) _
                                   , "$S", ExtKbdEx(KbdC_PGotoDir)) _
                                   , "$T", ExtKbdEx(KbdC_ODirToggle)) _
                                   , "$I", ExtKbdEx(KbdC_ODirInc)) _
                                   , "$D", ExtKbdEx(KbdC_ODirDec))
'    End If

  If vKioskMode Then
    SetToolTip xStaticIcons_CurSongPos, LoadResEx_String(1172&), ""
    SetToolTip xStaticIcons_Spectrum, LoadResEx_String(1176&), ""
    SetToolTip xStaticIcons_Tags, LoadResEx_String(1180&), ""
    Else
    SetToolTip xStaticIcons_CurSongPos, LoadResEx_String(1170&), _
                                        Replace(Replace(LoadResEx_String(1171&) _
                                        , "$L", ExtKbdEx(KbdC_CSeekB)) _
                                        , "$R", ExtKbdEx(KbdC_CSeekF))
    SetToolTip xStaticIcons_Spectrum, LoadResEx_String(1175&), ExtKbdEx(KbdC_OSpektrum)
    SetToolTip xStaticIcons_Tags, LoadResEx_String(1180&), ExtKbdEx(KbdC_OFileInfos)
    End If
  CurSongToolTip1 = LoadResEx_String(1107&)
  CurSongToolTip2 = Replace(Replace(Replace(Replace(Replace(LoadResEx_String(1108&) _
                                        , "$P", ExtKbdEx(KbdC_CPrevSongs)) _
                                        , "$S", ExtKbdEx(KbdC_CCSSelFile)) _
                                        , "$R", ExtKbdEx(KbdC_CCSRemFile)) _
                                        , "$D", ExtKbdEx(KbdC_CCSDelFile)) _
                                        , "$X", ExtKbdEx(KbdC_CCSExplorer))
  SetToolTip xStaticIcons_CurSong, Replace(CurSongToolTip1, "$M", FormatToolTip1), CurSongToolTip2 + FormatToolTip2
  
  
  SetToolTip xStaticIcons_Def0, LoadResEx_String(1110&), ExtKbdEx(KbdC_CPrev)
  SetToolTip xStaticIcons_Def1, LoadResEx_String(1115&), ExtKbdEx(KbdC_CPlay)
  SetToolTip xStaticIcons_Def2, LoadResEx_String(1120&), ExtKbdEx(KbdC_CPause)
  SetToolTip xStaticIcons_Def4, LoadResEx_String(1130&), ExtKbdEx(KbdC_CNext)
  xVolTTText = Replace(Replace(Replace(Replace(Replace(LoadResEx_String(1151&) _
               , "$+", ExtKbdEx(KbdC_CVolUp)) _
               , "$-", ExtKbdEx(KbdC_CVolDown)) _
               , "$M", ExtKbdEx(KbdC_CVolMute)) _
               , "$F", ExtKbdEx(KbdC_CVolFull)) _
               , "$V", "$")
  SetToolTip xStaticIcons_Volume, Replace(LoadResEx_String(1150&), "$", CStr(CInt(xSetVolume * 100 / 255))), xVolTTText
  
  xModeTTText = Replace(Replace(Replace(Replace(Replace(LoadResEx_String(1161&) _
               , "$S", ExtKbdEx(KbdC_CRepSingle)) _
               , "$A", ExtKbdEx(KbdC_CRepAll)) _
               , "$R", ExtKbdEx(KbdC_CRepRnd)) _
               , "$I", ExtKbdEx(KbdC_CIntroMode)) _
               , "$M", "$")
  
  'SetToolTip xStaticIcons_RepMode, Replace(LoadResEx_String(1160&), "$", LoadResEx_String(1162& + xRep)), xModeTTText
  SetToolTip xStaticIcons_RepMode, Replace(LoadResEx_String(1160&), "$", LoadResEx_String(1162& + xRep * 2& - CLng(IntroModeActive))), xModeTTText
  MainForm_SetTimerImage
  If GlobalKeys Then
    MainForm_SetGlobalKeys False
    MainForm_SetGlobalKeys True
    End If
  End Sub

Private Sub ResetStreamTT()
  If vKioskMode Then
    SetFormatToolTip LastStreamTT, ""
    ElseIf UrlsAllowed Then
    SetFormatToolTip LastStreamTT, _
                                     Replace(Replace(Replace(LoadResEx_String(1932&) _
                                     , "$I", ExtKbdEx(KbdC_OInternetRadio)) _
                                     , "$R", ExtKbdEx(KbdC_OInternetRadioRe)) _
                                     , "$U", ExtKbdEx(KbdC_OInternetRadioHP))
    Else
    SetFormatToolTip LastStreamTT, _
                                     Replace(Replace(LoadResEx_String(1935&) _
                                     , "$I", ExtKbdEx(KbdC_OInternetRadio)) _
                                     , "$R", ExtKbdEx(KbdC_OInternetRadioRe))
    End If
  End Sub

Public Sub MainForm_ProcessKey(ByVal Index As Long, Optional ByVal xKioskModeParam As Boolean = True)
  Static xLong1 As Long
  
  If IsDialoging = 0& And Not (PlayerIsLocked Or xAutoDoing) Then
    #If UseFileLogging And DetailLog Then
      If Not Index < 0& And Index < KbdCount Then
        MainForm_ctConsoleA_AddLog "Keyprocessing: #" & CStr(Index) & " (" & LoadResEx_String(3700& + Index) & ")"
        Else
        MainForm_ctConsoleA_AddLog "Keyprocessing: invalid id (#" & CStr(Index) & ")"
        End If
      #End If
    If Not vKioskMode Then xKioskModeParam = False
    If Not xKioskModeParam Then
      Select Case Index
        Case KbdC_FAddFileS
          If Not xSecureMode Then
            MenuEventFile 0&
            End If
        Case KbdC_FAddDir
          'If Not xSecureMode Then
            MenuEventFile 1&
           ' End If
        Case KbdC_FAddPList
          If Not xSecureMode Then
            MenuEventFile 2&
            End If
        Case KbdC_FNewPL
          MenuEventFile 6&
        Case KbdC_FLoadPL
          MenuEventFile 7&
        Case KbdC_FReloadPL
          MenuEventFile 9&
        Case KbdC_FLoadPLDefault
          MenuEventFile 8&
        Case KbdC_FSavePL
          If Not xNoSaveListMenu = 2& Then
            MenuEventFile 10&
            End If
        Case KbdC_FSavePLas
          If Not xSecureMode Then
            MenuEventFile 11&
            End If
        Case KbdC_FClearPL
          MenuEventFile 13&
        Case KbdC_CPrev, KbdC_GPrev
          MainForm_IconsMouseUp 0&
        Case KbdC_CPause, KbdC_GPause
          MainForm_IconsMouseUp 2&
        Case KbdC_CRepSingle, KbdC_GRepSingle
          xRep = 0&: MainForm_ApplyRepMode
        Case KbdC_CRepAll, KbdC_GRepAll
          xRep = 1&: MainForm_ApplyRepMode
        Case KbdC_CRepRnd, KbdC_GRepRnd
          xRep = 2&: MainForm_ApplyRepMode
        Case KbdC_CRepToggle, KbdC_GRepToggle
          xRep = (xRep + 1&) Mod 3&: MainForm_ApplyRepMode
        Case KbdC_CIntroMode, KbdC_GIntroMode
          IntroModeActive = Not IntroModeActive
          'CheckApiMenu 119&, IntroModeActive
          MainForm_ApplyRepMode
        Case KbdC_CSeekB, KbdC_GSeekB
          xTrackSeekA -1&
        Case KbdC_CSeekF, KbdC_GSeekF
          xTrackSeekA 1&
        Case KbdC_CCSRemFile, KbdC_GRemFile
          If Not vKioskMode Then
            If Not (CurMusic = ctSSLayer_MusicTypeNone Or (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)) Then
              RemoveCurrentSongFromList
              End If
            End If
        Case KbdC_CCSDelFile, KbdC_GDelFile
          If xDelMode And Not xSecureMode Then
            If Not (CurMusic = ctSSLayer_MusicTypeNone Or (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)) Then
              xDeleteFileNow True
              End If
            End If
        Case KbdC_CCSExplorer, KbdC_GExplorer
          If Not xSecureMode Then
            If Not (CurMusic = ctSSLayer_MusicTypeNone Or (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)) Then
              OpenInExplorer
              End If
            End If
        Case KbdC_PInvSel
          For xLong1 = 0& To PLCount - 1&
            If SendMessage(xListHWnd(2), &H187&, xLong1, 0&) = 0& Then
              Call SendMessage(xListHWnd(2), &H185&, 1&, xLong1)
              Else
              Call SendMessage(xListHWnd(2), &H185&, 0&, xLong1)
              End If
            'SetlPlayList_Selected xLong1, Not GetlPlayList_Selected(xLong1)
            Next
          MainForm_CheckDir False
        Case KbdC_PRemSongs
          xRemoveItems True
        Case KbdC_PRem
          xRemoveItems False
        Case KbdC_PCopyPLEnd
          MainForm_MenuEventPlaylist 13
        Case KbdC_PCrop
          MainForm_MenuEventPlaylist 14
        Case KbdC_PDelFiles
          If xDelMode And Not xSecureMode Then
            xDeleteFileNow False
            End If
        Case KbdC_PExplorer
          If Not xSecureMode Then
            If GetListIndexMS(xListHWnd(2), xLong1) Then
              #If BoldDirs = 98 Then
                OpenInExplorer PLI(xLong1).Filename
                #Else
                OpenInExplorer PLIFile(xLong1)
                #End If
              End If
            End If
        Case KbdC_OFading
          xGenFading = Not xGenFading
          CheckApiMenu 301&, xGenFading
        Case KbdC_OSpektrum
          If Not xNoSpectrumForce Then
            SpektrumToggleEvent
            End If
        Case KbdC_OFileInfos
          MP3GetID3Tag = Not MP3GetID3Tag
          CheckApiMenu 303&, MP3GetID3Tag
          If MP3GetID3Tag Then
            Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 5&)
            Else
            Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 0&)
            End If
          SetPanelSize
        Case KbdC_OFollowSong
          xFollowCurSong = Not xFollowCurSong
          CheckApiMenu 310&, xFollowCurSong
          If xFollowCurSong And SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 2 Then
            xLong1 = xFindSong
            If Not xLong1 = 0 Then xMarkSong xLong1, False, False
            End If
        Case KbdC_ORemMissing
          xRemoveMissingSongs = Not xRemoveMissingSongs
          CheckApiMenu 320&, xRemoveMissingSongs
        Case KbdC_ORemSkipped
          xRemoveSkippedSongs = Not xRemoveSkippedSongs
          CheckApiMenu 321&, xRemoveSkippedSongs
        Case KbdC_ORemPlayed
          xRemovePlayedSongs = Not xRemovePlayedSongs
          CheckApiMenu 322&, xRemovePlayedSongs
        Case KbdC_OXFade
          'If xProcessMode = 1& Then xProcessMode = 0& Else xProcessMode = 1&
          xProcessMode = 1&
          MainForm_ApplyProcessMode
        Case KbdC_OMDMode
          'If xProcessMode = 2& Then xProcessMode = 0& Else xProcessMode = 2&
          xProcessMode = 2&
          MainForm_ApplyProcessMode
        Case KbdC_OStopEach
          'If xProcessMode = 3& Then xProcessMode = 0& Else xProcessMode = 3&
          xProcessMode = 3&
          MainForm_ApplyProcessMode
        Case KbdC_OEndModeTggle
          xProcessMode = (xProcessMode + 1&) Mod 4&
          MainForm_ApplyProcessMode
        Case KbdC_ONoEndMode
          xProcessMode = 0&
          MainForm_ApplyProcessMode
        Case KbdC_OInternetRadio, KbdC_GIRadio
          If Not vKioskMode Then
            If Not InterfaceMode = Interface_Mode_Radio Then
              ToggleInterface Interface_Mode_Radio
              End If
            End If
        Case KbdC_OInternetRadioRe, KbdC_GIRadioRe
          If Not vKioskMode Then
            If Not Len(PLCurFullURL) = 0& Then
              StartWeb PLCurFullURL, True
              End If
            End If
        Case KbdC_OInternetRadioHP, KbdC_GIRadioHP
          If CurMusic = ctSSLayer_MusicTypeStream And (xSongIsMidi = 3&) Then
            SaveOpenURL SenderURL
            End If
        Case KbdC_OAutoRF, KbdC_GAutoRF
          If Not xNoAutoRefreshByUser Then
            MainForm_xAutoRefreshNow False, False, False, False, False
            CheckReplay
            End If
        Case KbdC_OAutoRFRem
          If Not xNoAutoRefreshByUser Then
            AutoRefreshHandler 1&
            End If
        Case KbdC_OAutoRFRemI
          If Not xNoAutoRefreshByUser Then
            AutoRefreshHandler 2&
            End If
        Case KbdC_OAutoRFSetup
          If Not (xNoAutoRefreshByUser Or xNoAutoSetup) Then
            ShowAutoRefresh
            End If
        Case KbdC_OAutoRFAlt, KbdC_GAutoRFAlt
          If Not xNoAutoRefreshByUser Then
            AutoRefreshHandler 4&
            End If
        Case KbdC_OTimerEdit
          If Not xNoTimers Then
          'If Not xSecureMode Then
            TimerMenuEvent 0&
            End If
        Case KbdC_OTimerImport
          If Not (xNoTimers Or xSecureMode) Then
            TimerMenuEvent 2&
            End If
        Case KbdC_OTimerExport
          If Not (xNoTimers Or xSecureMode) Then
            TimerMenuEvent 3&
            End If
        Case KbdC_OTimerClear
          If Not xNoTimers Then
            TimerMenuEvent 5&
            End If
        Case KbdC_OPresetSave
          If Not xSecureMode Then
            MenuEventPlaylistLoader 0&
            End If
        Case KbdC_OPresetLoad
          If Not xSecureMode Then
            MenuEventPlaylistLoader 1&
            End If
        Case KbdC_OSaveSetts
          If Not xNoSetup And xCmdSaveSet Then
            MainForm_SaveSettings 0&, False
            SavePList AppBase(DefaultPLExtension), True
            End If
        Case KbdC_OResetSnd, KbdC_GResetSND
          MainForm_OutputChange MainHWnd, False, True
        Case KbdC_OVolCtrl, KbdC_GVolCtrl
          'On Error Resume Next
          'Shell "sndvol32", vbNormalFocus
          If Not ShellExecute(0&, "open", "sndvol32", "", "", 5&) Then
            Call ShellExecute(0&, "open", "sndvol", "", "", 5&)
            End If
          'On Error GoTo 0
        Case KbdC_CMoreSetts
          If Not xNoSetup Then
''            If SetupDialogHWnd = 0& Then
'              'LockMe True
'              ShowIcon 0&
            ShowSetup
'              'LockMe False
'              ShowIcon 0&
'            'ShowVBWindowX frmSetup, SetupVisible
            End If
        Case KbdC_GShowMenu 'show menu/advmenu
          Call SetMenuDefaultItem(xMenu(3), -1&, True)
          'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(3), 0&, 0&)
          MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(3), 0&)
        Case KbdC_GShowAltMenu
          Call SetMenuDefaultItem(xMenu(2), -1&, True)
          'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(2), 0&, 0&)
          MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(2), 0&)
        End Select
      End If
    
    If Index >= KbdC_GPrevSel And Index <= KbdC_GRandSel Then
      If Not InterfaceMode = Interface_Mode_None Then
        ToggleInterface Interface_Mode_None
        GoTo SkipButs
        End If
      End If
    
    
    Select Case Index
      Case KbdC_FRefreshCD
        MenuEventCDDrive CDDriveLetters, True, True
      Case KbdC_FQuitSD, KbdC_GQuit 'quit
        If Not xNoQuitMenu Then
          Select Case xCmdQuitMode
            Case 1&, 4&, 5&
              If hwndShutDownDialog = 0& Then
                If xCmdQuitMode = 1& Then
                  QuitModePending = 1&
                  Else
                  QuitModePending = xCmdQuitMode - 2&
                  End If
                If (PlayerIsLocked Or xAutoDoing) Then
                  'Call PostMessageA(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 2&)
                  WantsShutdownDialog = True
                  Else
                  StartShutDownStuff
                  End If
                End If
            Case 2&: SHShutDownDialog MainHWnd
            Case Else: MainForm_UnloadPlayer
            End Select
          End If
      
      
      Case KbdC_CPlay, KbdC_GPlay
        If Not xKioskModeParam Or PlDClickAction = 0& Then
          MainForm_IconsMouseUp 1&
          End If
      Case KbdC_GPlayPause 'play/pause
        If Not xKioskModeParam Or PlDClickAction = 0& Then
          If xPStateIconPlaying Then
            MainForm_IconsMouseUp 2&
            Else
            MainForm_IconsMouseUp 1&
            End If
          End If
      Case KbdC_CStop, KbdC_GStop 'stop/abort
        MainForm_IconsMouseUp 3&
      Case KbdC_CNext, KbdC_GNext 'next
        If Not xKioskModeParam Or PlDClickAction = 0& Then
          MainForm_IconsMouseUp 4&
          End If
      Case KbdC_CEnd, KbdC_GEnd 'endsong
        If Not (xKioskModeParam And PlDClickAction = 2&) Then
          MainForm_IconsMouseUp 5&
          End If
        Case KbdC_CCSSelFile
          If Not (CurMusic = ctSSLayer_MusicTypeNone Or (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)) Then
            xLong1 = xFindSong
            If Not xLong1 = 0 Then
              xMarkSong xLong1, False, False
              End If
            End If
      Case KbdC_CVolUp, KbdC_GVolUp
        If Not xKioskModeParam Or vCanVolume Then
          ChangeVolumeMenu 0&
          End If
      Case KbdC_CVolDown, KbdC_GVolDown
        If Not xKioskModeParam Or vCanVolume Then
          ChangeVolumeMenu 1&
          End If
      Case KbdC_CVolMute To KbdC_CVolFull 'Vol 0-100
        If Not xKioskModeParam Or vCanVolume Then
          ChangeVolumeMenu 3& + (Index - KbdC_CVolMute)
          End If
      Case KbdC_GVolMute To KbdC_GVolFull 'Vol 0-100
        If Not xKioskModeParam Or vCanVolume Then
          ChangeVolumeMenu 3& + (Index - KbdC_GVolMute)
          End If
  
  
  
      Case KbdC_CPrevSongs
        If IsWindowVisible(xListHWnd(1)) Then
          ShowWindow xListHWnd(1), 0&
          DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
          Else
          ShowWindow xListHWnd(1), 5&
          DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 1&
          SetFocusAPI xListHWnd(1)
          End If
      
      
      
      Case KbdC_GPlayDef
        MainForm_PlayListDoubleClickEvent
        'If Not xKioskModeParam Or PlDClickAction = 0& Then
        '  MainForm_MenuEventPlaylist 0
        '  End If
      Case KbdC_PPlaySkip, KbdC_GPlaySkp
        If Not xKioskModeParam Or PlDClickAction = 0& Then
          MainForm_MenuEventPlaylist 0
          End If
      Case KbdC_PPlayEnd, KbdC_GPlayEnd
        If Not (xKioskModeParam And PlDClickAction = 2&) Then
          MainForm_MenuEventPlaylist 1
          End If
      Case KbdC_PPlayNext, KbdC_GPlayNxt
        MainForm_MenuEventPlaylist 2
      Case KbdC_GPrevSel
        xFindNextItemExPL -1&
        'xMarkSong xFindNextItem(-1&, True, False, True, False), False, False
        'xFollowCurSongLast = DoEventsEx_GetTickCount
        'xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
      Case KbdC_GNextSel
        xFindNextItemExPL 1&
        'xMarkSong xFindNextItem(1&, True, False, True, False), False, False
        'xFollowCurSongLast = DoEventsEx_GetTickCount
        'xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
      Case KbdC_GPrevPage
        Call SendMessage(xListHWnd(2), &H100&, &H21&, 0&)
        Call SendMessage(xListHWnd(2), &H101&, &H21&, 0&)
      Case KbdC_GNextPage
        Call SendMessage(xListHWnd(2), &H100&, &H22&, 0&)
        Call SendMessage(xListHWnd(2), &H101&, &H22&, 0&)
      Case KbdC_PFindCurrent, KbdC_GCurNSel
        MainForm_MenuEventPlaylist 4
      Case KbdC_PFind
        MainForm_MenuEventPlaylist 5
      Case KbdC_PFindNext
        MainForm_MenuEventPlaylist 6
      Case KbdC_PFindQuick
        ToggleInterface Interface_Mode_Search
      Case KbdC_PSelRnd, KbdC_GRandSel
        If Not PLCount = 0& Then
          xLong1 = RandomSong
          If Not xLong1 = -1& Then
            xMarkSong xLong1, False, False
            End If
          End If
      
      Case KbdC_PGotoDir
        If xDirList < 0& Then
          If IsWindowVisible(xListHWnd(0)) Then
            ShowWindow xListHWnd(0), 0&
            DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
            Else
            ShowWindow xListHWnd(0), 5&
            DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 1&
            SetFocusAPI xListHWnd(0)
            End If
          End If
      
      
      
      
      Case KbdC_ODirToggle
  '      If Not xNoDirList Then
          DirListEvent 0&
  '        End If
      Case KbdC_ODirInc
  '      If Not xNoDirList Then
          DirListEvent 2&
  '        End If
      Case KbdC_ODirDec
  '      If Not xNoDirList Then
          DirListEvent 3&
  '        End If
      
      
      Case KbdC_IHelp
        If Not (xKioskModeParam Or xSecureMode) Then
          If xUseChmHelp Then
            Call HtmlHelp(MainHWnd, xRealBaseFile + "chm", 0&, "welcome.htm")
            Else
            Call WinHelp(MainHWnd, xRealBaseFile + "hlp", &H101&, "Welcome")
            End If
          End If
      Case KbdC_IAbout
'        'ShowVBWindowX frmInfo, xInfo
'        DialogTimer True
'        Call LoadResEx_Dialog(MainHWnd, 2200&, , 0&, AddressOf GenericMessageHandler)
'        DialogTimer False
        ShowInfo

    
    
    
    
    '  Case 109& 'gplaykeys
    '  Case 110& To 116 ' prev / play / ...
    '  Case 113& ' STOP
    '  Case 116& To 120& ' repeatmode / toggle
    '  Case 120&, 121& 'Seek
    '  Case 122&, 123& 'volup/down
      
      
      
    '  Case 124&, 125& 'autorefr / delautorefr
      
  '    Case KbdC_GShowPlayer 'show player
  '      ShowPlayer true
      
      Case KbdC_GShowVolMenu 'show volmenu
        If Not xKioskModeParam Or vCanVolume Then
          'Call SetMenuDefaultItem(xMenu(4), -1&, True)
          'MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(4), 0&)
          MainForm_MenuEvent GetAPIPopupMenu(0&, xMenu(4), 0&)
          End If
      
      Case KbdC_GShowTitle
      'ShowIR
        If xPStateIconPlaying And Not (CurMusic = ctSSLayer_MusicTypeNone Or PlayerIsLocked) Then
          ShowIR_Do False
          End If
      
    '  Case 130&, 131& 'remdelfile / explorer
  '    Case 132& 'quit
      End Select
    End If

  If Index = KbdC_GShowPlayer And Not xInfoQuit Then
    ShowPlayer True
    ElseIf Index = KbdC_GShowPlayerPS And Not xInfoQuit Then
    ShowPlayer False
    ElseIf Index = KbdC_GAbortReading And Not vKioskMode Then
    AbortReading = True
    End If
SkipButs:
  #If UseFileLogging And DetailLog Then
    MainForm_ctConsoleA_AddLog "Keyprocessing finished"
    #End If
  End Sub

Private Sub EndSongEx()
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  setting as next song"
    #End If
  
  PLNext = PLCurItem
  #If BoldDirs = 98 Then
    PlNextSong = PLI(PLCurItem).Filename
    PlNextSongCap = PLI(PLCurItem).Caption
    #Else
    PlNextSong = PLIFile(PLCurItem)
    PlNextSongCap = PLICap(PLCurItem)
    #End If
  SetNextSong True
  SilentPlNext = True
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  done"
    #End If
  End Sub

Private Function PrevInstanceEx(ByVal xChar1 As String) As Boolean
  Dim xLong1 As Long
  'Dim xLong2 As Long
  Dim cds As COPYDATASTRUCT
  
  xLong1 = FindWindow(CalaymClassNameMsg, CalaymClassTitleMsg)
  If Not xLong1 = 0& Then
    xChar1 = FilteredCMD(xChar1) & vbNullChar
    cds.cbData = Len(xChar1) * 2&
    If cds.cbData = 2& Then
      Call SendMessage(xLong1, &H111&, 600&, 0&)
      Else
      cds.lpData = LocalAlloc(0&, cds.cbData)
      If Not cds.lpData = 0& Then
        Call RtlMoveMemory(ByVal cds.lpData, ByVal StrPtr(xChar1), cds.cbData)
        cds.dwdata = 1& - CLng(EnqueueFlag)
        Call SendMessage(xLong1, &H4A&, 0&, ByVal VarPtr(cds))
        Call LocalFree(cds.lpData)
        End If
      End If
    PrevInstanceEx = True
    End If
  End Function

Private Function MainForm_GetMessageA_CopyData(ByVal lParam As Long) As Long
  Dim cds As COPYDATASTRUCT
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "SendMessageA_CopyData Received" ', pushing to cds (" & CStr(PointerToCDStruct) & ")"
    #End If
  
  If GMCD_Pending Then
    MainForm_GetMessageA_CopyData = 2&
    ElseIf Not lParam = 0& Then
    Call RtlMoveMemory(cds, ByVal lParam, 12&)
    If Not (cds.lpData = 0& Or cds.cbData = 0&) Then
  
    '  Call RtlMoveMemory(cds, ByVal PointerToCDStruct, 12&)
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  Receiving data"
        #End If
      If cds.dwdata = 0& Then
        GMCD_URL = PeekS_Ansi(cds.lpData, cds.cbData)
        GMCD_Mode = -1&
        Else
        GMCD_URL = PeekS_Wide(cds.lpData, cds.cbData / 2&)
        GMCD_Mode = cds.dwdata
        End If
    '  If Right(gmcd_url, 1&) = vbNullChar Then gmcd_url = Left(gmcd_url, cds.cbData - 1&)
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  Received (Mode " + CStr(cds.dwdata) + "): " & Replace(GMCD_URL, vbCrLf, vbCrLf + Space(8))
        #End If
      MainForm_GetMessageA_CopyData = 1&
      GMCD_Pending = True
      Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 3&)
      End If
    End If
  End Function

Private Sub PostGetMessage()
  If Not (PlayerIsLocked Or xAutoDoing) Then
    If Not InStr(GMCD_URL, "://") = 0& Or GMCD_Mode = 3& Then
      'MainForm_GetMessageA_CopyData = -CLng(StartWeb(GMCD_URL))
      Call StartWeb(GMCD_URL, True)
      ElseIf AllowedItem(GMCD_URL, True) = 255& And Not LCase(GMCD_URL) = LCase(xCurPlistFilename) Then
      'MainForm_GetMessageA_CopyData = -CLng(LoadPList(GMCD_URL, (GMCD_Mode = 2&), False)) ', False))
      Call LoadPList(GMCD_URL, (GMCD_Mode = 2&), False)
      ElseIf AllowedItem(GMCD_URL, True) = 0& Then
      'MainForm_GetMessageA_CopyData = -CLng(AddFDirNowSubEx(GMCD_URL, Not (GMCD_Mode = 2&)))
      Call AddFDirNowSubEx(GMCD_URL, Not (GMCD_Mode = 2&))
      ElseIf GMCD_URL = "test" Then
      ShowPlayer True
      'MainForm_GetMessageA_CopyData = 1&
      End If
    End If
  DoEvents 'fetch all other pendings to be ignored
  GMCD_Pending = False
  End Sub

'private Sub MainForm_GetMessageA_ReqData(ByVal PointerToCDStruct As Long)
'  If Not PointerToCDStruct = 0& Then
'    If (xOldIconState = 1& Or xOldIconState = 2&) And Not (CurMusic = ctSSLayer_MusicTypeNone Or PLCurSong = "") Then
'      Call SetWindowText(PointerToCDStruct, CStr(xOldIconState + 1&) & "|" & PLCurSong)
'      Else
'      Call SetWindowText(PointerToCDStruct, PLCurSong)
'      End If
'    End If
'  End Sub

Private Function BoolChar(ByRef xBool1 As Boolean) As String
  BoolChar = Chr(48& - CLng(xBool1))
  End Function

Private Sub OpenInExplorer(Optional ByVal xChar1 As String = vbNullChar)
  If Not (vKioskMode Or xSecureMode) Then
    If xChar1 = vbNullChar Then
      If Not (PLCurSong = "" Or CurMusic = ctSSLayer_MusicTypeNone) Then
        xChar1 = PLCurSong
        End If
      End If
    If Not xChar1 = vbNullChar Then
      If Not xChar1 = "" Then
        If FileExists(xChar1) Then
          'On Error Resume Next
          'Shell "explorer.exe /e,/select," + Chr(34) + xChar1 + Chr(34), vbNormalFocus
          Call ShellExecute(MainHWnd, "open", "explorer", "/e,/select," + Chr(34) + xChar1 + Chr(34), "", 5&)
          'On Error GoTo 0
          End If
        End If
      End If
    End If
  End Sub

#If UseFileLogging Then
  Private Sub MainForm_PrintCDDARev()
    If CDDAUse Then
      If CDDAJitter Then
        MainForm_ctConsoleA_AddLog "    CDDA: on, JitterCorrection: On"
        Else
        MainForm_ctConsoleA_AddLog "    CDDA: on, JitterCorrection: Off"
        End If
      Else
      If CDDAJitter Then
        MainForm_ctConsoleA_AddLog "    CDDA: off, JitterCorrection: On"
        Else
        MainForm_ctConsoleA_AddLog "    CDDA: off, JitterCorrection: Off"
        End If
      End If
    End Sub
  #End If


Private Function TrackerIsLooping() As Boolean
  If xXFadeCurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And 4&) = 0& Then
    TrackerIsLooping = ctSSLayer_MusicHasFinished(SongPointer(OldSongID))
    ElseIf xXFadeCurMusic = ctSSLayer_MusicTypeTracker Then
    TrackerIsLooping = ctSSLayer_MusicHasFinished(SongPointer(OldSongID))
    End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  TrackerIsLooping=" & -CLng(TrackerIsLooping)
    #End If
  End Function

Private Sub CalcCrossFadeConsts()
  Const CrossFadeOverlap As Long = 80&
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  For xLong1 = 0 To xFadeConstsX
    xLong2 = xLong1 + xFadeConsts1
    'CrossFadeWaitTime = 1000&
    'CrossFadeWaitSeek = 1000&
    CrossFadeTimeDelay(xLong1) = xLong2 * 1000& * (100 - CrossFadeOverlap) / 200&
    CrossFadeTimeSingle(xLong1) = xLong2 * 1000 - CrossFadeTimeDelay(xLong1)
      
    CrossFadeMinTime(xLong1) = xLong2 * 3000& + CrossFadeWaitSeek + 5000& ' 12 - 66
    CrossFadeNewMinMinTime(xLong1) = CrossFadeTimeSingle(xLong1) * 2& + CrossFadeWaitSeek + 4000&
      CrossFadeMinRemainTime(xLong1) = CrossFadeTimeSingle(xLong1)
      CrossFadeMaxPos(xLong1) = xLong2 * 1000 + 1500&
      CrossFadeMinPos(xLong1) = CrossFadeMaxPos(xLong1) + 2250& + xLong2 * 200& ' *200 = 1/5 -> 0.4 - 4 seks
      
    CrossFadeMinPatterns(xLong1) = 10& + (xLong2 \ 10&)
    CrossFadeNewMinMinPatterns(xLong1) = 3& + (xLong2 \ 10&)
      CrossFadeMaxPattern(xLong1) = 2& + (xLong2 \ 10&)
      CrossFadeMinPattern(xLong1) = 3& + (xLong2 \ 10&)
      CrossFadeMinRemainPatterns(xLong1) = 2& + (xLong2 \ 10&)
      
    '      CrossFadeMinTime = CrossFadeTimeTotal * 2&
    '      CrossFadeNewMinMinTime = CrossFadeTimeTotal * 1.5
    '    CrossFadeMinTime = CrossFadeTimeTotal * 2.5 + 2000& + CrossFadeMinPos
    Next
  End Sub

Private Sub MainForm_SetStandby(ByVal SetMe As Boolean)
  Static xStandby As Boolean
'  Static xBool1 As Boolean
  
  If SetMe Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  Standing by / Hybernate"
      #End If
    If Not PlayerIsLocked Or xStandby Then
      xStandby = True
      MainForm_LockMe True
      xPStateIconSndcard = True
      MainForm_UpdateMainIcon
      
      Call ResetSystem(False, False)
      Call Tear_WaitFor(-2&)
      Call ctSSLayer_Close(0&)
      End If
    ElseIf xStandby Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  Done: Standing by / Hybernate"
      #End If
    xStandby = False
    Call ctSSLayer_RefreshDeviceList(MainHWnd)
    xInitMe MainHWnd, False, False, False
    Call ResetSystem(True, False)
    'xEndSongTimerEventTime = DoEventsEx_GetTickCount
    'LastPosTime = xEndSongTimerEventTime
    xPStateIconSndcard = False
    MainForm_UpdateMainIcon
    MainForm_LockMe False
#If UseFileLogging Then
    Else
    MainForm_ctConsoleA_AddLog "  (Done: Standing by / Hybernate)"
#End If
    End If
  End Sub

Private Sub StoreResume(ByVal ResumeNow As Boolean, ByVal WaitTime As Long)
  'WaitTime: 0 = resume std
  '          1 = resume next auto / nopause (for deleting old file / device has been removed)
  
  'Static xLong1 As Long
  Static xLong2 As Long
  Static xBool1 As Boolean ' play
  Static xBool2 As Boolean ' paused
  Static xBool3 As Boolean
  'Dim xBool3 As Boolean

'Debug.Print "Pre:", xBool1, xBool2, xBool3, xLong2, PLCurFullURL
  If ResumeNow Then
    If WaitTime = 1& Then 'Not (xBool2 And WaitTime = 1&) Then
      If xBool2 Then
        xReAutoPlay = False
        ReAutoPlayDR = False
        xBool1 = False
        End If
      ElseIf xBool1 Then 'And Not xLong1 = 0& Then
      xPStateIconPlaying = True
      If xBool3 Then
'Debug.Print "A1", PLCurFullURL
        If xPlayItem(False, xGenFading, False, True, xBool2, PLCurFullURL) Then
'Debug.Print "A2"
          If Not CurMusic = ctSSLayer_MusicTypeNone Then
'Debug.Print "A3"
            GoTo URLHack1
            End If
          End If
        xBool2 = False
        xLong2 = 0&
        End If
      
      'If xPlayItem(False, xGenFading And Not xBool2, False, True, xBool2) Then
      If xPlayItem(False, xGenFading, False, True, xBool2, "") Then
        If Not CurMusic = ctSSLayer_MusicTypeNone Then
URLHack1:
          xPStateIconPaused = xBool2
          MainForm_UpdateMainIcon
          'If Not (xLong2 < 0& Or xLong2 > PLCurSongLength - 1&) Then
            Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), xLong2)
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  IsPosition: " & CStr(xLong2)
            #End If
            'End If
          If (xGenFading And Tear_StepT(CurrentSongID) = 0&) And Not xBool2 Then
            Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 0&, 0&, SimpleFadingTotalTime, 1&)
            End If
          WaitTime = 0&
          End If
        Else
        If xBool2 Then
          WaitTime = 0&
          xBool1 = False
          Else
          WaitTime = 1&
          End If
        xPStateIconPlaying = False
xReAutoPlay = False
ReAutoPlayDR = False
        MainForm_UpdateMainIcon
        '''''''If Not PlNextSong = "" Then ShowIcon 2&
        End If
      End If
    
    If WaitTime = 1& Then
      xPStateIconPlaying = False
      MainForm_LockMe False
      If PlNextSong = "" Then
        If xRep = 2& Then
          PLCurItem = RandomSong
          Else
          If Not PLCurItem = 0& Then PLCurItem = PLCurItem - 1&
          ''xFindNextItem 1&, False , False, False, False
          'xFindNextItem 1&, True, False, True, False
          xFindNextItem 1&, True, False, False, False
          End If
        Else
        MainForm_LockMe False
        MainForm_PlayNext True, 1&, False, False
        End If
      End If
    If xBool1 Then
      MainForm_xPlayClick False
      End If
    
    
    Else
    If xPStateIconPlaying Then
      xLong2 = PLCurSongPos
      xBool1 = Not (CurMusic = ctSSLayer_MusicTypeNone)
      xBool2 = xBool1 And xPStateIconPaused
      xBool3 = ((CurMusic = ctSSLayer_MusicTypeStream) And (xSongIsMidi = 3&))
      MainForm_xEndSong (Not xPStateIconPaused), False, False
      xPStateIconPlaying = False
      MainForm_UpdateMainIcon
      If Not (xPStateIconPaused Or WaitTime = 0&) Then
        DoEventsEx WaitTime
        End If
      Else
      xBool1 = False
      xBool2 = False
      xBool3 = False
      End If
    End If
'Debug.Print "Post:", xBool1, xBool2, xBool3, xLong2, PLCurFullURL
  End Sub

Private Sub ResetSystem(ByRef ReInit As Boolean, ByRef LockOnly As Boolean)
  If ReInit Then
    If Not LockOnly Then Call StoreResume(True, 0&)
    LEDState = 0&
    Else
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Preparing to close sound system"
      #End If
    If Not LockOnly Then Call StoreResume(False, 2000&)
    End If
  End Sub

Public Sub MainForm_TrySearch(ByVal Force As Boolean) '(ByVal TryAddList As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xLong5 As Long
  Dim xLong6 As Long
  Dim xChar1 As String
  Dim xBool2 As Boolean
  Dim xLongA1(15) As String
  Dim xCharA1(15) As String
  Dim xCharA2(15) As String
  
  If Force Or Not (PlayerIsLocked Or xAutoDoing) Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H184&, 0&, 0&)
    xSearchCount = 0&
    If Len(Trim(CPListText)) = 0& Then
      xLong5 = -3&
      Else
      xSearchCount = 0&
      xChar1 = LCase(CPListText) & ";"
      xLong3 = -1&
      Do
        xLong1 = InStr(xChar1, ";")
        If xLong1 = 0& Then
          Exit Do
          Else
          xCharA1(xLong2) = Trim(Left(xChar1, xLong1 - 1&))
          If Not xCharA1(xLong2) = "" Then
            Select Case Asc(xCharA1(xLong2))
              Case 43 '+
                xCharA1(xLong2) = LTrim(Mid(xCharA1(xLong2), 2&))
                If Not xCharA1(xLong2) = "" Then
                  xLongA1(xLong2) = 1&
                  xLong3 = xLong2
                  xBool2 = True
                  xLong2 = xLong2 + 1&
                  End If
              Case 45 '-
                xCharA1(xLong2) = LTrim(Mid(xCharA1(xLong2), 2&))
                If Not xCharA1(xLong2) = "" Then
                  xLongA1(xLong2) = 2&
                  xLong2 = xLong2 + 1&
                  End If
              Case Else
                If Not xCharA1(xLong2) = "" Then
                  xLongA1(xLong2) = 0&
                  If Not xBool2 Then xLong3 = xLong2
                  xCharA2(xLong6) = xCharA1(xLong2)
                  xLong6 = xLong6 + 1&
                  xLong2 = xLong2 + 1&
                  End If
              End Select
            If xLong2 = 16& Then
              xLong5 = -2&
              Exit Do
              End If
            End If
          End If
        xChar1 = Mid(xChar1, xLong1 + 1&)
        Loop
      'If xLong3 = -1& Then
      '  End If
      
      If Not (xLong2 = 0& Or xLong3 = -1& Or xLong2 = 16&) Then
        xLong2 = xLong2 - 1&
        For xLong1 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
          If Not InStr(1&, LCase(PLI(xLong1).Caption), xCharA1(xLong3), vbTextCompare) = 0& Or (xLong6 > 1& And Not xBool2) Then
#Else
          If Not InStr(1&, LCase(PLICap(xLong1)), xCharA1(xLong3), vbTextCompare) = 0& Or (xLong6 > 1& And Not xBool2) Then
#End If
            xLong5 = 0&
            ' 0 = or found
            ' 1 = need
            ' 2 = must not
            
            ' 0 = init
            '-1 = nomatch
            '-2 = parseerror
            ' 1 = limitedfound
            For xLong4 = 0& To xLong2
#If BoldDirs = 98 Then
              If InStr(1&, PLI(xLong1).Caption, xCharA1(xLong4), vbTextCompare) = 0& Then
#Else
              If InStr(1&, PLICap(xLong1), xCharA1(xLong4), vbTextCompare) = 0& Then
#End If
                If xLongA1(xLong4) = 1& Then
                  xLong5 = -1&
                  Exit For
                  End If
                ElseIf xLongA1(xLong4) = 0& Then
                xLong5 = 1&
                'ElseIf xLongA1(xLong4) = 1& Then
                ElseIf xLongA1(xLong4) = 2& Then
                xLong5 = -1&
                Exit For
                End If
              Next
            If xLong5 = 1& Or (xLong5 = 0& And xLong6 = 0&) Then
              If xSearchCount = xSearchCountMax Then
                xSearchCount = xSearchCountOver
                Exit For
                Else
                xSearch(xSearchCount) = xLong1
                'If Asc(PLItem(xLong1)) = 0& Then
                '  xSearch(xSearchCount) = SimpleFile(PLItem(xLong1)) & vbNullChar & Mid(PLItem(xLong1), 3&) & vbNullChar & Chr(1) & CStr(xLong1)
                '  Else
                '  xSearch(xSearchCount) = SimpleFile(PLItem(xLong1)) & vbNullChar & Mid(PLItem(xLong1), 3&) & vbNullChar & vbNullChar & CStr(xLong1)
                '  End If
                xSearchCount = xSearchCount + 1&
                End If
              End If
            End If
          Next
        Else
        xLong5 = -2&
        End If
      End If
    If (xSearchCount = 0& Or xSearchCount = xSearchCountOver) Then
      EnableWindow Interface_Control(Interface_Control_Search_Results), 0&
      EnableWindow Interface_Control(Interface_Control_Search_Play), 0&
      EnableWindow Interface_Control(Interface_Control_Search_Select), 0&
      Else
      EnableWindow Interface_Control(Interface_Control_Search_Results), 1&
      EnableWindow Interface_Control(Interface_Control_Search_Play), 1&
      EnableWindow Interface_Control(Interface_Control_Search_Select), 1&
      End If
    Call SelectObject(xSharedDC, hFontPLLgB)
    If xSearchCount = 0& Then
      If xLong5 = -3& Then
        xSearchError = LoadResEx_String(2324&)
        ElseIf xLong5 = -2& Then
        xSearchError = LoadResEx_String(2323&)
        Else
        xSearchError = LoadResEx_String(2321&)
        End If
      xLong4 = GetTextOutWidth(xSharedDC, xSearchError)
      Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, -1&)
      Else
      If xSearchCount = xSearchCountOver Then
        xSearchError = LoadResEx_String(2322&)
        xLong4 = GetTextOutWidth(xSharedDC, xSearchError)
        Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, -1&)
        Else
        
        
        SortSearchResults
'        Call ArraySort_StringSearch
        
        
        For xLong1 = 0 To xSearchCount - 1&
#If BoldDirs = 98 Then
          xChar1 = PLI(xSearch(xLong1)).Caption
          Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, xLong1)
          xLong5 = PLI(xSearch(xLong1)).pxWidthLg
#Else
          xChar1 = PLICap(xSearch(xLong1))
          Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, xLong1)
          xLong5 = PLIPxLg(xSearch(xLong1))
#End If
          If xLong5 > xLong4 Then xLong4 = xLong5
          Next
        Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H19E, 0&, 0&)
        Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H185&, 1&, 0&)
        MainForm_CheckDir False
        xLong4 = 24& + xLong4
        End If
      End If
    xLong4 = 4& + xLong4
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H194&, lWidth + xLong4, 0&)
    'eButton(3).Enabled = Not (xSearchOldQueriesCount = 0&)
    
    CPListMustDo = False
'    DoSearchAddEx 'TryAddList
    End If
  End Sub

Public Function MainForm_TrySearchNow() As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long

  If Not (PlayerIsLocked Or xAutoDoing Or xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo) Then
    If Not SendMessage(Interface_Control(Interface_Control_Search_Results), &H190&, 0&, 0&) = 0& Then
      xLong2 = -1&
      Call SendMessage(xListHWnd(2), &H185&, 0&, -1&)
      For xLong1 = 0& To xSearchCount - 1&
        If SendMessage(Interface_Control(Interface_Control_Search_Results), &H187&, xLong1, 0&) > 0& Then
          Call SendMessage(xListHWnd(2), &H185&, 1&, xSearch(xLong1))
'          Debug.Print "Select:", xLong1, xSearch(xLong1)
          If xLong2 = -1& Then xLong2 = xLong1
          End If
        Next
'      Exit Function
      xLong1 = SendMessage(Interface_Control(Interface_Control_Search_Results), &H19F&, 0&, 0&)
      If xLong2 = -1& Then xLong2 = xLong1
      If xLong1 = -1& Then
        If Not xLong2 = -1& Then
          Call SendMessage(xListHWnd(2), &H19E&, xSearch(xLong2), 1&)
          MainForm_CheckDir False
          End If
        Else
        If SendMessage(Interface_Control(Interface_Control_Search_Results), &H187&, xLong1, 0&) > 0& Then
          Call SendMessage(xListHWnd(2), &H19E&, xSearch(xLong1), 1&)
          ElseIf Not xLong2 = -1& Then
          Call SendMessage(xListHWnd(2), &H19E&, xSearch(xLong2), 1&)
          Call SendMessage(xListHWnd(2), &H19E&, xSearch(xLong1), 0&)
          End If
        MainForm_CheckDir False
        End If
      MainForm_TrySearchNow = True
      End If
    'xMarkSong xSearch(Index), False, False
    End If
  End Function

Public Function MainForm_TrySearchNowEx() As Boolean
  Dim xLong1 As Long
  If Not (PlayerIsLocked Or xAutoDoing Or xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo) Then
    If SendMessage(Interface_Control(Interface_Control_Search_Results), &H190&, 0&, 0&) = 1& Then
      xLong1 = SendMessage(Interface_Control(Interface_Control_Search_Results), &H19F&, 0&, 0&)
      If Not xLong1 = -1& Then
        If SendMessage(Interface_Control(Interface_Control_Search_Results), &H187&, xLong1, 0&) > 0& Then
          xMarkSong xSearch(xLong1), False, False
          MainForm_TrySearchNowEx = True
          End If
        End If
      End If
    End If
  End Function

#If UseFileLogging Then
  Private Function CheckSync() As Boolean
    Dim xLong1 As Long
    
    If PLCount = SendMessage(xListHWnd(2), LB_GETCOUNT, 0&, 0&) Then
      CheckSync = True
      For xLong1 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
        If Not SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) = xLong1 Then
          Debug.Print "OutOfSync (" + CStr(xLong1) + ", but is " + SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) + ")"
          Debug.Print "  Affects / " + PLI(xLong1).Filename
          Debug.Print "      File: " + PLI(xLong1).Caption
#Else
        If Not SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) = xLong1 Then
          Debug.Print "OutOfSync (" + CStr(xLong1) + ", but is " + SendMessage(xListHWnd(2), LB_GETITEMDATA, xLong1, 0&) + ")"
          Debug.Print "  Affects / " + PLIFile(xLong1)
          Debug.Print "      File: " + PLICap(xLong1)
#End If
          CheckSync = False
          End If
        Next
      End If
    If Not CheckSync Then
      MainForm_ctConsoleA_AddLog "###### Playlist out of sync !!!!"
      MsgBox "Playlist out of sync, please clear playlist; please report this error"
      End If
    End Function
  #End If

Public Sub ShowIR_Do(ByVal Passive As Boolean)
  'Dim xType2 As POINTAPI
  Dim xLong1 As Long
  
  If Not xStaticIconsHWnd(xStaticIcons_ShowIR) = 0& Then
    
    If ShowIRWin Then
      If Not Passive Then
        ShowIRTicks = DoEventsEx_GetTickCount
        End If
      xLong1 = GetDC(xStaticIconsHWnd(xStaticIcons_ShowIR))
      If Not xLong1 = 0& Then
        Call SendMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H14&, xLong1, 0&)
        Call SendMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &HF&, xLong1, 0&)
        Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_ShowIR), xLong1)
        End If
      ElseIf Not Passive Then
      Call ProcClassWnd(1&, &H113&, 0&, 0&)  ' WM_Timer
      
      ShowIRTicks = DoEventsEx_GetTickCount
      ShowWindow xStaticIconsHWnd(xStaticIcons_ShowIR), &H4&
      Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), -1&, 0&, 0&, 0&, 0&, &H213&)
      Call SetTimer(xStaticIconsHWnd(xStaticIcons_ShowIR), 3&, 150&, AddressOf ProcClassWnd)
      ShowIRWin = True
      End If
    End If
  End Sub

Private Sub MainForm_DesireSpectrumState(ByRef Activated As Boolean, ByRef ForceLEDMode As Boolean)
  Static xBool1 As Boolean
  
  If LEDMode = 0& Then
    If (Not xShowSpectrum Or IsIconic(MainHWnd) Or xTrayIconIndicator) Then Activated = False
    If xBool1 Xor Activated Then
      xBool1 = Activated
      If Activated Then
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "Starting spectrum analyzer"
          #End If
        ctSSLayer_SetSpectrum True
        xCanSpectrum = True
        Call SetTimer(MainHWnd, 0, InfoProcInterval, AddressOf InfoProc)
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  Spectrum analyzer started"
          #End If
        Else
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "Stopping Spectrum analyzer"
          #End If
        'Do While xTimer1: DoEventsEx_SleepPeriodical: Loop
        Call KillTimer(MainHWnd, 0)
        'Do While xTimer1: DoEventsEx_SleepPeriodical: Loop
        xCanSpectrum = False
        ctSSLayer_SetSpectrum False
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  Spectrum analyzer stopped"
          #End If
        End If
      End If
    ElseIf ForceLEDMode Then
    'led mode
    If Not xShowSpectrum Then Activated = False
    If xBool1 Xor Activated Then
      xBool1 = Activated
      If Activated Then
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "Starting LED spectrum analyzer"
          #End If
         'no set/kill timer here! InfoProcKb
        ctSSLayer_SetSpectrum True
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  LED Spectrum analyzer started"
          #End If
        Else
        #If UseFileLogging Then
           MainForm_ctConsoleA_AddLog "Stopping LED Spectrum analyzer"
           #End If
        ctSSLayer_SetSpectrum False
        'Do While xTimer1: DoEventsEx_SleepPeriodical: Loop
        'Do While xTimer1: DoEventsEx_SleepPeriodical: Loop
        If LEDState = 0& Then
          LEDState = 1&
          Call InfoProcKb(0&, 0&, 0&, 0&)
          LEDState = 0&
          End If
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  LED Spectrum analyzer stopped"
          #End If
        End If
      End If
    End If
  End Sub

Private Sub MainForm_RestoreScheme()
  If SoundSchemePointer = 0& Then
    SoundSchemePointer = ctSoundSchemeSwitcher_ReadScheme(CalaymBackupSchemeREGName, "", DeleteScheme)
    End If
  If Not SoundSchemePointer = 0& Then
    Call ctSoundSchemeSwitcher_WriteScheme(SoundSchemePointer, ".Current")
    Call ctSoundSchemeSwitcher_DeleteScheme(CalaymBackupSchemeREGName)
    Call ctSoundSchemeSwitcher_FreeScheme(SoundSchemePointer)
    End If
  End Sub

Public Sub MP3GetFormatChanged(ByVal ReDraw As Boolean)
  If MP3GetFormat Then
    PLCurSongDisplaySpacePos = PLCurSongDisplaySpacePosOrg + 20&
    Else
    PLCurSongDisplaySpacePos = PLCurSongDisplaySpacePosOrg
    End If
  If ReDraw Then
    DrawWhiteDC xStaticIconsHWnd(xStaticIcons_CurSong), xListHWnd(1), 0&, True
    End If
  End Sub

#If PresetBinded Then

  'private function MainForm_LoadSettings(ByVal FileName As String) As String
  Private Sub MainForm_LoadSettings(ByVal Filename As String)
    Dim xChar1 As String
    Dim xChar2 As String
    Dim xLong1 As Long
    Dim xLong2 As Long
    Dim xLong4 As Long
    Dim xBool1 As Boolean
    Dim xBool2 As Boolean
    Dim xBool4 As Boolean
    Dim xBool5 As Boolean
    Dim xBool6 As Boolean
    Dim xBool7 As Boolean
    Dim xBoolIRFont As Long
    Dim hFile As Long
    
    hFile = TxtFile_ReadOpen(Filename)
    If Not hFile = 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Loading & performing preset (" & Filename & ")"
        #End If
      If GlobalKeys Then MainForm_SetGlobalKeys False
      
      Do While TxtFile_ReadLine(hFile, xChar1)
        xLong2 = InStr(xChar1 & "=", "=")
        xChar2 = Mid(xChar1, xLong2 + 1&)
        xLong1 = Val(xChar2)
        Select Case UCase(Left(xChar1, xLong2 - 1&))
          
          Case "ALLOWFILEDELETING"
            If Not xSecureMode Then
              xDelMode = (xLong1 = 1&)
              MainForm_EnableApiMenu 133, xDelMode
              MainForm_EnableApiMenu 230, xDelMode
              End If
          Case "AUTOPLAY"
            xAutoPlayback = (xLong1 > 0&)
          Case "BLOCKIDEWHILELOADING"
            xClassicBlockingOnLoad = (xLong1 > 0&)
          Case "CHECKACCURATESTREAMS"
            CheckAccurate = (xLong1 > 0&)
          Case "CHECKSONGSONPLAYLISTLOAD"
            RemoveMissingOnPLLoad = (xLong1 > 0&)
          Case "CONTROLS"
            'If GlobalKeys Then MainForm_SetGlobalKeys False
            GetControls xChar2, xKey()
            MainForm_UpdateMenus
          Case "CROSSFADETIME"
            xLong1 = xLong1 - xFadeConsts1
            If xLong1 < 0& Then
              xCrossFadeTimeM = 0&
              ElseIf xLong1 > xFadeConstsX Then
              xCrossFadeTimeM = xFadeConstsX
              Else
              xCrossFadeTimeM = xLong1
              End If
            'CalcCrossFadeConsts
            'xCrossFadeTimeM = xCrossFadeTime - xFadeConsts1
          Case "DIRECTORYLIST"
            If xLong1 < xDirListMaxX Then
              xLong1 = xDirListMaxX
              ElseIf xLong1 > xDirListMax Then
              xLong1 = xDirListMax
              End If
            If Not (xLong1 = 0& Or xLong1 = xDirList) Then ' Or xLong1 < xDirListMin Or xLong1 > xDirListMax) Then
              If xDirList > 0& Xor xLong1 > 0& Then
                xDirList = xLong1
                MainForm_ToggleDirList True
                Else
                xDirList = xLong1
                If xDirList > 0& Then
                  MainForm_ResizePlayer False
                  End If
                End If
              End If
          Case "DISABLEWINDOWSSOUNDS"
            If Not xLong1 < 0& And xLong1 < 3& Then
              SoundSchemePointerMode = xLong1
              MainForm_ApplyProcessMode
              End If
          Case "ELAPSEDTIME"
            xShowElapsed = (xLong1 > 0&)
          Case "EMPTYPLAYLISTSTARTUP"
            If Not xLong1 < 0& And xLong1 < 7& Then
              xOnEmptyPLStartup = xLong1
              End If
          Case "ENDSONGMODE"
            If Not xLong1 < 0& And xLong1 < 4& Then
              xProcessMode = xLong1
              MainForm_ApplyProcessMode
              End If
          Case "ENDSTREAMS"
            GetValidSkipper ctSSLayer_MusicTypeStream, xLong1
          Case "ENDTRACKERS"
            GetValidSkipper ctSSLayer_MusicTypeTracker, xLong1
          Case "ENDCDAUDIO"
            GetValidSkipper ctSSLayer_MusicTypeCDAudio, xLong1
          Case "FADING"
            xGenFading = (xLong1 > 0&)
            'mOMenu(0&).Checked = xGenFading
            CheckApiMenu 301&, xGenFading
          #If UseFileLogging Then
            Case "CDDA", "CDDAJITTERCORRECTION", "OUTPUTDEVICE", _
                 "FAILSAVE", "NOVERWARN", "TUTORIALSHOWN", _
                 "PLAYERWINDOWSTATE", "PLAYERWINDOWPOS"
              MainForm_ctConsoleA_AddLog "Tag not supported in presets (tag ignored): " & Left(xChar1, xLong2 - 1&)
            #End If
          Case "FOLLOWSONG"
            xFollowCurSong = (xLong1 > 0&)
            CheckApiMenu 310&, xFollowCurSong
          Case "GETFILEFORMAT", "GETMP3FORMAT"
            If MP3GetFormat Xor (xLong1 > 0&) Then
              MP3GetFormat = (xLong1 > 0&)
              'ShowWindow xStaticIconsHWnd(xStaticIcons_MP3FormatIcon), -5& * CLng(MP3GetFormat)
              SetFormatToolTip LoadResEx_String(1900&), ""
              Call MP3GetFormatChanged(True)
              'MainForm_ResizePlayer False
              MainForm_ReadLate "", False
              End If
          Case "GETFILEINFOS"
            If MP3GetID3Tag Xor (xLong1 > 0&) Then
              MP3GetID3Tag = (xLong1 > 0&)
              'If MP3GetID3Tag Then ReadLate ""
              If MP3GetID3Tag Then
                MainForm_ReadLate "", False
                Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 5&)
                Else
                Call ShowWindow(xStaticIconsHWnd(xStaticIcons_Tags), 0&)
                End If
              End If
            CheckApiMenu 303&, MP3GetID3Tag
          Case "GLOBALPLAYKEYS"
            'If GlobalKeys Xor (xLong1 > 0&) Then
              GlobalKeys = Not GlobalKeys '(xLong1 = 1&)
            '  MainForm_SetGlobalKeys GlobalKeys
            '  End If
          Case "IGNOREUNDERSCORED"
            IgnoreUnderscored = (xLong1 > 0&)
          Case "INTROMODE"
            If xLong1 < 0& Then
              IntroModeActive = False
              xLong1 = -xLong1
              Else
              IntroModeActive = True
              End If
            If xLong1 < IntroMdSkipperMinVal Then
              IntroModeTime = IntroMdSkipperMinVal
              ElseIf xLong1 > IntroMdSkipperMaxVal Then
              IntroModeTime = IntroMdSkipperMaxVal
              Else
              IntroModeTime = xLong1
              End If
          Case "PLAYLISTDOUBLECLICK"
            If Not xLong1 < 0& And xLong1 < 3& Then
              PlDClickAction = xLong1
              If InterfaceMode = Interface_Mode_Search Then
                SearchForm_CheckBoxTxtChange
                End If
              End If
          Case "PLAYLISTUPDATEINTERVAL"
            If Not xLong1 < 0& And xLong1 < 3& Then
              LockWinUpd = xLong1
              End If
          Case "PRIORITY"
            If xLong1 < -2& Then
              xLong4 = 8& ' 10& + -2&
              ElseIf xLong1 > 3& Then
              xLong4 = 13& ' 10 + 3&
              Else
              xLong4 = xLong1 + 10&
              End If
          Case "RADIOAUTOCLOSE"
            xRadioAutoClose = (xLong1 > 0&)
          Case "RANDOMSEEK"
            RandomPosOnTrackerSkipper = (xLong1 > 0&)
          Case "REGISTERFOLDERS"
            'If Not xLong1 < 0& And xLong1 < 15& Then
              RegFolderMode(0) = CBool(xLong1 And 1&)
              RegFolderMode(1) = CBool(xLong1 And 2&)
              RegFolderMode(2) = CBool(xLong1 And 4&)
              RegFolderMode(3) = CBool(xLong1 And 8&)
            '  End If
          Case "REMOVEMISSINGSONGS"
            xRemoveMissingSongs = (xLong1 > 0&)
            CheckApiMenu 320&, xRemoveMissingSongs
          Case "REMOVEPLAYEDSONGS"
            xRemovePlayedSongs = (xLong1 > 0&)
            CheckApiMenu 322&, xRemovePlayedSongs
          Case "REMOVESKIPPEDSONGS"
            xRemoveSkippedSongs = (xLong1 > 0&)
            CheckApiMenu 321&, xRemoveSkippedSongs
          Case "REPEATMODE"
            If Not xLong1 < 0& And xLong1 < 3& Then
              xRep = xLong1
              'ApplyRepMode
              End If
          Case "SEARCHAUTOCLOSE"
            xSearchAutoClose = (xLong1 > 0&)
          Case "SEARCHAUTOPLAY"
            xSearchAutoPlay = (xLong1 > 0&)
          Case "SHOWFULLPATH"
            xBool5 = (xLong1 > 0&)
          Case "SHOWFULLTITLE"
            xBool7 = (xLong1 > 0&)
          Case "SORTING"
            xSorting = (xLong1 > 0&)
          Case "SPECTRUM"
            xBool4 = (xLong1 And 1&) = 1&
            xDeluxeSpectrum = (xLong1 And 2&) = 2&
'          Case "TAGSFONT"
'            xBool2 = GrabPLFont(1&, xChar2)
          Case "TITLESPOSITION"
            If Not (xLong1 < 0& Or xLong1 > 15&) Then
              xBoolIRFont = xLong1
              End If
          Case "TITLESWHENMINIMIZED"
            If Not (xLong1 < 0& Or xLong1 > 2&) Then
              xTitlesWhenMinimized = xLong1
              End If
          Case "TRACKERPANSEPERATION"
            If xLong1 < 0& Then
              xModPanning = 0&
              ElseIf xLong1 > 100& Then
              xModPanning = 100&
              Else
              xModPanning = xLong1
              End If
            UpdatePanning
          Case "TRAYICON"
            If Not xTrayIconVisible = (xLong1 > 0&) Then
              xBool1 = (xLong1 > 0&)
              End If
          Case "TRAYLEFTCLICK"
            If Not xLong1 < 0& And xLong1 < 7& Then
              xTrayIconLeft = xLong1
              End If
          Case "TRAYMINIMIZE", "MINIMIZETOTRAY"
            If Not ((xLong1 > 0&) = xTrayIconEnabled Or xShellMode) Then
              xBool6 = (xLong1 > 0&)
              End If
          Case "TRAYRIGHTCLICK"
            If Not xLong1 < 0 And xLong1 < 3& Then
              xTrayIconRight = xLong1
              End If
          Case "USEDFILETYPES"
            xUsedTypes = xChar2
          Case "USEPLAYLISTSETTINGS"
            UsePLSettings = (xLong1 > 0&)
          Case "VOLUME"
            If xLong1 > 255& Then
              xSetVolume = 255&
              ElseIf xLong1 < 0& Then
              xSetVolume = 0&
              Else
              xSetVolume = xLong1
              End If
            DoVolume xSetVolume, True
          #If UseFileLogging Then
            Case Else
              MainForm_ctConsoleA_AddLog "Unknown tag: '" & Left(xChar1, xLong2 - 1&) & "' (tag ignored)"
            #End If
          End Select
        Loop
      Call TxtFile_ReadClose(hFile)
      If GlobalKeys Then MainForm_SetGlobalKeys False
#If UseFileLogging Then
      Else
      MainForm_ctConsoleA_AddLog "Error loading configuration / preset (" & Filename & ")"
#End If
      End If
    
    
    UpdateFolderReg True
'    TestRestoreReg false
    MainForm_ApplyRepMode

    If xBool2 Then
      If Not xTitlesPos = xBoolIRFont Then
        xTitlesPos = xBoolIRFont
        MainForm_UpdateMyFont
        ForceIRPosUpdate
        Else
        MainForm_UpdateMyFont
        End If
      ElseIf Not xTitlesPos = xBoolIRFont Then
      xTitlesPos = xBoolIRFont
      ReCreateIRFont
      ForceIRPosUpdate
      End If

    
    If InterfaceMode = Interface_Mode_Search Then
      UpdateSearchVals True
      ElseIf InterfaceMode = Interface_Mode_Radio Then
      RadioForm_UpdateRadioVals
      End If
    If (xBool1 Xor xTrayIconVisible) Or (xBool6 Xor xTrayIconEnabled) Then
      MainForm_ShowTray False
      End If
    If xBool4 Xor xShowSpectrum Then
      If xShowSpectrum Then
        If LEDMode = 0& Then
          SpektrumToggleEvent
          End If
        Else
        If ctSSLayer_CanUseItem(4&) And InStr(CommandX, "/NOSPECTRUM") = 0& Then
          SpektrumToggleEvent
          End If
        End If
      End If
    
    SetPanelSize
    If ShowFullPath Xor xBool5 Then
      ShowFullPath = xBool5
      MainForm_ReGetWidth
      MainForm_xBuildDirs
      End If
    
    If ShowFullTitle Xor xBool7 Then
      ShowFullTitle = xBool7
      If ShowIRWin Then ShowIR_Do True
      #If SelfPathElipse Then
        'NeedIRPathElipse = True
        NeedIRPathElipse = ""
        #End If
      End If
    
    If Not (xLong4 = 0& Or xLong4 = PlayerPriority + 10&) Then
      UpdatePriority xLong4 - 10&
      End If
    
    CheckFileTypes
    End Sub
  
  Private Function SavePreset(ByVal Filename As String) As Boolean
    Dim hFile As Long

    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Saving preset"
      #End If

    hFile = TxtFile_WriteCreate(Filename)
    If Not hFile = 0& Then
      TxtFile_WriteLine hFile, "AllowFileDeleting=" + BoolChar(xDelMode)
      TxtFile_WriteLine hFile, "AutoPlay=" + BoolChar(xAutoPlayback)
      TxtFile_WriteLine hFile, "BlockIDEWhileLoading=" + BoolChar(xClassicBlockingOnLoad)
      TxtFile_WriteLine hFile, "CheckAccurateStreams=" + BoolChar(CheckAccurate)
      TxtFile_WriteLine hFile, "CheckSongsOnPlaylistLoad=" + BoolChar(RemoveMissingOnPLLoad)
      TxtFile_WriteLine hFile, "Controls=" & MergeControls
      TxtFile_WriteLine hFile, "CrossfadeTime=" & CStr(xCrossFadeTimeM + xFadeConsts1)
      TxtFile_WriteLine hFile, "DirectoryList=" & CStr(xDirList)
      TxtFile_WriteLine hFile, "DisableWindowsSounds=" & CStr(SoundSchemePointerMode)
      TxtFile_WriteLine hFile, "ElapsedTime=" + BoolChar(xShowElapsed)
      TxtFile_WriteLine hFile, "EmptyPlaylistStartup=" + CStr(xOnEmptyPLStartup)
      TxtFile_WriteLine hFile, "EndSongMode=" + CStr(xProcessMode)
      TxtFile_WriteLine hFile, "EndStreams=" + CStr(xTrackerSkipper(ctSSLayer_MusicTypeStream))
      TxtFile_WriteLine hFile, "EndTrackers=" + CStr(xTrackerSkipper(ctSSLayer_MusicTypeTracker))
      TxtFile_WriteLine hFile, "EndCDAudio=" + CStr(xTrackerSkipper(ctSSLayer_MusicTypeCDAudio))
      TxtFile_WriteLine hFile, "Fading=" + BoolChar(xGenFading)
      TxtFile_WriteLine hFile, "FollowSong=" + BoolChar(xFollowCurSong)
      TxtFile_WriteLine hFile, "GetFileInfos=" + BoolChar(MP3GetID3Tag)
      TxtFile_WriteLine hFile, "GetFileFormat=" + BoolChar(MP3GetFormat)
      TxtFile_WriteLine hFile, "GlobalPlayKeys=" + BoolChar(GlobalKeys)
      TxtFile_WriteLine hFile, "IgnoreUnderscored=" + BoolChar(IgnoreUnderscored)
      If IntroModeActive Then
        TxtFile_WriteLine hFile, "IntroMode=" & CStr(IntroModeTime)
        Else
        TxtFile_WriteLine hFile, "IntroMode=" & CStr(-IntroModeTime)
        End If
      TxtFile_WriteLine hFile, "PlaylistDoubleClick=" + CStr(PlDClickAction)
      TxtFile_WriteLine hFile, "PlaylistUpdateInterval=" & CStr(LockWinUpd)
      TxtFile_WriteLine hFile, "Priority=" & CStr(PlayerPriority)
      TxtFile_WriteLine hFile, "RadioAutoClose=" & BoolChar(xRadioAutoClose)
      TxtFile_WriteLine hFile, "RandomSeek=" + BoolChar(RandomPosOnTrackerSkipper)
      TxtFile_WriteLine hFile, "RemoveMissingSongs=" + BoolChar(xRemoveMissingSongs)
      TxtFile_WriteLine hFile, "RemovePlayedSongs=" + BoolChar(xRemovePlayedSongs)
      TxtFile_WriteLine hFile, "RemoveSkippedSongs=" + BoolChar(xRemoveSkippedSongs)
      TxtFile_WriteLine hFile, "RepeatMode=" + CStr(xRep)
      TxtFile_WriteLine hFile, "SearchAutoClose=" + BoolChar(xSearchAutoClose)
      TxtFile_WriteLine hFile, "SearchAutoPlay=" + BoolChar(xSearchAutoPlay)
      TxtFile_WriteLine hFile, "ShowFullPath=" + BoolChar(ShowFullPath)
      TxtFile_WriteLine hFile, "ShowFullTitle=" + BoolChar(ShowFullTitle)
      TxtFile_WriteLine hFile, "Sorting=" + BoolChar(xSorting)
      'If xShowSpectrum Then xLong1 = 1&
      'If xDeluxeSpectrum Then xLong1 = xLong1 Or 2&
      TxtFile_WriteLine hFile, "Spectrum=" + CStr(-CLng(xShowSpectrum) - 2& * CLng(xDeluxeSpectrum))
      TxtFile_WriteLine hFile, "RegisterFolders=" & CStr(-CLng(RegFolderMode(0)) - 2& * CLng(RegFolderMode(1)) - 4& * CLng(RegFolderMode(2) - 8& * CLng(RegFolderMode(3))))
      TxtFile_WriteLine hFile, "TitlesPosition=" + CStr(xTitlesPos)
      TxtFile_WriteLine hFile, "TitlesWhenMinimized=" + CStr(xTitlesWhenMinimized)
      TxtFile_WriteLine hFile, "TrackerPanSeperation=" + CStr(xModPanning)
      TxtFile_WriteLine hFile, "TrayIcon=" + BoolChar(xTrayIconVisible)
      TxtFile_WriteLine hFile, "TrayLeftClick=" + CStr(xTrayIconLeft)
      TxtFile_WriteLine hFile, "TrayMinimize=" + BoolChar(xTrayIconEnabled)
      TxtFile_WriteLine hFile, "TrayRightClick=" + CStr(xTrayIconRight)
      TxtFile_WriteLine hFile, "UsedFileTypes=" + PrettyFileTypes
      TxtFile_WriteLine hFile, "UsePlaylistSettings=" + BoolChar(UsePLSettings)
      TxtFile_WriteLine hFile, "Volume=" + CStr(xSetVolume)
      SavePreset = True
      Call TxtFile_WriteClose(hFile)
#If UseFileLogging Then
      Else
      MainForm_ctConsoleA_AddLog "Error saving preset"
#End If
      End If
    End Function
  #End If

Private Sub CheckFileTypes()
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xLong1 As Long
  
  xUsedTypes = Replace(xUsedTypes, ",", vbNullChar) & vbNullChar
  Do
    xLong1 = InStr(xUsedTypes, vbNullChar)
    If xLong1 = 0& Then
      Exit Do
      Else
      xChar2 = LCase(Left(xUsedTypes, xLong1 - 1&))
      If Not InStr(xSupportedFiles, vbNullChar & xChar2 & vbNullChar) = 0& Then
        xChar1 = xChar1 & vbNullChar & xChar2
        End If
      xUsedTypes = Mid(xUsedTypes, xLong1 + 1&)
      End If
    Loop
  If xChar1 = "" Then
    xUsedTypes = vbNullChar & Replace(Replace(ctSSLayer_GetInfoString(&H19&), ";", vbNullChar), ",", vbNullChar)
    Else
    xUsedTypes = xChar1 & vbNullChar
    End If
  xUsedTypesResort = True
  End Sub

Private Function PrettyFileTypes() As String
  If xUsedTypesResort Then
    MainForm_ResortTypes xUsedTypes
    xUsedTypesResort = False
    End If
  PrettyFileTypes = Replace(Mid(xUsedTypes, 2&, Len(xUsedTypes) - 2&), vbNullChar, ",")
  End Function

Public Sub MainForm_ResortTypes(ByRef TypesString As String)
  Dim xCharA1(99&) As String
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  TypesString = Mid(TypesString, 2&)
  Do
    xLong2 = InStr(TypesString, vbNullChar)
    If xLong2 = 0& Or xLong1 = 100& Then
      Exit Do
      Else
      xCharA1(xLong1) = Left(TypesString, xLong2 - 1&)
      xLong1 = xLong1 + 1&
      TypesString = Mid(TypesString, xLong2 + 1&)
      End If
    Loop
  Call ArraySort_String_IC(xCharA1(), xLong1)
  TypesString = ""
  For xLong2 = 0& To xLong1 - 1&
    TypesString = TypesString & vbNullChar & xCharA1(xLong2)
    Next
  TypesString = TypesString & vbNullChar
  End Sub

'ReadDir
'      TempMusic = Asc(Left(PLItem(PLCurItem), 1&)) - 1&
'      xFullMode = AllowedItemZ(Mid(PLItem(PLCurItem), 3&))
'
' alloweditem[x] -> supported or enabled?

Private Function ClearOverload()
  If Not (PLCount > PLCountWarn) And (xWarnOverload Or xOldWarnOverload) Then
    #If NoDroppingIfOverloaded Then
      If xWarnOverload Then
        Call RegisterDragDrop(MainHWnd, ObjPtr(DragClass))
        xWarnOverload = False
        End If
      #Else
      xWarnOverload = False
      #End If
    Call MainForm_EnableApiMenu(10&, True)
    Call MainForm_EnableApiMenu(11&, True)
    Call MainForm_EnableApiMenu(12&, True)
    xOldWarnOverload = False
    'If xOldIconState < 10& Then
    '  DoAnimateSetIcon -1&
    '  End If
    'UpdateAnimIcon
    MainForm_UpdateMainIcon
    End If
  End Function

Public Sub MainForm_HideIRWin()
  If ShowIRWin Then
    Call ShowWindow(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&)
    xCalaymClassAlign = -1&
    Call KillTimer(xStaticIconsHWnd(xStaticIcons_ShowIR), 3&)
    ShowIRWin = False
    End If
  End Sub


Public Sub SetPanelSize()
  Dim xLong1 As Single
  
  If vKioskMode Then
    If MP3GetID3Tag Then
      If xShowSpectrum Then
        hwndSpectrumTop = PanelSizeDefKiosk + (hWndTagsHeight - hwndSpectrumHeight) \ 2
        End If
      xLong1 = PanelSizeDefKiosk + hWndTagsHeight '+ eSpTop
      ePic0Height = 0&
      ElseIf xShowSpectrum Then
      hwndSpectrumTop = PanelSizeDefKiosk
      xLong1 = PanelSizeDefKiosk + hwndSpectrumHeight '+ eSpTop
      Else
      xLong1 = PanelSizeDefKiosk - eSPTop
      End If
    Else
    If MP3GetID3Tag Then
      xLong1 = PanelSizeDefTags
      Else
      xLong1 = PanelSizeDef
      End If
    End If
  
  If Not xLong1 = ePic0Height Then
    ePic0Height = xLong1
    MainForm_ResizePlayer True
    Else
    Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Spectrum), 0&, hwndSpectrumLeft, hwndSpectrumTop, hwndSpectrumWidth, hwndSpectrumHeight, &H215&)
    Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Tags), 0&, 0&, hWndTagsTop, hWndTagsWidth, hWndTagsHeight, &H214&)
    End If
  End Sub

Public Sub MainForm_RestartEx()
  Tear_Volume(CurrentSongID) = 1
  Call ctSSLayer_SetPause(SongPointer(CurrentSongID), 1&)
  Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), 0&)
  Call ctSSLayer_SetVolume(SongPointer(CurrentSongID), xSetVolume)
  Tear_Volume(CurrentSongID) = 1&
  Call ctSSLayer_SetPause(SongPointer(CurrentSongID), 0&)
  If CurMusic = ctSSLayer_MusicTypeTracker Then
    ResetPatternLog
    End If
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Sound restarted"
    #End If
  End Sub

Public Sub CheckApiMenu(ByVal MenuID As Long, ByVal Checked As Boolean)
  Static xLong1 As Long
  
  If Checked Then
    For xLong1 = 0& To xMenuCountX
      Call CheckMenuItem(xMenu(xLong1), 1000& + MenuID, 8&)
      Call CheckMenuItem(xMenu(xLong1), 2000& + MenuID, 8&)
      Next
    Else
    For xLong1 = 0& To xMenuCountX
      Call CheckMenuItem(xMenu(xLong1), 1000& + MenuID, 0&)
      Call CheckMenuItem(xMenu(xLong1), 2000& + MenuID, 0&)
      Next
    End If
  End Sub

Public Sub RadioApiMenu(ByVal MenuIDFirst As Long, ByVal MenuIDLast As Long, ByVal MenuIDChecked As Long)
  Dim xLong1 As Long
  For xLong1 = 0& To xMenuCountX
    Call CheckMenuRadioItem(xMenu(xLong1), 1000& + MenuIDFirst, 1000& + MenuIDLast, 1000& + MenuIDChecked, 0&)
    Call CheckMenuRadioItem(xMenu(xLong1), 2000& + MenuIDFirst, 2000& + MenuIDLast, 2000& + MenuIDChecked, 0&)
    Next
  End Sub

Public Sub MainForm_EnableApiMenu(ByVal MenuID As Long, ByVal IsEnabled As Boolean)
  Dim xLong1 As Long
  
  If IsEnabled Then
    For xLong1 = 0& To xMenuCountX
      Call EnableMenuItem(xMenu(xLong1), 1000& + MenuID, 0&)
      Call EnableMenuItem(xMenu(xLong1), 2000& + MenuID, 0&)
      Next
    Else
    For xLong1 = 0& To xMenuCountX
      Call EnableMenuItem(xMenu(xLong1), 1000& + MenuID, 3&)
      Call EnableMenuItem(xMenu(xLong1), 2000& + MenuID, 3&)
      Next
    End If
  End Sub

Private Sub UpdateMenuCaption(ByVal MenuID As Long, ByVal Kbd_Def As Long, ByVal Kbd_Gbl As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Static xLong3 As Long
  'Static xChar1 As String
  'Static xChar2 As String
  
  xLong2 = -1&
  'If Kbd_Def = -1& Then Kbd_Def = 0& Else Kbd_Def = xKey(Kbd_Def)
  If Not Kbd_Def = -1& Then If xKey(Kbd_Def) = 0& Then Kbd_Def = -1&
  If Not Kbd_Gbl = -1& Then If xKey(Kbd_Gbl) = 0& Then Kbd_Gbl = -1&
  
  For xLong1 = 0& To xMenuCountX
    If Kbd_Def = -1& Then
      SetMenuShortcut xMenu(xLong1), 1000& + MenuID, ""
      Else
      SetMenuShortcut xMenu(xLong1), 1000& + MenuID, ExtKbdEx(Kbd_Def)
      End If
    If Kbd_Gbl = -1& Then
      SetMenuShortcut xMenu(xLong1), 2000& + MenuID, ""
      Else
      SetMenuShortcut xMenu(xLong1), 2000& + MenuID, ExtKbdEx(Kbd_Gbl)
      End If
    Next
  End Sub

Private Sub ShowPlayer(ByVal AndFocus As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  'If Not xTitlesWhenMinimized = 2& Then MainForm_HideIRWin: Debug.Print "!!!!!Hidden2"
  xLong1 = GetLastActivePopup(MainHWnd)
  If IsIconic(MainHWnd) Then
    If xLong1 = 0& Or xLong1 = MainHWnd Then
      If AndFocus Then
        Call ShowWindow(MainHWnd, 9&)
        Call SetForegroundWindow(MainHWnd)
        Else
        xLong2 = GetForegroundWindow
        Call ShowWindow(MainHWnd, 9&)
        Call SetForegroundWindow(xLong2)
        End If
      Else
      If AndFocus Then
        Call ShowWindow(MainHWnd, 9&)
        Call ShowWindow(xLong1, 9&)
        Call SetForegroundWindow(xLong1)
        Call SetWindowPos(MainHWnd, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(MainHWnd, -2&, 0&, 0&, 0&, 0&, &H213&)
        Else
        xLong2 = GetForegroundWindow
        Call ShowWindow(MainHWnd, 9&)
        Call ShowWindow(xLong1, 9&)
        Call SetForegroundWindow(xLong2)
        Call SetWindowPos(MainHWnd, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(MainHWnd, -2&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(xLong1, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(xLong1, -2&, 0&, 0&, 0&, 0&, &H213&)
        End If
      End If
    Else
    If xLong1 = 0& Then
      Call ShowWindow(MainHWnd, 5&)
      If AndFocus Then
        Call SetForegroundWindow(MainHWnd)
        Else
        Call SetWindowPos(MainHWnd, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(MainHWnd, -2&, 0&, 0&, 0&, 0&, &H213&)
        End If
      Else
      Call ShowWindow(MainHWnd, 5&)
      Call ShowWindow(xLong1, 5&)
      If AndFocus Then
        Call SetForegroundWindow(xLong1)
        Else
        Call SetWindowPos(MainHWnd, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(MainHWnd, -2&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(xLong1, -1&, 0&, 0&, 0&, 0&, &H213&)
        Call SetWindowPos(xLong1, -2&, 0&, 0&, 0&, 0&, &H213&)
        End If
      End If
    End If
  xTrayIconIndicator = False
  MainForm_ShowTray False
  If SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 2 Then
    xLong1 = xFindSong
    If Not xLong1 = 0 Then
      xLong2 = xFollowCurSongLastDiff
      xMarkSong xLong1, False, False
      If xLong2 < xFollowCurSongLastDiffDef Then
        xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
        Else
        xFollowCurSongLastDiff = xLong2
        End If
      End If
    End If
  End Sub

Private Sub RemoveCurrentSongFromList()
  Dim xLong1 As Long
  
  If Not vKioskMode Then
    xLong1 = xFindSong
    If Not xLong1 = 0 Then
      xMarkSong xLong1, False, False
      xRemoveItems True
      End If
    End If
  End Sub

'Private Sub UnloadApiMenu(ByVal MenuID As Long)
'  Static xLong1 As Long
'
'  For xLong1 = 0& To xMenuCountX
'    'Do While RemoveMenu(xMenu(xLong1), 1000& + MenuID, 0&): Loop
'    'Do While RemoveMenu(xMenu(xLong1), 2000& + MenuID, 0&): Loop
'    Call RemoveMenu(xMenu(xLong1), 1000& + MenuID, 0&)
'    Call RemoveMenu(xMenu(xLong1), 2000& + MenuID, 0&)
'    Next
'  End Sub

Public Sub MainForm_MenuEvent(ByVal MenuID As Long)
  'Debug.Print "  Undefined!!!: ", MenuID
  
  If MenuID > 999& And MenuID < 3000& Then
    MenuID = MenuID Mod 1000&
    
    Select Case MenuID
      'quit
      'case 1&
        Case 10&: MainForm_ProcessKey KbdC_FAddFileS
        Case 11&: MainForm_ProcessKey KbdC_FAddDir
        Case 12&: MainForm_ProcessKey KbdC_FAddPList
        'Case 20&
        Case 20&, 22&: MainForm_ProcessKey KbdC_FRefreshCD
          'Case 870& To 896&
        Case 30&: MainForm_ProcessKey KbdC_FNewPL
        Case 31&: MainForm_ProcessKey KbdC_FLoadPL
        'Case 32&
        Case 34&: MainForm_ProcessKey KbdC_FReloadPL
        Case 36&: MainForm_ProcessKey KbdC_FLoadPLDefault
          ''''''
        Case 40&: MainForm_ProcessKey KbdC_FSavePL
        Case 41&: MainForm_ProcessKey KbdC_FSavePLas
        Case 50&: MainForm_ProcessKey KbdC_FClearPL
        Case 60&: MainForm_ProcessKey KbdC_FQuitSD
      
      'defctrl
      'Case 100&
        Case 101&: MainForm_ProcessKey KbdC_CPrev
        Case 102&: MainForm_ProcessKey KbdC_CPlay
        Case 103&: MainForm_ProcessKey KbdC_CPause
        Case 104&: MainForm_ProcessKey KbdC_CStop
        Case 105&: MainForm_ProcessKey KbdC_CNext
        Case 106&: MainForm_ProcessKey KbdC_CEnd
        'repeat
        Case 110&: MainForm_ProcessKey KbdC_CRepSingle
        Case 111&: MainForm_ProcessKey KbdC_CRepAll
        Case 112&: MainForm_ProcessKey KbdC_CRepRnd
        
        Case 113&: MainForm_ProcessKey KbdC_CRepToggle
        Case 119&: MainForm_ProcessKey KbdC_CIntroMode
        
        Case 120&: MainForm_ProcessKey KbdC_CSeekB
        Case 121&: MainForm_ProcessKey KbdC_CSeekF
        
        'Case 130&
          Case 131&: MainForm_ProcessKey KbdC_CCSSelFile
          Case 132&: MainForm_ProcessKey KbdC_CCSRemFile
          Case 133&: MainForm_ProcessKey KbdC_CCSDelFile
          Case 134&: MainForm_ProcessKey KbdC_CCSExplorer
      
      'Case 200&
        Case 201&: MainForm_ProcessKey KbdC_PPlaySkip
        Case 202&: MainForm_ProcessKey KbdC_PPlayEnd
        Case 203&: MainForm_ProcessKey KbdC_PPlayNext
        Case 210&: MainForm_ProcessKey KbdC_PFindCurrent
        Case 211&: MainForm_ProcessKey KbdC_PFind
        Case 212&: MainForm_ProcessKey KbdC_PFindNext
        Case 213&: MainForm_ProcessKey KbdC_PFindQuick
        Case 214&: MainForm_ProcessKey KbdC_PSelRnd
        Case 220&: MainForm_ProcessKey KbdC_PInvSel
        Case 221&: MainForm_ProcessKey KbdC_PRemSongs
        Case 222&: MainForm_ProcessKey KbdC_PRem
        Case 223&: MainForm_ProcessKey KbdC_PCopyPLEnd
        Case 224&: MainForm_ProcessKey KbdC_PCrop
        Case 230&: MainForm_ProcessKey KbdC_PDelFiles
        Case 250&: MainForm_ProcessKey KbdC_PExplorer
      
      'Case 300&
        Case 301&: MainForm_ProcessKey KbdC_OFading
        Case 302&: MainForm_ProcessKey KbdC_OSpektrum
        Case 303&: MainForm_ProcessKey KbdC_OFileInfos
        Case 310&: MainForm_ProcessKey KbdC_OFollowSong
        Case 320&: MainForm_ProcessKey KbdC_ORemMissing
        Case 321&: MainForm_ProcessKey KbdC_ORemSkipped
        Case 322&: MainForm_ProcessKey KbdC_ORemPlayed
        Case 330&: MainForm_ProcessKey KbdC_ONoEndMode
        Case 331&: MainForm_ProcessKey KbdC_OXFade
        Case 332&: MainForm_ProcessKey KbdC_OMDMode
        Case 333&: MainForm_ProcessKey KbdC_OStopEach
        Case 339&: MainForm_ProcessKey KbdC_OEndModeTggle
        ' Case 340& Volume menu
          Case 341&: ChangeVolumeMenu 0& 'mainform_ProcessKey KbdC_CVolUp
          Case 342&: ChangeVolumeMenu 1& ' mainform_ProcessKey KbdC_CVolDown
          Case 345& To 355&: ChangeVolumeMenu MenuID - 342&  ' ChangeVolumeMenu 3& + MenuID - 345& 'mainform_ProcessKey KbdC_CVolMute + MenuID - 345&
        Case 360&: MainForm_ProcessKey KbdC_CMoreSetts
      
      'Case 400&
      '  Case 405 = IRadioParent
        Case 406&: MainForm_ProcessKey KbdC_OInternetRadio
        Case 407&: MainForm_ProcessKey KbdC_OInternetRadioRe
        Case 408&: MainForm_ProcessKey KbdC_OInternetRadioHP
      'case 410 = AutoRFParent (unused in resources, but disabled with /NoAutoRefresh anyway)
        Case 411&: MainForm_ProcessKey KbdC_OAutoRF
        Case 412&: MainForm_ProcessKey KbdC_OAutoRFAlt
        Case 413&: MainForm_ProcessKey KbdC_OAutoRFRem
        Case 414&: MainForm_ProcessKey KbdC_OAutoRFRemI
        Case 415&: MainForm_ProcessKey KbdC_OAutoRFSetup
        Case 419&  ' = abortreading (unused)
          If Not vKioskMode Then AbortReading = True
        Case 420&
                   If xDirList < 0& Then
                     ShowWindow xListHWnd(0), 5&
                     DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 1&
                     SetFocusAPI xListHWnd(0)
                     End If

        'Case 425&: mainform_ProcessKey KbdC_
          Case 426&: MainForm_ProcessKey KbdC_ODirToggle
          Case 427&: MainForm_ProcessKey KbdC_ODirInc
          Case 428&: MainForm_ProcessKey KbdC_ODirDec
        Case 421&
                  ShowWindow xListHWnd(1), 5&
                  DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 1&
                  SetFocusAPI xListHWnd(1)
        'Case 440&: mainform_ProcessKey KbdC_
          Case 441&: MainForm_ProcessKey KbdC_OTimerEdit
          Case 442&: MainForm_ProcessKey KbdC_OTimerImport
          Case 443&: MainForm_ProcessKey KbdC_OTimerExport
          Case 444&: MainForm_ProcessKey KbdC_OTimerClear
        Case 450&: MainForm_ProcessKey KbdC_OResetSnd
        Case 455&: MainForm_ProcessKey KbdC_OVolCtrl
        'Case 460&: mainform_ProcessKey KbdC_
          Case 461&: MainForm_ProcessKey KbdC_OPresetSave
          Case 462&: MainForm_ProcessKey KbdC_OPresetLoad
            'case 464...? preset by ID
        Case 470&: MainForm_ProcessKey KbdC_OSaveSetts
      
      'Case 500&: mainform_ProcessKey KbdC_IInfoMenu
        Case 501&: MainForm_ProcessKey KbdC_IHelp
        Case 502&: MainForm_ProcessKey KbdC_IAbout
        
      'showplayer
      Case 800&: MainForm_ProcessKey KbdC_GShowPlayer
      ' 900+ => specialmenus / popups
      ' 970++ = CD drive IDs
      Case 970& To 995&
        'Debug.Print Chr(MenuID - 970& + 65&)
        MenuEventCDDrive Mid(CDDriveLetters, MenuID - 969&, 1&), True, True
'      Case Else
'        Debug.Print "  Undefined!!!: ", MenuID
      End Select
    ElseIf MenuID > 2999& And MenuID < 3000& + vPLXMax Then
    'Debug.Print "m3u: "; MenuID, vPLXMain & vPLX(MenuID - 3001&)
    If Not xNoPLMenu Then
      Call LoadPList(vPLXMain + vPLX(MenuID - 3000&), False, False) ', False)
      End If
    ElseIf MenuID > 3999& And MenuID < 4000& + vPLXMax Then
    'Debug.Print "cps: "; MenuID, vPLXMain & vPLX(vPLXMax+ MenuID - 4001&)
    If Not xNoPresets Then
      MainForm_LoadSettings vPLXMain + vPLX(vPLXMax + MenuID - 4000&)
      End If
    ElseIf MenuID > 4999& And MenuID < 5000& + vPLXMax Then
    'Debug.Print "cps: "; MenuID, vPLXMain & vPLX(2& * vPLXMax+ MenuID - 4001&)
    If Not xNoTimers Then
      MainForm_LoadTimers MainHWnd, vPLXMain + vPLX(2& * vPLXMax + MenuID - 5000&), False
      End If
    End If
  End Sub

Private Sub MainForm_EndSongCallback(ByRef ptrSong As Long)
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Callback event occurred (pointer: " + CStr(ptrSong) + ")"
    #End If
  If ptrSong = SongPointer(CurrentSongID) And Not (CurMusic = ctSSLayer_MusicTypeNone Or PlayerIsLocked Or xAutoDoing) Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Callback valid, processing 'CheckAfter' procedure"
      #End If
    MainForm_CheckAfter False, False, False, False
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Callback done"
      #End If
    End If
  End Sub

Public Sub GenCalaymStructure(ByVal NoApply As Boolean, ByVal ArraySize As Long, ByVal SortNames As Boolean)
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xLong5 As Long
  Dim xLong6 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  

'DoEventsEx_Sleep 5000&

  
  If Not ArraySize = 0& Then
    If SortNames Then '!!!!! required to prevent wasting of dirs
      Call ArraySort_String_IC(sCharA1(), ArraySize)
      End If
    xLong6 = PLCount
    
    xChar1 = SimpleDir(sCharA1(0))
    xChar2 = LCase(xChar1) & "\"
    xLong3 = Len(xChar2)
    
    For xLong2 = 1& To ArraySize - 1&
      If Not LCase(Left(sCharA1(xLong2), xLong3)) = xChar2 Then
        Do While (Not (LCase(Left(sCharA1(xLong2), xLong3)) = xChar2 Or xChar1 = ""))
          xLong3 = InStrRev(xChar1, "\")
          If xLong3 = 0& Then
            xChar1 = ""
            Exit Do
            Else
            xChar1 = Left(xChar1, xLong3 - 1&)
            If xChar1 = "\" Or xChar1 = "\\" Then
              xChar1 = ""
              Exit Do
              Else
              xChar2 = LCase(xChar1) & "\"
              xLong3 = Len(xChar2)
              End If
            End If
          Loop
        End If
      Next
    
    If SortNames Then
      For xLong2 = 0& To ArraySize - 2&
        If StrComp(sCharA1(xLong2), sCharA1(xLong2 + 1&), vbTextCompare) = 0& Then
          sCharA1(xLong2) = ""
          End If
        Next
      End If
    
    If xChar1 = "" Then
      xChar2 = ""
      xLong3 = 0&
      Else
      #If BoldDirs = 98 Then
        PLI(PLCount).Filename = xChar1
        PLI(PLCount).Caption = xChar1
        PLI(PLCount).DirDepthFlags = PLI_IsDir Or xLong4
        xLong4 = 1&
        #Else
        PLIFile(PLCount) = xChar1
        PLICap(PLCount) = xChar1
        PLIDF(PLCount) = PLI_IsDir Or xLong4
        xLong4 = 1&
        #End If
      PLCount = PLCount + 1&
      If PLCount = PLCountMax Then GoTo lbl1
      
      For xLong5 = 0& To ArraySize - 1&
        If Not Len(sCharA1(xLong5)) = 0& Then
          xChar3 = LCase(SimpleDir(sCharA1(xLong5))) & "\"
          If xChar3 = xChar2 Then
            #If BoldDirs = 98 Then
              PLI(PLCount).Filename = sCharA1(xLong5)
              PLI(PLCount).Caption = SimpleFile(sCharA1(xLong5))
              PLI(PLCount).DirDepthFlags = xLong4 '+ 1&
              #Else
              PLIFile(PLCount) = sCharA1(xLong5)
              PLICap(PLCount) = SimpleFile(sCharA1(xLong5))
              PLIDF(PLCount) = xLong4 '+ 1&
              #End If
            PLCount = PLCount + 1&
            sCharA1(xLong5) = ""
            ElseIf Not Left(xChar3, xLong3) = xChar2 Then
            Exit For
            End If
          End If
        Next
      End If
    
    For xLong2 = 0& To ArraySize - 1&
      If Not Len(sCharA1(xLong2)) = 0& Then
        xChar1 = LCase(SimpleDir(sCharA1(xLong2))) & "\"
        If Not xChar1 = xChar2 Then
          'is more parent?
          If Not Left(xChar1, xLong3) = xChar2 Then
            Do While (Not Left(xChar1, xLong3) = xChar2)
              xLong4 = xLong4 - 1&
              xLong3 = InStrRev(xChar2, "\", xLong3 - 1&)
              If xLong3 = 0& Then
                xChar2 = ""
                Exit Do
                Else
                xChar2 = Left(xChar2, xLong3)
                If xChar2 = "\\" Then xChar2 = "": xLong3 = 0&
                End If
              Loop
            End If
          If Not xChar1 = xChar2 Then
            'is a subdir of current!
            If Left(xChar1, xLong3) = xChar2 Then
              xChar1 = Mid(sCharA1(xLong2), xLong3 + 1&) ' & "\" 'last "\" is filename seperator
              'If Not xLong3 = 0& Then xLong3 = xLong3 - 1&
              Do
                xLong5 = InStr(xChar1, "\")
                If xLong5 = 1& Then
                  xLong5 = InStr(3&, xChar1, "\")
                  End If
                If xLong5 = 0& Then
                  Exit Do
                  Else
                  #If BoldDirs = 98 Then
                    PLI(PLCount).Caption = Left(xChar1, xLong5 - 1&)
                    PLI(PLCount).DirDepthFlags = PLI_IsDir Or xLong4
                    xLong4 = xLong4 + 1&
                    #Else
                    PLICap(PLCount) = Left(xChar1, xLong5 - 1&)
                    PLIDF(PLCount) = PLI_IsDir Or xLong4
                    xLong4 = xLong4 + 1&
                    #End If
                  xLong3 = xLong3 + xLong5
                  #If BoldDirs = 98 Then
                    PLI(PLCount).Filename = Left(sCharA1(xLong2), xLong3 - 1&)
                    #Else
                    PLIFile(PLCount) = Left(sCharA1(xLong2), xLong3 - 1&)
                    #End If
                  xChar1 = Mid(xChar1, xLong5 + 1&)
                  PLCount = PLCount + 1&
                  If PLCount = PLCountMax Then GoTo lbl1
                  xChar2 = LCase(Left(sCharA1(xLong2), xLong3 - 1&)) & "\"
                  
                  For xLong5 = xLong2 To ArraySize - 1&
                    If Not sCharA1(xLong5) = "" Then
                      xChar3 = LCase(SimpleDir(sCharA1(xLong5))) & "\"
                      If xChar3 = xChar2 Then
                        #If BoldDirs = 98 Then
                          PLI(PLCount).Filename = sCharA1(xLong5)
                          PLI(PLCount).Caption = SimpleFile(sCharA1(xLong5))
                          PLI(PLCount).DirDepthFlags = xLong4 ' + 1&
                          #Else
                          PLIFile(PLCount) = sCharA1(xLong5)
                          PLICap(PLCount) = SimpleFile(sCharA1(xLong5))
                          PLIDF(PLCount) = xLong4 ' + 1&
                          #End If
                        PLCount = PLCount + 1&
                        If Not xLong5 = xLong2 Then sCharA1(xLong5) = ""
                        ElseIf Not Left(xChar3, xLong3) = xChar2 Then
                        Exit For
                        End If
                      End If
                    Next
                  
                  
                  End If
                Loop
              End If
            End If
          xChar2 = LCase(SimpleDir(sCharA1(xLong2))) & "\"
          xLong3 = Len(xChar2)
          End If
        
        End If
      Next
    
lbl1:
    If PLCount = PLCountMax Then
      Do While (Not PLCount = 0&)
#If BoldDirs = 98 Then
        If (PLI(PLCount - 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
        If (PLIDF(PLCount - 1&) And PLI_IsDir) = PLI_IsDir Then
#End If
          PLCount = PLCount - 1&
          Else
          Exit Do
          End If
        Loop
      End If
    
    If Not NoApply Then
      RealPLAdd xLong6
      End If
    End If
  'Erase sCharA1()
  ReDim sCharA1(sCharA1MaxX)
  End Sub

Public Sub MainForm_XMenuRefresher() '(ByVal TimerEditorOnly As Boolean)
  Dim xMenuIDX As Long
  Dim xMenuHwnd As Long
  Dim hFindFile As Long
  Dim xType1 As WIN32_FIND_DATA
  Dim LeadMenuID As Long
  Dim InsertMenuID As Long
  Static xItemCount(2) As Long
  Dim IsPl As Long
  Dim xCount As Long
  Dim xLong1 As Long
  'Dim WantsUnicode As Boolean
  
  For IsPl = 0& To 2&
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Detecting files / updating menu (" & Hex(IsPl) & ")"
      #End If
    
    If IsPl = 0& Then
      If Not xNoPLMenu Then
' playlistformatsimport
        hFindFile = FindFirstFile(vPLXMain & "*.*", xType1, &H2F&)
        LeadMenuID = 1033&
        InsertMenuID = 3000&
        End If
      ElseIf IsPl = 1& Then
      If Not xNoPresets Then
        hFindFile = FindFirstFile(vPLXMain & "*.cps", xType1, &H2F&)
        LeadMenuID = 1463&
        InsertMenuID = 4000&
        End If
      Else
      If Not xNoTimers Then
        hFindFile = FindFirstFile(vPLXMain & "*.cpt", xType1, &H2F&)
        LeadMenuID = 1445&
        InsertMenuID = 5000&
        End If
      End If
    xCount = 0&
    If Not hFindFile = -1& Then
      If IsPl = 0& Then
        Do
          If AllowedItemPLFast(xType1.Filename) Then
            If Not xType1.dwFileAttributes = -1& And (xType1.dwFileAttributes And 16&) = 0& Then
              vPLX(IsPl * vPLXMax + xCount) = xType1.Filename
              xCount = xCount + 1&
              End If
            End If
          If xCount = vPLXMax Then Exit Do
          Loop Until FindNextFile(hFindFile, xType1) = 0&
        Else
        Do
          If Not xType1.dwFileAttributes = -1& And (xType1.dwFileAttributes And 16&) = 0& Then
            vPLX(IsPl * vPLXMax + xCount) = xType1.Filename
            xCount = xCount + 1&
            End If
          If xCount = vPLXMax Then Exit Do
          Loop Until FindNextFile(hFindFile, xType1) = 0&
        End If
      Call FindClose(hFindFile)
      Call ArraySort_String_StartPos_IC(vPLX(), xCount, IsPl * vPLXMax)
      For xMenuIDX = 0& To xMenuCountX
        xMenuHwnd = xMenu(xMenuIDX)
        GoSub ProcessMenu
        Next
      If xTimerEditing Then
        xMenuHwnd = xTimerPLMenu(IsPl)
        LeadMenuID = 2750& + IsPl * 10&
        InsertMenuID = 3000& + IsPl * 100&
        xItemCount(IsPl) = xTimerPLMenuLoaded(IsPl)
        xTimerPLMenuLoaded(IsPl) = xCount
        GoSub ProcessMenu
        End If
      hFindFile = -1&
      End If
    xItemCount(IsPl) = xCount
    Next
  Exit Sub

ProcessMenu:
  If Not (GetMenuState(xMenuHwnd, LeadMenuID, 0&) = -1& And GetMenuState(xMenuHwnd, InsertMenuID, 0&) = -1&) Then
    If xCount <= xItemCount(IsPl) Then
      If xCount = 0& And Not xItemCount(IsPl) = 0& Then
        LoadResEx_Menu_Add xMenuHwnd, LeadMenuID, LoadResEx_String(1050&), InsertMenuID
        End If
      For xLong1 = InsertMenuID + xItemCount(IsPl) - 1& To InsertMenuID + xCount Step -1&
        Call DeleteMenu(xMenuHwnd, xLong1, 0&)
        Next
      For xLong1 = 0& To xCount - 1&
        LoadResEx_Menu_SetCaption xMenuHwnd, InsertMenuID + xLong1, vPLX(IsPl * vPLXMax + xLong1)
        Next
      Else
      If Not xItemCount(IsPl) = 0& Then
        LoadResEx_Menu_Add xMenuHwnd, LeadMenuID, LoadResEx_String(1050&), InsertMenuID
        For xLong1 = InsertMenuID + xItemCount(IsPl) - 1& To InsertMenuID Step -1&
          Call DeleteMenu(xMenuHwnd, xLong1, 0&)
          Next
        End If
      If Not xCount = 0& Then
        For xLong1 = 0& To xCount - 1&
          LoadResEx_Menu_Add xMenuHwnd, InsertMenuID + xLong1, vPLX(IsPl * vPLXMax + xLong1), LeadMenuID
          Next
        Call DeleteMenu(xMenuHwnd, LeadMenuID, 0&)
        End If
      End If
    End If
  Return
  End Sub

Public Sub MainForm_XMenuRefresherDevices()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim LeadMenuID As Long
  Dim InsertMenuID As Long
  Static xDevicesCount As Long
  Dim xBool1 As Boolean
  Dim xBool2 As Boolean
  
  'vKioskMode = True
  'CommandX = "/CD"
  xBool1 = Not (InStr(CommandX, "/DD") = 0& And vKioskMode)
  xBool2 = xBool1 Or Not (InStr(CommandX, "/CD") = 0&)
  'vKioskMode = False
  
  If xBool2 Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Detecting CD drives"
      #End If
    xLong1 = GetLogicalDrives()
    CDDriveLetters = ""
    
    For xLong2 = FirstNonFloppy To 25&
      If (xLong1 And (2& ^ xLong2)) Then
        Select Case GetDriveTypeA(Chr(65& + xLong2) & ":")
          Case 2&
            If xBool1 Then
              CDDriveLetters = CDDriveLetters & Chr(xLong2 + 65&)
              End If
          Case 5&
            CDDriveLetters = CDDriveLetters & Chr(xLong2 + 65&)
          End Select
        End If
      Next
    #If UseFileLogging Then
      Else
      MainForm_ctConsoleA_AddLog "Detecting CD drives ignored (kiosk mode)"
      #End If
    End If
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  Detected: " & CDDriveLetters
    #End If
  xLong1 = Len(CDDriveLetters)
  LeadMenuID = 1023&
  InsertMenuID = 1970&
  For xLong2 = 0& To xMenuCountX
    xLong3 = xMenu(xLong2)
    GoSub ProcessMenu
    Next
      
  'If xTimerEditing Then
  '  xLong3 = xTimerPLMenu(IsPl)
  '  LeadMenuID = 2750& + IsPl * 10&
  '  InsertMenuID = 3000& + IsPl * 100&
  '  GoSub ProcessMenu
  '  End If
  
  xDevicesCount = xLong1
  MainForm_EnableApiMenu 21&, (Not (xDevicesCount = 0&))
  MainForm_EnableApiMenu 22&, (Not (xDevicesCount = 0&))
  Exit Sub

ProcessMenu:
  If Not (GetMenuState(xLong3, LeadMenuID, 0&) = -1& And GetMenuState(xLong3, InsertMenuID, 0&) = -1&) Then
    If xLong1 <= xDevicesCount Then
      If xLong1 = 0& And Not xDevicesCount = 0& Then
        LoadResEx_Menu_Add xLong3, LeadMenuID, LoadResEx_String(1052&), InsertMenuID
        End If
      For xLong4 = InsertMenuID + xDevicesCount - 1& To InsertMenuID + xLong1 Step -1&
        Call DeleteMenu(xLong3, xLong4, 0&)
        Next
      For xLong4 = 0& To xLong1 - 1&
        LoadResEx_Menu_SetCaption xLong3, InsertMenuID + xLong4, Replace(LoadResEx_String(1051&), "$", Mid(CDDriveLetters, xLong4 + 1&, 1&))
        Next
      Else
      If Not xDevicesCount = 0& Then
        LoadResEx_Menu_Add xLong3, LeadMenuID, LoadResEx_String(1052&), InsertMenuID
        For xLong4 = InsertMenuID + xDevicesCount - 1& To InsertMenuID Step -1&
          Call DeleteMenu(xLong3, xLong4, 0&)
          Next
        End If
      If Not xLong1 = 0& Then
        For xLong4 = 0& To xLong1 - 1&
          LoadResEx_Menu_Add xLong3, InsertMenuID + xLong4, Replace(LoadResEx_String(1051&), "$", Mid(CDDriveLetters, xLong4 + 1&, 1&)), LeadMenuID
          Next
        Call DeleteMenu(xLong3, LeadMenuID, 0&)
        End If
      End If
    End If
  Return
  End Sub

Private Sub MainForm_SetAutoDirs(ByVal SetThem As Boolean)
  Dim xLong1 As Long

  If SetThem Then
    If xAutoRefreshTimer(1) > 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Adding autorefresh monitor items"
        #End If
      For xLong1 = 0& To xAutoDirCount - 1&
        xAutoDirHWnd(xLong1) = FindFirstChangeNotification(xAutoDir(xLong1), 1&, 3&)
        ' + FILE_NOTIFY_CHANGE_DIR_NAME
        Next
      xAutoDirHWndCount = xAutoDirCount
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Added " & CStr(xAutoDirHWndCount) & " autorefresh monitor items"
        #End If
      End If
    Else
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Removing autorefresh monitor items"
      #End If
    For xLong1 = 0& To xAutoDirHWndCount - 1&
      If Not xAutoDirHWnd(xLong1) = 0& Then
        Call FindCloseChangeNotification(xAutoDirHWnd(xLong1))
        End If
      Next
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Removed " & CStr(xAutoDirHWndCount) & " autorefresh monitor items"
      #End If
    xAutoDirHWndCount = 0&
    End If
  End Sub

Private Sub CheckAutoDirs()
  'Dim xBoolA1(xAutoDirCountMaxX) As Boolean
  Dim xLong1 As Long
  Dim xBool1 As Boolean

  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "Checking " & CStr(xAutoDirHWndCount) & " autorefresh monitor items"
    #End If

  For xLong1 = 0& To xAutoDirHWndCount - 1&
    If Not xAutoDirHWnd(xLong1) = 0& Then
      If WaitForSingleObject(xAutoDirHWnd(xLong1), 0&) = 0& Then
        Call FindNextChangeNotification(xAutoDirHWnd(xLong1))
        xBool1 = True
        End If
      End If
    Next
  
  If xBool1 Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "  done, changed"
      #End If
    MainForm_xAutoRefreshNow False, True, False, False, True
    #If UseFileLogging Then
    Else
      MainForm_ctConsoleA_AddLog "  done, unchanged"
      #End If
    End If
  'ReAutoPlayDR = False
  'If Not xAutoDoing Or ForceNow Then
  '  LockMe True
  End Sub

Private Sub GetMyWindowPos(ByRef xChar1 As String, ByRef FirstValue As Long)
  Dim xLong1 As Long
  Dim xLongA1(3) As Long
  
  If Not Right(xChar1, 3&) = ",-1" Then
    For xLong1 = 0& To 3&
    'If Not FirstValue < 0& Then
      xLongA1(xLong1) = FirstValue
      FirstValue = InStr(xChar1, ",") + 1&
      If FirstValue = 1& Then
        Exit For
        Else
        xChar1 = Mid(xChar1, FirstValue)
        FirstValue = Val(xChar1)
        End If
      Next
    Call RtlMoveMemory(xMWinPos, ByVal VarPtr(xLongA1(0)), 16&)
    End If
  End Sub

Private Sub ResetPatternLog()
  Dim xLong1 As Long
  
  xModCurPattern = -1&
  If CurMusic = ctSSLayer_MusicTypeTracker And (ctSSLayer_CanUseItem(6&) And &H1&) = &H1& Then
    If (ctSSLayer_CanUseItem(6&) And &H4&) = &H4& Then
      xLong1 = ctSSLayer_MusicGetPatterns(SongPointer(CurrentSongID))
      Else
      xLong1 = PLCurSongLength
      End If
    If xLong1 = 0& Then
      xModPatterns = vbNullChar ' always need at least 1 bit
      Else
      'xModPatterns = String((xLong1 + 7&) \ 8&, vbNullChar)
      xModPatterns = String((xLong1 + 9&) \ 8&, vbNullChar) ' reserve 2 more bits for +0/1 each
      End If
    Else
    xModPatterns = vbNullChar ' always need at least 1 bit
    End If
  TrackerLoop = False
  End Sub

Public Sub MainForm_ReadLate(ByVal Filename As String, ByVal UpdateMode As Boolean)
'Exit Sub
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  'Dim xChar3 As String
  Dim IsMidiValue As Long
  Static xType1 As ctSSLayerSongInfoStruct
  
'Exit Sub
  If UpdateMode Then
    IsMidiValue = xSongIsMidi
    Else
    IsMidiValue = xSongWillBeMidi
    End If
  
  If Filename = "" Then 'Or FileName = Chr(1&) Then
    Filename = PLCurSong
    'ClearOldSongInfo
    End If

  If (MP3GetID3Tag Or MP3GetFormat) Then
    Select Case CurMusic
      Case ctSSLayer_MusicTypeNone
        ClearOldSongInfo
      Case ctSSLayer_MusicTypeStream
        If Not ctSSLayer_ReadSongInfo(SongPointer(CurrentSongID), xType1) Then
          xType1.FormatOK = 0&
          xType1.TagOK = 0&
          End If
        If xType1.TagOK = 0& Then 'And Not IsMidiValue = 3& Then
          curTagText = ""
          SetWindowText xStaticIconsHWnd(xStaticIcons_Tags), ""
          Else
          xChar1 = Replace(LoadResEx_String(1185&), "|", vbCrLf)
          GoSub ComReplace
          If Not curTagText = xChar1 Then
            curTagText = xChar1
            SetWindowText xStaticIconsHWnd(xStaticIcons_Tags), curTagText
            End If
          End If
        xLong1 = 1920& - CLng(Not (xType1.FormatOK = 0&)) - 10& * CLng(IsMidiValue = 3&)
        If (xLong1 = 1930& Or xLong1 = 1931&) And Not UrlsAllowed Then xLong1 = xLong1 + 3&
        xChar2 = LoadResEx_String(xLong1)


        If xType1.FormatOK = 0& Then 'And Not IsMidiValue = 3& Then  'And xType1.Format_Quality < 7& Then
'Debug.Print "FormatOK"
          If Not UpdateMode Then
            If IsMidiValue = 3& Then
              Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(4), 0&)
              Else
              Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(0), 0&)
              End If
            End If
          Else
          'If CurMusic = ctSSLayer_MusicTypeInetStream Then
            xChar2 = Replace(xChar2, "$B", CStr(Fix(xType1.Format_BitRate \ 1000&))) '\ 1000&)))
          '  Else
          '  End If
          xChar2 = Replace(xChar2, "$F", CStr(xType1.Format_Frequency \ 1000&)) ' \ 1000&)))
          If xType1.Format_Channels > 2& Then
            xChar2 = Replace(xChar2, "$C", Replace(LoadResEx_String(1992&), "$", CStr(xType1.Format_Channels)))
            Else
            xChar2 = Replace(xChar2, "$C", LoadResEx_String(1989& + xType1.Format_Channels))
            End If
          If Not UpdateMode Then
            If IsMidiValue = 3& Then
              Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(4), GetQualityStreamRGB(xType1))
              Else
              Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(0), GetQualityStreamRGB(xType1))
              End If
            End If
          End If
      'Case ctSSLayer_MusicTypeInetStream
      Case ctSSLayer_MusicTypeTracker
        If IsMidiValue Then
          Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(1), 0&)
          xChar2 = LoadResEx_String(1902&)
          Else
          Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(2), 0&)
          xChar2 = LoadResEx_String(1903&)
          End If
      Case ctSSLayer_MusicTypeCDAudio
        Call MainForm_DrawRawBMP_CalaymStatic(-1&, FormatIcon(3), 0&)
        xChar2 = LoadResEx_String(1904&)
      End Select
      
    If IsMidiValue = 3& Then
'Debug.Print "ReCreate"
      'xchar2=xchar1
      xChar1 = LoadResEx_String(1190&)
      GoSub ComReplace
'Debug.Print "  IsNow:", xChar1
      If Not PLCurSongDisplay = xChar1 Then
'Debug.Print "  Update!"
        PLCurSongDisplay = xChar1
        MainForm_UpdateMainIcon
        Call GetTimeFmt(True)
        #If FlatCastCF17 Then
          xFCI.Text0 = xChar1
          #End If
        #If LoudestSongMethode Then
          Tear_Caption(CurrentSongID) = PLCurSongDisplay
          #Else
          If Not xPStateIconPaused Then
            ShowIR
            End If
          #End If
        #If PersonalStuffABC Then
          xChar1 = "$A - $T (Internetradio)"
          GoSub ComReplace
          ABChat_SetSong xChar1
          #End If
        xChar1 = LoadResEx_String(1191&)
        GoSub ComReplace
        MainForm_ctConsoleA_AddSong xChar1
        End If
      If Len(xType1.IR_Name) = 0& Then
        xType1.IR_Name = LoadResEx_String(1936&)
        End If
      If Len(xType1.IR_URL) = 0& Then
        xType1.IR_URL = LoadResEx_String(1937&)
        ElseIf Len(SenderURL) = 0& Then
        SenderURL = xType1.IR_URL
        End If
      'xChar1 = LoadResEx_String(1936&)
      LastStreamTT = Replace(Replace(Replace(xChar2, "$U", Chr(1)), "$T", xType1.IR_Name), Chr(1), xType1.IR_URL)
      'If vKioskMode Then
      '  SetformatToolTip LastStreamTT, ""
      '  ElseIf UrlsAllowed Then
      '  SetformatToolTip LastStreamTT, Replace(Replace(Replace(LoadResEx_String(1932&), "$I", ExtKbdEx(KbdC_OInternetRadio)), "$R", ExtKbdEx(KbdC_OInternetRadioRe)), "$U", ExtKbdEx(KbdC_OInternetRadioHP))
      '  Else
      '  SetformatToolTip LastStreamTT, Replace(Replace(LoadResEx_String(1935&), "$I", ExtKbdEx(KbdC_OInternetRadio)), "$R", ExtKbdEx(KbdC_OInternetRadioRe))
      '  End If
      ResetStreamTT
      Else
      xChar1 = GetExtension(Filename)
      SetFormatToolTip Replace(xChar2, "$T", UCase(Left(xChar1, 1&)) & Mid(xChar1, 2&)), ""
      End If
      
    'xChar2 = GetExtension(Filename)
    'SetformatToolTip Replace(xChar1, "$T", UCase(Left(xChar2, 1&)) & Mid(xChar2, 2&)), xChar3
    End If
  
  Exit Sub
  
ComReplace:
  xChar1 = Replace(Replace(Replace(Replace(Replace(Replace(xChar1 _
           , "$A", Chr(1&)) _
           , "$T", Chr(2&)) _
           , "$L", Chr(3&)) _
           , "$N", Chr(4&)) _
           , "$Y", Chr(5&)) _
           , "$S", Chr(6&))
  If xType1.Tag_Artist = "" Then xType1.Tag_Artist = LoadResEx_String(1186&)
  If xType1.Tag_Title = "" Then xType1.Tag_Title = LoadResEx_String(1186&)
  If xType1.Tag_Album = "" Then xType1.Tag_Album = LoadResEx_String(1186&)
  xChar1 = Replace(Replace(Replace(xChar1 _
           , Chr(1&), xType1.Tag_Artist) _
           , Chr(2&), xType1.Tag_Title) _
           , Chr(3&), xType1.Tag_Album)
  If xType1.Tag_TrackID = 0& Then
    xChar1 = Replace(xChar1, Chr(4&), LoadResEx_String(1186&))
    Else
    xChar1 = Replace(xChar1, Chr(4&), CStr(xType1.Tag_TrackID))
    End If
  If xType1.Tag_Year = 0& Then
    xChar1 = Replace(xChar1, Chr(5&), LoadResEx_String(1186&))
    Else
    xChar1 = Replace(xChar1, Chr(5&), CStr(xType1.Tag_Year))
    End If
  If Len(SenderURL) = 0& Then
    If Len(xType1.IR_Name) = 0& Then
      If Len(xType1.IR_URL) = 0& Then
        xChar1 = Replace(xChar1, Chr(6&), LoadResEx_String(1192&))
        Else
        xChar1 = Replace(xChar1, Chr(6&), xType1.IR_URL)
        End If
      Else
      xChar1 = Replace(xChar1, Chr(6&), SenderURL)
      End If
    Else
    xChar1 = Replace(xChar1, Chr(6&), xType1.IR_Name)
    End If
  Return
  End Sub

Private Function GetQualityStreamRGB(ByRef xType1 As ctSSLayerSongInfoStruct) As Long
  Dim xSingle1 As Single
  
  With xType1
    xSingle1 = (0.5 + .Format_Frequency / 22050& * 0.25) * (Sqr(.Format_Channels) - 0.4) * .Format_BitRate / 224000 * (1& - 0.15 * CLng(.Format_VBR)) * .Format_QualityMulti
    End With
  
  Select Case xSingle1
    Case Is >= 1
      GetQualityStreamRGB = &H100A000
    Case 0.75 To 1
      GetQualityStreamRGB = (((1 - xSingle1) * &H2800000) And &HFF0000) + &H100A000
    Case 0.5 To 0.75
      GetQualityStreamRGB = ((xSingle1 * &H28000 - &H14000) And &HFF00&) + &H1A00000
    Case 0.25 To 0.5
      GetQualityStreamRGB = &H1A00140 - xSingle1 * &H280&
    Case 0 To 0.25
      GetQualityStreamRGB = ((xSingle1 * &H2800000) And &HFF0000) + &H10000A0
    End Select
'  Debug.Print "Q: " + Hex(GetQualityStreamRGB)
'  Stop
  End Function

Private Sub ProcessTimer(ByVal TimerEvSetts As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar2 As String
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "ShouldDoTimer: " & TimerEvSetts
    #End If
  xLong1 = InStr(TimerEvSetts, "=")
  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
        If ProcessTimerEvent(Val(xChar2), "") Then Exit Do ', TimerIndex) Then Exit Do
        Else
        Call ProcessTimerEvent(Val(Left(xChar2, xLong3 - 1&)), Trim(Mid(xChar2, xLong3 + 1&))) ', TimerIndex)
        End If
      xLong1 = xLong2
      End If
    Loop
  End Sub
  
Private Function ProcessTimerEvent(ByVal EventID As Long, ByVal Filename As String) As Boolean ', ByVal TimerIndex As Long) As Boolean
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "  Event #" & Right(Space(4) & CStr(EventID), 4&) & ": " + LoadResEx_String(3700& + EventID)
    If HasSubFile(EventID) Then MainForm_ctConsoleA_AddLog "  with file: " & Filename
    #End If
  Dim xLong1 As Long
  Dim xChar1 As String
  
  If Not (PlayerIsLocked Or xAutoDoing) Then
    Select Case EventID
      Case KbdC_FAddFileS
        If AllowedItem(Filename, True) = 1& Then
          If PLCount < PLCountMaxX Then
            xChar1 = SimpleDir(Filename)
            #If BoldDirs = 98 Then
              PLI(PLCount).Filename = xChar1
              PLI(PLCount).Caption = xChar1
              PLI(PLCount).DirDepthFlags = PLI_IsDir
              'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
              PLCount = PLCount + 1&
              PLI(PLCount).Filename = Filename
              PLI(PLCount).Caption = SimpleFile(Filename)
              PLI(PLCount).DirDepthFlags = 1&
              'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
              #Else
              PLIFile(PLCount) = xChar1
              PLICap(PLCount) = xChar1
              PLIDF(PLCount) = PLI_IsDir
              'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
              PLCount = PLCount + 1&
              PLIFile(PLCount) = Filename
              PLICap(PLCount) = SimpleFile(Filename)
              PLIDF(PLCount) = 1&
              'Call SendMessage(xListHWnd(2), &H180&, 0&, PLCount)
              #End If
            PLCount = PLCount + 1&
            RealPLAdd PLCount - 2&
'            MainForm_xBuildDirs
            End If
          End If
      Case KbdC_FAddDir
        If AllowedItem(Filename, True) = 0& Then
          SetAutoDoing True
          xPStateIconRefreshing = True
          MainForm_UpdateMainIcon
          AbortReading = False
          MainForm_LockMe True
          DoEvents
          xLong1 = PLCount
          ReadDirectory Filename, 0&, True, 0&
          RealPLAdd xLong1
          MainForm_LockMe False
          xPStateIconRefreshing = False
          MainForm_UpdateMainIcon
          SetAutoDoing False
          CheckReplay
          End If
      Case KbdC_FLoadPL, KbdC_FAddPList
        If AllowedItem(Filename, True) = 255& Then
          If EventID = KbdC_FLoadPL Then
            PLCount = 0&
            xRadioCount = 0&
            RadioForm_PrePreset
            End If
          Call LoadPList(xChar1, EventID = KbdC_FAddPList, False) ', False)
          End If
'      Case KbdC_FLoadPL
      Case KbdC_FSavePLas
        If AllowedItem(Filename, False) = 255& Then
          SavePList Filename, False
          End If
      Case KbdC_OTimerImport
        If AllowedItem(Filename, True) = 254& Then
          Call MainForm_LoadTimers(MainHWnd, Filename, True)
          End If
      Case KbdC_OTimerExport
        If LCase(Right(Filename, 4&)) = ".cpt" Then
          ' silent mode!
          MainForm_SaveTimers 0&, Filename, True ', xShowSpectrum
          End If
      Case KbdC_OPresetSave
        If LCase(Right(Filename, 4&)) = ".cps" Then
          SavePreset Filename
          End If
      Case KbdC_OPresetLoad
        If AllowedItem(Filename, True) = 253& Then
          MainForm_LoadSettings Filename
          End If
      Case KbdCTm_FShutDown
        Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 2&)
      Case KbdCTm_FHibernate
        Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 4&)
      Case KbdCTm_FStandby
        Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 5&)
      Case KbdCTm_FMonitoff
        Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 6&)
      Case KbdCTm_CPlay
        If xPStateIconPlaying Then
          If xPStateIconPaused Then MainForm_xPauseClick
          Else
          MainForm_xPlayClick False
          End If
      Case KbdCTm_CPause
        If xPStateIconPlaying And xPStateIconPaused Then
          MainForm_xPauseClick
          End If
      Case KbdCTm_CStop
        If Not Len(PlNextSong) = 0& Then
          SetNextSong False
          End If
        MainForm_IconsMouseUp 3&
        MainForm_LockMe True
        Call Tear_WaitFor(-2&)
        MainForm_LockMe False
      Case KbdCTm_CIntroModeOn
        If Not IntroModeActive Then
          MainForm_ProcessKey KbdC_CIntroMode
          End If
      Case KbdCTm_CIntroModeOff
        If IntroModeActive Then
          MainForm_ProcessKey KbdC_CIntroMode
          End If
      Case KbdCTm_PFindCurrent
        MainForm_MenuEventPlaylist 400&
      Case KbdCTm_OFadingOn, KbdCTm_OFadingOff
        If xGenFading = (EventID = KbdCTm_OFadingOff) Then MainForm_ProcessKey KbdC_OFading
      Case KbdCTm_OSpektrumOn, KbdCTm_OSpektrumOff
        'If Not vKioskMode Then
          If xShowSpectrum = (EventID = KbdCTm_OSpektrumOff) Then MainForm_ProcessKey KbdC_OSpektrum, False
        '  End If
      Case KbdCTm_OFileInfosOn, KbdCTm_OFileInfosOff
        If MP3GetID3Tag = (EventID = KbdCTm_OFileInfosOff) Then MainForm_ProcessKey KbdC_OFileInfos
      Case KbdCTm_OFollowSongOn, KbdCTm_OFollowSongOff
        If xFollowCurSong = (EventID = KbdCTm_OFollowSongOff) Then MainForm_ProcessKey KbdC_OFollowSong
      Case KbdCTm_ORemMissingOn, KbdCTm_ORemMissingOff
        If xRemoveMissingSongs = (EventID = KbdCTm_ORemMissingOff) Then MainForm_ProcessKey KbdC_ORemMissing
      Case KbdCTm_ORemSkippedOn, KbdCTm_ORemSkippedOff
        If xRemoveSkippedSongs = (EventID = KbdCTm_ORemSkippedOff) Then MainForm_ProcessKey KbdC_ORemSkipped
      Case KbdCTm_ORemPlayedOn, KbdCTm_ORemPlayedOff
        If xRemovePlayedSongs = (EventID = KbdCTm_ORemPlayedOff) Then MainForm_ProcessKey KbdC_ORemPlayed
      Case KbdCTm_ONoEndMode
        If Not xProcessMode = 0& Then MainForm_ProcessKey KbdC_ONoEndMode
      Case KbdCTm_OXFadeOn
        If Not xProcessMode = 1& Then MainForm_ProcessKey KbdC_OXFade
      Case KbdCTm_OMDModeOff
        If Not xProcessMode = 2& Then MainForm_ProcessKey KbdC_OMDMode
      Case KbdCTm_OStopEachOn
        If Not xProcessMode = 3& Then MainForm_ProcessKey KbdC_OStopEach
      'Case KbdCTm_OEndModeOff
      '  If Not xProcessMode = 0& Then
      '    xProcessMode = 0&
      '    MainForm_ApplyProcessMode
      '    End If
      Case Else
        MainForm_ProcessKey EventID, False
      End Select
    End If
  End Function

Private Function GetValidSkipper(ByVal MusType As Long, ByVal xLong1 As Long) As Long
  Select Case xLong1
    Case Is < xAnySkipperMaxN
      xTrackerSkipper(MusType) = xAnySkipperMaxN
    Case Is > xAnySkipperMax
      xTrackerSkipper(MusType) = xAnySkipperMax
    Case Is < xAnySkipperMinN, Is > xAnySkipperMin
      xTrackerSkipper(MusType) = xLong1
    Case Is < 0
      xTrackerSkipper(MusType) = -xAnySkipperMinN
    'Case Is < TrackerSkipperMinValM, Is > TrackerSkipperMinVal
    Case Else '>=0
      xTrackerSkipper(MusType) = xAnySkipperMin
    End Select
  End Function

Private Sub ClearPLEx()
  Call SendMessage(xListHWnd(2), &H184&, 0&, 0&)
  Call SendMessage(xListHWnd(2), &H194&, 0&, 0&)
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "ClearDirCap due ClearPLEx"
    #End If
  MainForm_SetDirCaption ""
  xDirCount = 0&
  'eDropDown(0).Clear
  Call SendMessage(xListHWnd(0), &H184&, 0&, 0&)
  Call SendMessage(xListHWnd(0), &H194&, 0&, 0&)
  End Sub

'#If LockWinUpd = 2& Or AnimateReadDir Then
  Public Sub DoEventsEx_SleepPeriodicalLW(ByVal hWndLocked As Long)
    Static xLong1 As Long
    If DoEventsEx_IsTickCountElapsed(xLong1, DoEventsEx_SleepPeriodical_Interval) Then
      
'      #If LockWinUpd = 2& Then
      If Not hWndLocked = 0& Then Call LockWindowUpdate(0&)
      DoEventsEx_Sleep 25&
      If Not xHiddenAutoDoing Then Call DoAnimateReadDir(1&)
'      #If LockWinUpd = 2& Then
      If Not hWndLocked = 0& Then Call LockWindowUpdate(hWndLocked)
      xLong1 = (GetTickCount And &H3FFFFFFF)
      End If
    End Sub
'  #End If

Private Sub SetMenuShortcut(ByVal hMenu As Long, ByVal MenuID As Long, ByVal ShortCut As String)
  Dim xLong1 As Long
  Dim xChar1 As String
  
  xChar1 = LoadResEx_Menu_GetCaption(hMenu, MenuID)
  xLong1 = InStr(xChar1, vbTab)
  If xLong1 = 0& Then
    If Not Len(ShortCut) = 0& Then
      xChar1 = xChar1 + vbTab + ShortCut
      End If
    ElseIf Len(ShortCut) = 0& Then
    xChar1 = Left(xChar1, xLong1 - 1&)
    Else
    xChar1 = Left(xChar1, xLong1) + ShortCut
    End If
  LoadResEx_Menu_SetCaption hMenu, MenuID, xChar1
  End Sub
  
Public Sub MainForm_OLECompleteDrag() '(ByRef Effect As Long)
  If Not vKioskMode Then
    Call RegisterDragDrop(MainHWnd, ObjPtr(DragClass))
    xOLEisDragging = False
    End If
  End Sub

Public Sub MainForm_OLEStartDrag(ByRef Data As DataObject, ByRef AllowedEffects As Long)
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xOLEShowDragShift As Boolean
  Dim xOLEShowDragAlt As Boolean
  Dim xByteA1() As Byte
  
  xOLEShowDragShift = ((GetAsyncKeyState(&H10&) And &HFFFF0000) And LEDMode = 0&)
  xOLEShowDragAlt = ((GetAsyncKeyState(&H12&) And &HFFFF0000) And LEDMode = 0&)
  
  If DragOut And Not (PlayerIsLocked Or xAutoDoing) Then
    #If UseFileLogging Then
      MainForm_ctConsoleA_AddLog "Preparing OLE Dragging"
      #End If
    SetAutoDoing True
    If Not vKioskMode Then
      Call RevokeDragDrop(MainHWnd)
      End If
    
    Data.Files.Clear
    
    'If CalaymDragSource = 0& Then
      xLong1 = FloodSelected((GetAsyncKeyState(&H11&) And &HFFFF0000) And LEDMode = 0&)
      'Else
      'xLong1 = SearchSelected
    '  End If
    If Not xLong1 = 0& Then
      If xOLEShowDragShift Then
        Data.Clear
        If xOLEShowDragAlt Then
          For xLong1 = 0& To xLong1 - 1&
            xChar1 = xChar1 & vbCrLf & sCharA1(xLong1)
            Next
          Else
          For xLong1 = 0& To xLong1 - 1&
            xChar1 = xChar1 & vbCrLf & SimpleFile(sCharA1(xLong1))
            Next
          End If
        If IsUnicode Then
          xChar1 = Mid(xChar1, 3&) ' + vbNullChar
          xLong1 = Len(xChar1) * 2&
          ReDim xByteA1(xLong1 + 1&) '-1+2 'nullchars
          Call RtlMoveMemory(xByteA1(0), ByVal StrPtr(xChar1), xLong1)
          Data.SetData xByteA1(), 13& ' Mid(xChar1, 3&), 13&
          Else
          Data.SetData Mid(xChar1, 3&), 0
          End If
        AllowedEffects = 7&
        Else
        For xLong1 = 0& To xLong1 - 1&
          Data.Files.Add sCharA1(xLong1)
          Next
        Data.SetData , 15
        AllowedEffects = 7&
        End If
      End If
    xOLEisDragging = True
    SetAutoDoing False
    End If
  End Sub


Private Sub GetWhiteBackGrnd() '(ByVal NoApply As Boolean)
  'Dim xLong1 As Long
  'Dim xLong2 As Long
  'Dim xLong3 As Long
  Dim xChar1 As String
  'Const xColorModx As Long = &H18&
  
  'MSDN: The default delay times are based on the double-click time. For the default double-click time of 500 ms, the initial, autopop, and reshow delay times are 500ms, 5000ms, and 100ms respectively. The following code fragment uses the GetDoubleClickTime [ http://msdn.microsoft.com/en-us/library/ms646258(VS.85,printer).aspx ] function to determine the three delay times for any system.
  ToolTipDelay = GetDoubleClickTime
  
  ScrollComboWidth = GetSystemMetrics(2&)
  FLineColor = GetSysColor(13&)
  FLineColorX = GetSysColor(5&)
  FLineColorWDCTxt = GetSysColor(8&)
  FormatIconFore = GetSysColor(18&)
  FormatIconBack = GetSysColor(15&)
  FormatIconBackSunkenD = GetSysColor(16&)
  FormatIconBackSunkenW = GetSysColor(20&)
  FormatIconBackSunken = FormatIconBackSunkenW

  xDoAnimColor1 = MixRGB(FLineColor, FLineColorX, 66)
  xDoAnimColor2 = MixRGB(FLineColor, FLineColorX, 33)
  xDoAnimColor3 = MixRGB(FLineColor, FLineColorX, 10)
  
  
  
  Call SetBkColor(xSharedDC, FLineColorX)
  Call SetTextColor(xSharedDC, FLineColorWDCTxt)
  
  
  
  If xTimerEvCount = 0& Then
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_TimerInd, xTimerIconBMP, 0&)
    Else
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_TimerInd, xTimerIconBMP, &H10000000)
    End If
  
  MainForm_DrawVolFaderA xSetVolume, 2&
  
  xChar1 = LoadResEx_Data(1000&, 2&, LoadResEx_ThemeFile_hModule)
  If Len(xChar1) = 0& Then xChar1 = LoadResEx_Data(1000&, 2&, App.hInstance)
  
  Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def0, xChar1, &H0&)
  Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def4, xChar1, &H1000000)
  
  If Not vKioskMode Then
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H2000000)
    End If
  'Call MainForm_ApplyRepMode
  If IntroModeActive Then
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_RepMode, DynButtonsResXRep, &H10000000 + xRep * &H1000000)
    Else
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_RepMode, DynButtonsResXRep, xRep * &H1000000)
    End If
  
  If xPStateIconPlaying Then
    If xPStateIconPaused Then
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def1, DynButtonsResX, &H0)
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def2, DynButtonsResX, &H11000000)
      Else
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def1, DynButtonsResX, &H10000000)
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def2, DynButtonsResX, &H1000000)
      End If
    If xHasNextSong And Not SilentPlNext Then
      If vKioskMode Then
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H13000000)
        Else
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def5, DynButtonsResX, &H13000000)
        End If
      Else
      If vKioskMode Then
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H3000000)
        Else
        Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def5, DynButtonsResX, &H3000000)
        End If
      End If
    Else
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def1, DynButtonsResX, &H0)
    Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def2, DynButtonsResX, &H1000000)
    If vKioskMode Then
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H3000000)
      Else
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def5, DynButtonsResX, &H3000000)
      End If
    End If
  'Call DrawRawBMP_CalaymStatic(3&, DynButtonsResX, &H13000000)
  
#If ProcCtl Then
  If Not xStaticIconsHWnd(xStaticIcons_CurSongPos) = 0& Then
    Call SendMessage(xStaticIconsHWnd(xStaticIcons_CurSongPos), &H2001&, 0&, FLineColorX)
    End If
#End If
  
  MainForm_ReadLate "", False
  
  Call DoAnimateReadDir(0&)
  'MainForm_UpdateMyFont
  'MainForm_UpdateMyFontOther
  
  'xLong3 = GetSysColor(24&)
  '
  'xLong1 = xLong3 \ &H10000
  'xLong3 = xLong3 - xLong1 * &H10000
  'xLong2 = xLong3 \ &H100&
  ''xLong1 = (xLong1 + xLong2 + (xLong3 - xLong2 * &H100&)) / 3&
  'xLong1 = (xLong1 + xLong2 + (xLong3 - xLong2 * &H100&)) \ 3&
  '
  'If xLong1 < 96& Then xLong1 = 1& Else xLong1 = 0&
  
  End Sub

Private Function MixRGB(ByVal Color1 As Long, ByVal Color2 As Long, Optional ByVal PercentColor1 As Single = 50)
  If (Color1 And &H80000000) = &H80000000 Then
    Color1 = GetSysColor(Color1 And &HFFFFFF)
    End If
  If (Color2 And &H80000000) = &H80000000 Then
    Color2 = GetSysColor(Color2 And &HFFFFFF)
    End If
  Select Case PercentColor1
    Case 50
      MixRGB = ((((Color1 And &HFF0000) + (Color2 And &HFF0000)) \ &H2&) And &HFF0000) + _
               ((((Color1 And &HFF00&) + (Color2 And &HFF00&)) \ &H2&) And &HFF00&) + _
               ((((Color1 And &HFF&) + (Color2 And &HFF&)) \ &H2&) And &HFF&)
    Case 0
      MixRGB = Color2
    Case 100
      MixRGB = Color1
    Case Else
      If PercentColor1 < 0 Then
        PercentColor1 = 0
        ElseIf PercentColor1 > 100 Then PercentColor1 = 1
        Else
        PercentColor1 = PercentColor1 / 100
        End If
      MixRGB = (1 - PercentColor1)
      MixRGB = (((Color1 And &HFF0000) * PercentColor1 + (Color2 And &HFF0000) * MixRGB) And &HFF0000) + _
               (((Color1 And &HFF00&) * PercentColor1 + (Color2 And &HFF00&) * MixRGB) And &HFF00&) + _
               (((Color1 And &HFF&) * PercentColor1 + (Color2 And &HFF&) * MixRGB) And &HFF&)
    End Select
  End Function


Private Sub SetNextSong(ByVal SetNow As Boolean)
  If SetNow Then
    xHasNextSong = True
    If vKioskMode Then
      'eImage(10).ForeColor = &H8000000E
      'eImage(10).BackColor = &H8000000D
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H13000000)
      MainForm_EnableApiMenu 104&, True
      SetToolTip xStaticIcons_Def3, Replace(LoadResEx_String(1142&), "$", PlNextSongCap), ExtKbdEx(KbdC_CStop)
      Else
      'eImage(5).ForeColor = &H8000000E
      'eImage(5).BackColor = &H8000000D
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def5, DynButtonsResX, &H13000000)
      SetToolTip xStaticIcons_Def3, LoadResEx_String(1127&), ExtKbdEx(KbdC_CStop)
      SetToolTip xStaticIcons_Def5, Replace(LoadResEx_String(1137&), "$", PlNextSongCap), ExtKbdEx(KbdC_CEnd)
      End If
    SilentPlNext = False
    CheckApiMenu 106, True
    If CurMusic = ctSSLayer_MusicTypeTracker And Not xSongIsMidi Then
      Call ctSSLayer_UpdateLooping(SongPointer(CurrentSongID), False)
      End If
    Else
    xHasNextSong = False
    If vKioskMode Then
      'eImage(10).ForeColor = &H80000008
      'eImage(10).BackColor = &H80000005
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def3, DynButtonsResX, &H3000000)
      MainForm_EnableApiMenu 104&, False
      SetToolTip xStaticIcons_Def3, LoadResEx_String(1140&), ExtKbdEx(KbdC_CStop)
      Else
      'eImage(5).ForeColor = &H80000008
      'eImage(5).BackColor = &H80000005
      Call MainForm_DrawRawBMP_CalaymStatic(xStaticIcons_Def5, DynButtonsResX, &H3000000)
      SetToolTip xStaticIcons_Def3, LoadResEx_String(1125&), ExtKbdEx(KbdC_CStop)
      SetToolTip xStaticIcons_Def5, LoadResEx_String(1135&), ExtKbdEx(KbdC_CEnd)
      End If
    PlNextSong = ""
    PlNextSongCap = ""
    SilentPlNext = False
    CheckApiMenu 106&, False
    If CurMusic = ctSSLayer_MusicTypeTracker And Not xSongIsMidi Then
      Call ctSSLayer_UpdateLooping(SongPointer(CurrentSongID), (xRep = 0&))
      End If
    End If
  End Sub

Private Sub SetFormatToolTip(ByVal xChar1 As String, ByVal xChar2 As String)
  FormatToolTip1 = xChar1
  FormatToolTip2 = xChar2
  SetToolTip xStaticIcons_CurSong, Replace(CurSongToolTip1, "$M", FormatToolTip1), CurSongToolTip2 + FormatToolTip2
  End Sub

Private Sub SetToolTip(ByVal Index As Long, ByVal xChar1 As String, ByVal xChar2 As String)
  If Not (xStaticIconsToolTip(Index, 0&) = xChar1 And xStaticIconsToolTip(Index, 1&) = xChar2) Then
    xStaticIconsToolTip(Index, 0&) = xChar1
    xStaticIconsToolTip(Index, 1&) = xChar2
    If ToolTipInitTime = -1& And Index = CurShownToolTip Then
      CalcToolTip
      'Call SendMessage(xStaticIconsHWnd(xStaticIcons_ToolTip), &H14&, 0&, 0&)
      MainForm_EraseBackgroundTT
      Call SendMessage(xStaticIconsHWnd(xStaticIcons_ToolTip), &HF&, 0&, 0&)
      End If
    End If
  End Sub

Public Sub CalcToolTip()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar1 As String
  Dim xType1 As POINTAPI
  Dim xType2 As POINTAPI
  Dim xType3 As RECT
  Dim xCount As Long
  
  xChar1 = xStaticIconsToolTip(CurShownToolTip, 0&) + TTSplitter
  Call SelectObject(xSharedDC, hFontTTip)
  Do
    xLong1 = InStr(xChar1, TTSplitter)
    If xLong1 = 0& Then
      Exit Do
      Else
      If xCount = 1& Then xType3.Bottom = xType3.Bottom + 5&
      xCount = xCount + 1&
      Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(xChar1), xLong1 - 1&, xType1)
      If xType1.x > xType3.Right Then xType3.Right = xType1.x
      xType3.Bottom = xType3.Bottom + CurShownToolTip_SpHeight
      xChar1 = Mid(xChar1, xLong1 + 1&)
      End If
    Loop
  xType3.Right = xType3.Right + TTSpaceLeft
  
  CurShownToolTip_SpKey = 0&
  If Not Len(xStaticIconsToolTip(CurShownToolTip, 1&)) = 0& Then
    'If xCount < 2& Then xType3.Bottom = xType3.Bottom + 5&
    xType3.Bottom = xType3.Bottom + 4&
    
    xChar1 = xStaticIconsToolTip(CurShownToolTip, 1&) + TTSplitter2
    If InStr(xChar1, vbTab) = 0& Then xChar1 = vbTab + xChar1
    Do
      xLong1 = InStr(xChar1, TTSplitterTab)
      xLong2 = InStr(xChar1, TTSplitter)
      If xLong1 = 0& Or xLong2 = 0& Or xLong1 > xLong2 Then
        Exit Do
        Else
        Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(xChar1), xLong1 - 1&, xType1)
        If xType1.x > CurShownToolTip_SpKey Then CurShownToolTip_SpKey = xType1.x
        xChar1 = Mid(xChar1, xLong1 + 1&)
        Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(xChar1), xLong2 - xLong1 - 1&, xType1)
        If xType1.x > xLong3 Then xLong3 = xType1.x
        xType3.Bottom = xType3.Bottom + CurShownToolTip_SpHeight
        xChar1 = Mid(xChar1, xLong2 - xLong1 + 1&)
        End If
      Loop
    CurShownToolTip_SpKey = CurShownToolTip_SpKeyPre + CurShownToolTip_SpKey '+ TTSpaceLeft
    xLong3 = xLong3 + CurShownToolTip_SpKey '+CurShownToolTip_SpKeyPre
    If xLong3 > xType3.Right Then xType3.Right = xLong3
    End If
  
  xType3.Right = xType3.Right + TTSpaceLeft
  CurShownToolTip_TotalWidthX = xType3.Right
  xType3.Bottom = xType3.Bottom + TTSpaceTopD
  Call AdjustWindowRectEx(xType3, &H8E800000, 0&, &H84&)
  xType1.x = xType3.Right - xType3.Left
  xType1.y = xType3.Bottom - xType3.Top
  'GetTextOutWidth = sPointApi.x
  
  Call WindowToCursorPos(xType1, xType2)
  Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ToolTip), 0&, xType2.x, xType2.y, xType1.x, xType1.y, &H214&)
  End Sub

Private Sub WriteToolTip()
  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 xhPen As Long
  Dim xhPenOld As Long
  Dim xCount As Long
  
  'If ToolTipInitTime = -1& And Not CurShownToolTip = -1& Then
    xLong1 = GetDC(xStaticIconsHWnd(xStaticIcons_ToolTip))
    If Not xLong1 = 0& Then
      xLong5 = SelectObject(xLong1, hFontTTip)
      Call SetBkColor(xLong1, GetSysColor(24&))
      Call SetTextColor(xLong1, GetSysColor(23&))
      Call SetBkMode(xLong1, 1&)

      xLong4 = TTSpaceTop
      
      xChar1 = xStaticIconsToolTip(CurShownToolTip, 0&) + TTSplitter
      Do
        xLong2 = InStr(xChar1, TTSplitter)
        If xLong2 = 0& Then
          Exit Do
          Else
          If xCount = 1& Then GoSub AddLine
          xCount = xCount + 1&
          Call TextOut(xLong1, Left(xChar1, xLong2 - 1&), TTSpaceLeft, xLong4)
          xLong4 = xLong4 + CurShownToolTip_SpHeight
          xChar1 = Mid(xChar1, xLong2 + 1&)
          End If
        Loop
  
  
      If Not Len(xStaticIconsToolTip(CurShownToolTip, 1&)) = 0& Then
        'If xCount < 2& Then GoSub AddLine
        xLong4 = xLong4 + 4&
        
        Call TextOut(xLong1, LoadResEx_String(1100&), TTSpaceLeft, xLong4)
        
        xChar1 = xStaticIconsToolTip(CurShownToolTip, 1&) + TTSplitter '2
        If InStr(xChar1, vbTab) = 0& Then xChar1 = vbTab + xChar1
        Do
          xLong2 = InStr(xChar1, TTSplitterTab)
          xLong3 = InStr(xChar1, TTSplitter)
          If xLong2 = 0& Or xLong3 = 0& Or xLong2 > xLong3 Then
            Exit Do
            Else
            If xLong2 = 0& Then xLong3 = xLong2: xLong2 = Len(xChar1)
            Call TextOut(xLong1, Left(xChar1, xLong2 - 1&), CurShownToolTip_SpKeyPre, xLong4)
            Call TextOut(xLong1, Mid(xChar1, xLong2 + 1&, xLong3 - xLong2 - 1&), CurShownToolTip_SpKey, xLong4)
            xLong4 = xLong4 + CurShownToolTip_SpHeight
            xChar1 = Mid(xChar1, xLong3 + 1&)
            End If
          Loop
        End If
      Call SelectObject(xLong1, xLong5)
      Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_ToolTip), xLong1)
      End If
'    End If
  GoTo LblEndSub
  
AddLine:
  xLong4 = xLong4 + 2&
  Call MoveToEx(xLong1, 1&, xLong4, 0&)
  xhPen = CreatePen(0&, 0&, GetSysColor(23&))
  If Not xhPen = 0& Then
    xhPenOld = SelectObject(xLong1, xhPen)
    Call LineTo(xLong1, CurShownToolTip_TotalWidthX, xLong4)
    Call SelectObject(xLong1, xhPenOld)
    Call DeleteObject(xhPen)
    End If
  xLong4 = xLong4 + 3&
  Return

LblEndSub:
  End Sub

Private Sub MainForm_EraseBackgroundTT()
  Dim xLong1 As Long
  Dim xType1 As RECT
  
  xLong1 = GetDC(xStaticIconsHWnd(xStaticIcons_ToolTip))
  If Not xLong1 = 0& Then
    Call GetClientRect(xStaticIconsHWnd(xStaticIcons_ToolTip), xType1)
    Call FillRect(xLong1, xType1, 25&)
    Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_ToolTip), xLong1)
    End If
  End Sub





Private Function DropFiles(ByVal hDrop As Long, ByVal Message As Long, ByVal wParam0 As Long, ByVal lParamKeyStates As Long) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim Shift As Long
  Dim xCharA1() As String
  
  'Shift
  'Shift = -CLng(IsShiftPressed)
  Shift = (lParamKeyStates And 6) ' msshift 4 mkrbutton 2
  xLong1 = DragQueryFileCount(hDrop)
  If Not xLong1 = 0 Then
    ReDim xCharA1(xLong1 - 1&)
    If Not OLEDropMemory = 0& Then
      Call LocalFree(OLEDropMemory)
      OLEDropMemory = 0&
      End If
    For xLong1 = 0& To xLong1 - 1&
      If DragQueryFile(hDrop, xLong1, xCharA1(xLong4)) Then
        xLong2 = xLong2 + Len(xCharA1(xLong4))
        xLong4 = xLong4 + 1&
        End If
      Next
    OLEDropMemory = LocalAlloc(0&, xLong2 * 2& + (xLong4 + 2&) * 4&) 'datacount, button
    If Not OLEDropMemory = 0& Then
      xLong2 = OLEDropMemory
      Call PokeL(xLong2, xLong4)
      UIntAddUInt xLong2, 4&
      Call PokeL(xLong2, CLng(Shift))
      UIntAddUInt xLong2, 4&
      For xLong1 = 0& To xLong4 - 1&
        xLong3 = Len(xCharA1(xLong1))
        Call PokeL(xLong2, xLong3)
        UIntAddUInt xLong2, 4&
        xLong3 = xLong3 * 2&
        Call RtlMoveMemory(ByVal xLong2, ByVal StrPtr(xCharA1(xLong1)), xLong3)
        Call UIntAddUInt(xLong2, xLong3)
        Next
      Call PostMessage(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 1&)
      End If
    End If
  DragFinish hDrop
  End Function

Private Sub MainForm_DrawRawBMP_CalaymStatic(ByVal xIsMp3Icon As Long, ByVal xData As String, ByRef IconMod As Long)
  Dim xType1 As BITMAPINFOHEADER
  Dim xChar1 As String
  Dim xLong1 As Long
  Dim xLong1F As Long
  Dim oldXLong1 As Long
  Dim oldXLong2 As Long
  Dim PicDC As Long
  Dim xType2 As RECT

  xType2.Right = 20& + 4& * CLng(xIsMp3Icon = -1&)
  xType2.Bottom = 16
  
  xLong1F = CreateCompatibleBitmapX(xType2.Right, 16&)
  If Not xLong1F = 0& Then
    oldXLong2 = SelectObject(xSharedDC, xLong1F)
    
    If (IconMod And &H10000000) Then
      Call FillRect(xSharedDC, xType2, 17&)
      xType2.Left = 1&
      xType2.Top = 1&
      xType2.Right = xType2.Right - 1&
      xType2.Bottom = xType2.Bottom - 1&
      xLong1 = 21& 'FormatIconBackSunken
      ElseIf xIsMp3Icon = -1& Then
      xLong1 = 6& 'FormatIconBack
      Else
      xLong1 = 16& 'FormatIconBack
      End If
      Call FillRect(xSharedDC, xType2, xLong1)
      xLong1 = GetSysColor(xLong1 - 1&)
    
    If Len(xData) > 40& Then
      Call RtlMoveMemory(xType1, ByVal xData, 40&)
      '40+4*2^1
      Select Case xType1.biBitCount
        Case 8&: xChar1 = Mid(xData, 1065&): xData = Left(xData, 1064&) ' 40 + 4*256 indexes
        Case 4&: xChar1 = Mid(xData, 105&): xData = Left(xData, 104&) ' 40 + 4*16 indexes
        Case 1&: xChar1 = Mid(xData, 49&): xData = Left(xData, 48&) ' 40 + 4*x indexes
        End Select
      
      If Not xChar1 = "" Then
        Mid(xData, 41& + (2& ^ xType1.biBitCount - 1&) * 4&, 4&) = Chr(xLong1 \ &H10000) & Chr((xLong1 And &HFF00&) \ &H100&) & Chr(xLong1 And &HFF&) & vbNullChar
        xLong1 = FormatIconFore '&H0&
        Mid(xData, 41& + (2& ^ xType1.biBitCount - 2&) * 4&, 4&) = Chr(xLong1 \ &H10000) & Chr((xLong1 And &HFF00&) \ &H100&) & Chr(xLong1 And &HFF&) & vbNullChar
        If Not xType1.biBitCount = 1& Then
          xLong1 = IconMod And &HFFFFFF  '&HFF&
          Mid(xData, 41& + (2& ^ xType1.biBitCount - 3&) * 4&, 4&) = Chr(xLong1 \ &H10000) & Chr((xLong1 And &HFF00&) \ &H100&) & Chr(xLong1 And &HFF&) & vbNullChar
          End If
        
        xLong1 = CreateCompatibleBitmapX(xType1.biWidth, xType1.biHeight)
        If Not xLong1 = 0& Then
          PicDC = CreateCompatibleDC(0&)
          If Not PicDC = 0& Then
            Call SetDIBits(PicDC, xLong1, 0&, xType1.biHeight, ByVal xChar1, ByVal xData, 0&)
            oldXLong1 = SelectObject(PicDC, xLong1)
            If xIsMp3Icon = -1& Then
              Call BitBlt(xSharedDC, 0&, 0&, 16&, 16&, PicDC, ((IconMod And &HF000000) \ &H1000000) * 16&, 0&, vbSrcCopy)
              Else
              Call BitBlt(xSharedDC, 2&, 1&, 16&, 14&, PicDC, ((IconMod And &HF000000) \ &H1000000) * 16&, 0&, vbSrcCopy)
              End If
            Call SelectObject(PicDC, oldXLong1)
            Call DeleteDC(PicDC)
            End If
          Call DeleteObject(xLong1)
          End If
        End If
      End If
    Call SelectObject(xSharedDC, oldXLong2)
    
    If xIsMp3Icon = -1& Then
      If Not MP3FormatBitmap = 0& Then
        Call DeleteObject(MP3FormatBitmap)
        End If
      MP3FormatBitmap = xLong1F
      Else
      SendMessage xStaticIconsHWnd(xIsMp3Icon), &H172&, 0&, xLong1F
      If Not xStaticIconsBMP(xIsMp3Icon) = 0& Then
        DeleteObject xStaticIconsBMP(xIsMp3Icon)
        End If
      xStaticIconsBMP(xIsMp3Icon) = xLong1F
      End If
    End If
  End Sub

Private Sub RemoveListItemMS(ByVal hWnd As Long, ByVal Index As Long)
  Dim xLong1 As Long
  
  xLong1 = SendMessage(hWnd, &H19F&, 0&, 0&)
'  Debug.Print "OldCaret (Get): ", xLong1
  Call SendMessage(hWnd, &H182&, Index, 0&)
#If UseFileLogging Then
  Debug.Print "Remove: ", xLong1, "from", hWnd
#End If
  If xLong1 >= Index And Not xLong1 = 0& Then
    Call SendMessage(hWnd, &H19E&, xLong1 - 1&, 0&)
'    Debug.Print "NewCaret (Set): ", xLong1 - 1&
    End If
  End Sub

Private Sub InitResearchClear()
  If InterfaceMode = Interface_Mode_Search Then InitCPList
  End Sub

Private Sub InitResearch(ByVal Force As Boolean)
  If InterfaceMode = Interface_Mode_Search Then MainForm_TrySearch Force
  End Sub

Private Sub RemoveFromSearch(ByVal Index As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long

  If InterfaceMode = Interface_Mode_Search Then
    For xLong1 = 0& To xSearchCount - 1&
      If xSearch(xLong1) = Index Then
        'PLRemoveShifted1 = xLong2
        RemoveListItemMS Interface_Control(Interface_Control_Search_Results), xLong2
        ElseIf xSearch(xLong1) > Index Then
        xSearch(xLong2) = xSearch(xLong1) - 1&
        xSearchSortAlias(xLong2) = xSearchSortAlias(xLong1)
        Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H19A&, xLong2, xLong2)
        xLong2 = xLong2 + 1&
        Else
        xSearch(xLong2) = xSearch(xLong1)
        xSearchSortAlias(xLong2) = xSearchSortAlias(xLong1)
        Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H19A&, xLong2, xLong2)
        xLong2 = xLong2 + 1&
        End If
      Next
'    For xLong1 = 0& To xSearchCount - 1&
''      Debug.Print xLong1, "points to", xSearch(xLong1)
''      xSearchSortAlias(xLong1) = xSearchSortAlias(xLong1 + 1&)
'      Next
    xSearchCount = xLong2
    
    'PLRemoveShifted3 = &H7FFFFFFF
    End If
  End Sub

Private Sub SortSearchResults()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xLong5 As Long
  Dim ElementsToSort As Long
  
  ElementsToSort = xSearchCount - 1&
  For xLong2 = 0& To ElementsToSort
    xSearchSortAlias(xLong2) = xLong2
    #If BoldDirs = 98 Then
      xSearchSortCap(xLong2) = PLI(xSearch(xLong2)).Caption
      If (PLI(xSearch(xLong2)).DirDepthFlags And PLI_IsDir) = 0& Then
        xSearchSortDir(xLong2) = -3&
        ElseIf PLIconFolder = 0& Then
        xSearchSortDir(xLong2) = PLIconFolderFake
        Else
        xSearchSortDir(xLong2) = PLIconFolder
        End If
      #Else
      xSearchSortCap(xLong2) = PLICap(xSearch(xLong2))
      If (PLIDF(xSearch(xLong2)) And PLI_IsDir) = 0& Then
        xSearchSortDir(xLong2) = -3&
        ElseIf PLIconFolder = 0& Then
        xSearchSortDir(xLong2) = PLIconFolderFake
        Else
        xSearchSortDir(xLong2) = PLIconFolder
        End If
      #End If
    Next
  Do
    xLong1 = 3& * xLong1 + 1&
    Loop Until xLong1 > ElementsToSort
  Do
    xLong1 = xLong1 \ 3&
    For xLong2 = xLong1 To ElementsToSort
      xLong4 = xSearchSortAlias(xLong2)
      xLong5 = xSearch(xLong2)
      For xLong3 = xLong2 - xLong1 To 0& Step -xLong1
        'If Not xSearchSortCap(xSearchSortAlias(xLong3)) > xSearchSortCap(xLong4) Then Exit For
        If StrComp(xSearchSortCap(xSearchSortAlias(xLong3)), xSearchSortCap(xLong4), vbTextCompare) < 1 Then Exit For
        xSearchSortAlias(xLong3 + xLong1) = xSearchSortAlias(xLong3)
        xSearch(xLong3 + xLong1) = xSearch(xLong3)
        Next
      xSearchSortAlias(xLong3 + xLong1) = xLong4
      xSearch(xLong3 + xLong1) = xLong5
      Next
    Loop While xLong1
  End Sub

Private Sub UpdateFolderReg(ByVal DoSet As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long

  For xLong2 = 0& To 1&
    If RegFolderMode(xLong2) Then
      If DoSet Then
        If 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
        Else
        Call RegDeleteKey(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&) + "\command")
        Call RegDeleteKey(&H80000000, "Folder\Shell\" + LoadResEx_String(3534& + xLong2 * 5&))
        End If
      End If
    Next
  
  If RegFolderMode(2) Then
' playlistformatsimport
    If DoSet Then
      RegisterFileTypes_SetRegistered "m3u", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "M3U"), "", -1&, RegFolderMode(3), 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&, RegFolderMode(3), True, LoadResEx_String(3529&)
        RegisterFileTypes_SetRegistered "wpl", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "WPL"), "", -1&, RegFolderMode(3), True, LoadResEx_String(3529&)
        End If
      RegisterFileTypes_SetRegistered "pls", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "PLS"), "", -1&, RegFolderMode(3), True, LoadResEx_String(3529&)
      RegisterFileTypes_SetRegistered "aap", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "AAP"), "", -1&, RegFolderMode(3), True, LoadResEx_String(3529&)
      RegisterFileTypes_SetRegistered "xspf", LoadResEx_String(3525&), LoadResEx_String(3527&), LoadResEx_String(3526&), Replace(LoadResEx_String(3528&), "$", "XSPF"), "", -1&, RegFolderMode(3), True, LoadResEx_String(3529&)
      Else
      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 "aap", LoadResEx_String(3527&), LoadResEx_String(3529&)
      RegisterFileTypes_RemoveRegistered "xspf", LoadResEx_String(3527&), LoadResEx_String(3529&)
      RegisterFileTypes_RemoveRegistered "wpl", LoadResEx_String(3527&), LoadResEx_String(3529&)
      End If
    End If
  End Sub

Public Sub TestRestoreReg(ByVal ForceRemove As Boolean)
  Dim xLong1 As Long
  Dim xBool1 As Boolean
  Dim xChar1 As String

  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "CheckRegSet: " + CStr(-CLng(Reg_IsAutoRun))
    #End If
  If Reg_IsAutoRun Then
    ElseIf Not SoundSchemePointer = 0& Or Not SoundSchemePointerMode = 0& Then
    xBool1 = True
    Else
    For xLong1 = 0& To 2&
      If RegFolderMode(xLong1) Then xBool1 = True
      Next
    End If
    
  If (xBool1 Xor NeedRestoreReg) Or ForceRemove Then
    NeedRestoreReg = xBool1
    If NeedRestoreReg And Not ForceRemove Then
#If InIDE Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "RestoreRegSet (ignored due IDE)"
        #End If
#Else
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "RestoreRegSet"
        #End If
      If RegCreateKeyEx(&H80000002, "Software\Microsoft\Windows\CurrentVersion\RunOnce", xLong1) Then
        xChar1 = Chr(34) + xRealBaseFile + "exe" + Chr(34) + " /RESTOREREG"
        Call RegSetValueEx_String(xLong1, LoadResEx_String(3501&), xChar1)
        Call RegCloseKey(xLong1)
        End If
#End If
      Else
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "RestoreRegRemove"
        #End If
      If RegOpenKeyEx(&H80000002, "Software\Microsoft\Windows\CurrentVersion\RunOnce", 2&, xLong1) Then
        Call RegDeleteValue(xLong1, LoadResEx_String(3501&))
        Call RegCloseKey(xLong1)
        End If
      End If
    End If
  End Sub

Private Sub FindNowCB()
  Dim xChar1 As String
  
  If IsUnicode Then
    xChar1 = PeekS_Wide(CurrentFindHMem, 256&)
    Else
    xChar1 = PeekS_Ansi(CurrentFindHMem, 513&)
    End If
  If Not Len(Trim(xChar1)) = 0& Then
          '  xChar1 = InputBox(LoadResEx_String(1441&), LoadResEx_String(1440&), xFindNextString)
          '  If Trim(xChar1) = "" Then
          '    Exit Do
          '    Else
    xFindNextString = xChar1
    If FindNext Then
      'Debug.Print "EndDialog!"
      'EndDialog CurrentFindHWnd, 0&
      EnableWindow MainHWnd, 1&
      Call DestroyWindow(CurrentFindHWnd)
      SetForegroundWindow MainHWnd
      SetFocusAPI MainHWnd
      If Not CurrentFindHWnd = 0& Then
        CurrentFindHWnd = 0&
        LocalFree CurrentFindHMem
        CurrentFindHMem = 0&
        DialogTimer False
        EnableWindow MainHWnd, 1&
        End If
      End If
    End If
  End Sub

Private Function FindNext() As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long


  
  If Not (xFindNextString = "" Or PLCount = 0&) Then
    xLong2 = SendMessage(xListHWnd(2), &H19F&, 0&, 0&) + 1
    Call GetListIndexMS(xListHWnd(2), xLong2)
    If xLong2 = -1& Then xLong2 = 0&
    xLong1 = xLong2
    'xLong2 = xLong2 + CurrentFindInvert
    'If xLong2 = -1& Then
    '  xLong2 = PLCount - 1&
    '  ElseIf xLong2 = PLCount Then
    '  xLong2 = 0&
    '  End If
    Do
      xLong1 = xLong1 + CurrentFindInvert
      If xLong1 = -1& Then
        xLong1 = PLCount - 1&
        ElseIf xLong1 = PLCount Then
        xLong1 = 0&
        'ElseIf xLong1 = xLong2 Then
        'Exit Do
        End If
      'vbBinaryCompare
      'vbTextCompare
#If BoldDirs = 98 Then
      If Not InStr(1&, PLI(xLong1).Caption, xFindNextString, CurrentFindMatchCase) = 0& Then
#Else
      If Not InStr(1&, PLICap(xLong1), xFindNextString, CurrentFindMatchCase) = 0& Then
#End If
        xMarkSong xLong1, False, False
        FindNext = True
        Exit Do
        ElseIf xLong1 = xLong2 Then
        Exit Do
        End If
      Loop
    End If
  End Function

Public Function StartWeb(ByVal xURL As String, ByVal NoAutoResume As Boolean) As Boolean
  Dim xBool1 As Boolean
  Dim xBool2 As Boolean
  Dim xBool3 As Boolean
  Dim xChar1 As String
  
  If CanIRadio Then
    If Not (PlayerIsLocked Or xAutoDoing) Then
      MainForm_LockMe True
      If xPStateIconPlaying Then
        xBool1 = xPStateIconPaused
        xBool2 = xGenFading And (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)
        xPStateIconPaused = False
'        If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
'          xChar1 = LastSuccessStream 'PLCurSongDisplay
'          End If
        xBool3 = (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&)
        StartWeb = xPlayItem(xGenFading, xBool2, xProcessMode = 1&, True, False, xURL)
'Debug.Print StartWeb, CurMusic, xPStateIconPlaying
        If NoAutoResume And (CurMusic = ctSSLayer_MusicTypeNone Or Not StartWeb) Then
          If CurMusic = ctSSLayer_MusicTypeNone Then
            GoTo FailPlay
            Else
            xPStateIconPlaying = True
            xPStateIconPaused = xBool1
            MainForm_UpdateMainIcon
            GoTo CannotRunIt
            End If
          End If
        If Not xPStateIconPlaying Or CurMusic = ctSSLayer_MusicTypeNone Then
          'If Not (xBool1 Or Len(xChar1) = 0&) Then
          If xBool3 And Not xBool1 Then
            'xGenFading And Not AllTears_Types(3) = 0&
            'If xPlayItem(xGenFading, xBool2, xProcessMode = 1&, True, False, xChar1) Then
            If xPlayItem(xGenFading, xBool2, xProcessMode = 1&, True, False, PLCurFullURL) Then
              If Not CurMusic = ctSSLayer_MusicTypeNone Then
                GoTo FadeIn
                End If
              End If
            End If
          'If xBool1 And NoAutoResume Then
          '  xPStateIconPaused = xBool2
          '  GoTo FailPlay
          '  End If
          xPStateIconPlaying = False
          MainForm_UpdateMainIcon
          If Not xBool1 Then
            MainForm_LockMe False
            MainForm_xPlayClick True
            End If
          Else
FadeIn:
          MainForm_UpdateMainIcon
          If (xBool2 And Tear_StepT(CurrentSongID) = 0&) Then
            Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 0&, 0&, SimpleFadingTotalTime, 1&)
            MainForm_UpdateMainIcon
            End If
          End If
        Else
        If Not NoAutoResume Then xReAutoPlay = True
'MainForm_UpdateMainIcon
        xChar1 = Mid(xURL, InStr(xURL + vbCr, vbCr) + 1&)
        PLCurSongDisplay = RealURL(xChar1)
        If Len(PLCurSongDisplay) = 0& Then
          PLCurSongDisplay = RealURL(xURL)
          If Len(PLCurSongDisplay) = 0& Then
            GoTo CannotRunIt
            End If
          End If
        xPStateIconPlaying = True
        MainForm_UpdateMainIcon
        If xPlayItem(True, False, True, True, False, xURL) Then
          If CurMusic = ctSSLayer_MusicTypeNone Then
            xPStateIconPlaying = False
            MainForm_UpdateMainIcon
            If NoAutoResume Then GoTo FailPlay
            Else
            StartWeb = (Not CurMusic = ctSSLayer_MusicTypeNone)
            End If
          Else
FailPlay:
          xPStateIconPlaying = False
          xPStateIconPaused = False
          MainForm_UpdateMainIcon
          End If
        End If
#If ProcCtl Then
      Call SendMessage(xStaticIconsHWnd(xStaticIcons_CurSongPos), 1026&, 0&, 0&)
#Else
      MainForm_EraseBackground xStaticIconsHWnd(xStaticIcons_CurSongPos)
#End If
      xEndSongTimerEventTime = DoEventsEx_GetTickCount
CannotRunIt:
      MainForm_LockMe False
      End If
    End If
  End Function

Private Function RealURL(ByVal xChar1 As String) As String
  Dim xLong1 As Long

  xLong1 = InStr(xChar1, vbCr)
  If xLong1 = 0& Then xLong1 = InStr(xChar1, vbLf)
  If xLong1 = 0& Then xLong1 = Len(xChar1) + 1&
  RealURL = Left(xChar1, xLong1 - 1&)
  End Function

Private Function GetGDU(ByVal xFont As Long) As Single
  xFont = SelectObject(xSharedDC, xFont)
  If xSharedDCOldFont = 0& Then xSharedDCOldFont = xFont
  GetGDU = GetTextOutWidth(xSharedDC, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
  GetGDU = CLng(GetDialogBaseUnits And &HFFFF&) / (2 * CLng(GetGDU / 62&))
  End Function

Public Sub ForceIRPosUpdate()
  Call ProcClassWnd(1&, &H113&, 0&, 0&)
  ShowIR_Do True      'False
  End Sub

Private Sub MainForm_UpdateMyFont() '(ByVal AlsoDefault As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xChar1 As String
  Dim xType1 As LOGFONT
  Dim xType2 As POINTAPI
  
  'Const LenNONCLIENTMETRICS_A = 325&
  'Const LenNONCLIENTMETRICS_W = 485& ' - 4?
  
  
  hFontGSys = GetStockObject(17&)
  xGlobalDialogUnits = GetGDU(hFontGSys) 'CLng(GetDialogBaseUnits And &HFFFF&) / (2 * CLng(xType1.X / 62&))
  
  
  
  If IsUnicode Then
    Call SystemParametersInfoW(31&, 61&, xType1, 0&)
    Else
    Call SystemParametersInfoA(31&, 61&, xType1, 0&)
    End If
  
  xLong3 = xType1.lfHeight
  xType1.lfHeight = -11& ' \ xGlobalDialogUnits)
  xLong1 = CreateFontIndirectEx(VarPtr(xType1))
  xGlobalDialogUnitsGFix = GetGDU(xLong1)
  Call SendMessage(xStaticIconsHWnd(xStaticIcons_Tags), &H30&, xLong1, 1&)
  Call SendMessage(xListHWnd(1), &H30&, xLong1, 1&)
  Select Case InterfaceMode
    Case Interface_Mode_Search: SearchForm_UpdateFont xLong1, 1&
    Case Interface_Mode_Timer:  TimerListForm_UpdateTimerListWinFont xLong1
    Case Interface_Mode_Radio:  RadioForm_UpdateFont xLong1, 1&
    End Select
  
  Call SelectObject(xSharedDC, xSharedDCOldFont)
  If Not hFontGFix = 0& Then Call DeleteObject(hFontGFix)
  hFontGFix = xLong1
  If InterfaceMode = Interface_Mode_Timer Then TimerListForm_ReDetectWidth

  
  xLastPathCurMaxWidth = 0&
  Call SelectObject(xSharedDC, xLong1)
  For xLong2 = 0& To xLastPathCount - 1& 'maxx
    xLastPathLen(xLong2) = GetTextOutWidth(xSharedDC, xLastPathCap(xLong2))
    If xLastPathLen(xLong2) > xLastPathCurMaxWidth Then xLastPathCurMaxWidth = xLastPathLen(xLong2)
    Next
  
  xLastPathTimeSpace = 0&
  For xLong1 = 0& To 9&
    xChar1 = " " + String(2, Chr(xLong1 + 48&)) + ":" + String(2, Chr(xLong1 + 48&)) + ":" + String(4, Chr(xLong1 + 48&))
    xLong2 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong2 > xLastPathTimeSpace Then
      xLastPathTimeSpace = xLong2 'some space!
      End If
    Next
  
  'xLongA1(0) = xLastPathTimeSpace * xGlobalDialogUnitsGFix
  'xLongA1(1) = (xLastPathTimeSpace + xLastPathCurMaxWidth) * xGlobalDialogUnitsGFix '* 4
  'Call SendMessage(xListHWnd(1), &H192&, 2&, VarPtr(xLongA1(0)))
  Call SendMessage(xListHWnd(1), &H194&, (32& + xLastPathTimeSpace + xLastPathCurMaxWidth), 0&)
  
  ReCreateIRFont
  
  
  
  xType1.lfHeight = xLong3
  xLong4 = xType1.lfWeight
  If xType1.lfWeight > 499& Then
    xType1.lfWeight = 900&
    ElseIf xType1.lfWeight < 300& Then
    xType1.lfWeight = 600&
    Else
    xType1.lfWeight = xType1.lfWeight + 300&
    End If
  xLong1 = CreateFontIndirectEx(VarPtr(xType1))
  Call SendMessage(xListHWnd(2), &H30&, xLong1, 1&)
  If InterfaceMode = Interface_Mode_Search Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H30&, xLong1, 1&)
    'ElseIf InterfaceMode = Interface_Mode_Radio Then
    'Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H30&, xLong1, 1&)
    End If
  If Not hFontPLLgB = 0& Then Call DeleteObject(hFontPLLgB)
  hFontPLLgB = xLong1

  xType1.lfWeight = xType1.lfWeight - 300&
  xType1.lfHeight = xLong3
  xLong1 = CreateFontIndirectEx(VarPtr(xType1))
  If InterfaceMode = Interface_Mode_Radio Then
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H30&, xLong1, 1&)
    End If
  If Not hFontPLLg = 0& Then Call DeleteObject(hFontPLLg)
  hFontPLLg = xLong1



  xType1.lfWeight = xLong4
'  xType1.lfHeight = Int(21& / xGlobalDialogUnits)
  xType1.lfHeight = -11& ' \ xGlobalDialogUnits)
  xLong1 = CreateFontIndirectEx(VarPtr(xType1))
  Call SendMessage(xListHWnd(0), &H30&, xLong1, 1&)
  If Not hFontPLSm = 0& Then Call DeleteObject(hFontPLSm)
  hFontPLSm = xLong1
  xGlobalDialogUnitsPLSm = GetGDU(xLong1)
  
  
  
  
  
  Call SelectObject(xSharedDC, hFontPLLgB)
  xLong1 = Len(DefTxt)
  Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(DefTxt), xLong1, xType2)
  PLODHeight = xType2.y
  If PLODHeight < 15& Then
    PLODHeight = 17&
    'Else
    'PLODHeight = PLODHeight + 4&
    End If
  PLODIconTopShift = (PLODHeight - 16) \ 2&
  Call SendMessage(xListHWnd(2), &H1A0&, 0&, PLODHeight)
  If InterfaceMode = Interface_Mode_Search Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H1A0&, 0&, PLODHeight)
    ElseIf InterfaceMode = Interface_Mode_Radio Then
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H1A0&, 0&, PLODHeight)
    End If
  
  Call SelectObject(xSharedDC, hFontGFix)
  
  
  
  
' 500& / 340&
  xLong1 = ptrTemp
  If Not hFontTTip = 0& Then
    Call DeleteObject(hFontTTip)
    hFontTTip = 0&
    End If
  If IsUnicode Then
    Call PokeL(ptrTemp, 500&)
    If Not SystemParametersInfoW(41&, 500&, ByVal ptrTemp, 0&) = 0& Then
      'Const SizeW_FontStruct As Long = 5& * 4& + 8& + 64&
      'Const SizeW_OffsetTT As Long = 10& * 4& + 3 * SizeW_FontStruct
      ' 316
      Call UIntAddUInt(xLong1, 316&)
      hFontTTip = CreateFontIndirectEx(xLong1)
      End If
    Else
    Call PokeL(ptrTemp, 340&)
    If Not SystemParametersInfoA(41&, 340&, ByVal ptrTemp, 0&) = 0& Then
      'Const SizeA_FontStruct As Long = 5& * 4& + 8& + 32&
      'Const SizeA_OffsetTT As Long = 10& * 4& + 3 * SizeA_FontStruct
      ' 220
      Call UIntAddUInt(xLong1, 220&)
      hFontTTip = CreateFontIndirectEx(xLong1)
      End If
    End If
  
  Call SelectObject(xSharedDC, hFontTTip)
  xChar1 = LoadResEx_String(1100&)
  CurShownToolTip_SpKeyPre = GetTextOutWidth(xSharedDC, xChar1) + TTSpaceLeft
  xChar1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  Call GetTextExtentPoint32Wide(xSharedDC, StrPtr(xChar1), 62&, xType2)
  CurShownToolTip_SpHeight = xType2.y
  
  ' 11 long, 4 logfont
  

  
  

  
  If InterfaceMode = Interface_Mode_Search Then
    MainForm_TrySearch False
    ElseIf InterfaceMode = Interface_Mode_Radio Then
    RadioForm_DetectListWidth
    End If
  
  Call SelectObject(xSharedDC, hFontGFix)
  PLCurSongDisplaySpacePerc = GetTimeFmtMaxPerc
  
  If Not CurMusic = ctSSLayer_MusicTypeNone Then
    If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
      PLCurSongDisplaySpace = PLCurSongDisplaySpacePerc
      Else
      PLCurSongDisplaySpace = GetTimeFmtMax
      End If
    Call GetTimeFmt(xSongIsMidi = 3&)
    End If
  MainForm_ReGetWidth
  MainForm_xBuildDirs
  End Sub

Public Sub ReCreateIRFont()
  Dim xLong1 As Long
  'Dim xLong2 As Long
  Dim xType1 As LOGFONT
  Dim xType2 As TEXTMETRIC

  If IsUnicode Then
    Call SystemParametersInfoW(31&, 61&, xType1, 0&)
    Else
    Call SystemParametersInfoA(31&, 61&, xType1, 0&)
    End If

  xType1.lfHeight = Int(21& / xGlobalDialogUnits)
  hFontShIRIsVerticalBitmapFont = False
  If xTitlesPos = 1& Or xTitlesPos = 2& Then
    xType1.lfEscapement = 900&
    xType1.lfOrientation = 900&
    'xType1.lfOutPrecision = 7&
    xLong1 = CreateFontIndirectEx(VarPtr(xType1))
    If Not xLong1 = 0& Then
      If Not SelectObject(xSharedDC, xLong1) = 0& Then
        If GetTextMetricsA(xSharedDC, xType2) = 0& Then
          Call SelectObject(xSharedDC, hFontGFix)
          Else
          Call SelectObject(xSharedDC, hFontGFix)
          If (xType2.tmPitchAndFamilyA And &H6&) = 0& Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "Bitmap font; Cannot be used vertically"
            #End If
            hFontShIRIsVerticalBitmapFont = True
            End If
          End If
        End If
      End If
    Else
    xLong1 = CreateFontIndirectEx(VarPtr(xType1))
    End If
  If Not hFontShIR = 0& Then Call DeleteObject(hFontShIR)
  hFontShIR = xLong1
  End Sub

Private Sub MessageHandler()
  Dim xLong1 As Long
  'Dim hwnd As Long
  Dim xMsg As Msg
  
  IsMessageing = True
  If IsUnicode Then
    xLong1 = GetMessageW(xMsg, 0&, 0&, 0&)
    Do Until (xLong1 = 0&)
      If xLong1 = -1& Then
        ' handle the error and possibly exit
        ElseIf CurrentFindHWnd = 0& Then
          'If Not TranslateAccelerator(hwnd, haccel, xMsg) Then
            Call TranslateMessage(xMsg)
            Call DispatchMessageW(xMsg)
          '  End If
        ElseIf IsDialogMessageW(CurrentFindHWnd, xMsg) = 0& Then
        'If Not TranslateAccelerator(hwnd, haccel, xMsg) Then
          Call TranslateMessage(xMsg)
          Call DispatchMessageW(xMsg)
        '  End If
        End If
      #If XPDeviceRemoveDetect And XPDeviceRemoveDetectPST Then
        If PendingDeviceRegB Then
          If DoEventsEx_IsTickCountElapsed(PendingDeviceRegT, 2500&) Then
            Call RemovableDevices_UnregisterAll
            Call RemovableDevices_RegisterAll(MainHWnd)
            End If
          PendingDeviceRegB = False
          End If
        #End If
      xLong1 = GetMessageW(xMsg, 0&, 0&, 0&)
      Loop
    Else
    xLong1 = GetMessageA(xMsg, 0&, 0&, 0&)
    Do Until (xLong1 = 0&)
      If xLong1 = -1& Then
        ' handle the error and possibly exit
        ElseIf CurrentFindHWnd = 0& Then
          'If Not TranslateAccelerator(hwnd, haccel, xMsg) Then
            Call TranslateMessage(xMsg)
            Call DispatchMessageA(xMsg)
          '  End If
        ElseIf IsDialogMessageA(CurrentFindHWnd, xMsg) = 0& Then
        'If Not TranslateAccelerator(hwnd, haccel, xMsg) Then
          Call TranslateMessage(xMsg)
          Call DispatchMessageA(xMsg)
        '  End If
        End If
      xLong1 = GetMessageA(xMsg, 0&, 0&, 0&)
      Loop
    End If
  IsMessageing = False
  End Sub

Private Function NotOutOfSession() As Boolean
  Dim xLong1 As Long
  
  On Error GoTo Er1
  xLong1 = WTSGetActiveConsoleSessionId
  If Not xLong1 = -1& Then NotOutOfSessionOld = (WTSGetActiveConsoleSessionId = OutOfSession_MySessionID)
  NotOutOfSession = NotOutOfSessionOld
Rs1:
  On Error GoTo 0
  Exit Function
  
Er1:
  NotOutOfSession = True
  NotOutOfSessionOld = True
  Resume Rs1
  End Function

Private Sub InitOutOfSession()
  On Error GoTo Er1
  If ProcessIdToSessionId(GetCurrentProcessId, ByVal VarPtr(OutOfSession_MySessionID)) = 0& Then
    OutOfSession_MySessionID = -1&
    End If
Rs1:
  On Error GoTo 0
  Exit Sub
  
Er1:
  OutOfSession_MySessionID = -1&
  Resume Rs1
  End Sub

Private Function RegReadString(ByVal uRGSection As String, Optional ByVal uRGName As String) As String
  Dim xLong1 As Long

  If RegOpenKeyEx(&H80000002, uRGSection, 1&, xLong1) Then
    Call RegQueryValueEx_String(xLong1, uRGName, RegReadString)
    RegCloseKey (xLong1)
    End If
  End Function

Private Sub MainForm_AdvanceSong(ByVal TabMode As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long

'0 updir
'1 any
'2 any inv
'3 same
'4 same inv

'#If 0 Then

  If Not PlayerIsLocked Or xAutoDoing Then
  'If SendMessage(xListHWnd(2), &H190&, 0&, 0&) = 1& Then
    'xLong1 = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
    Call GetListIndexMS(xListHWnd(2), xLong1)
    If xLong1 = -1& Then xLong1 = 0&
      
    Select Case TabMode
      Case 0&
        
        #If BoldDirs = 98 Then
          xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth) - 2& ' Len(PLCap(xLong1)) - Len(LTrim(PLCap(xLong1))) - 4&
          #Else
          xLong2 = (PLIDF(xLong1) And PLI_Depth) - 2& ' Len(PLCap(xLong1)) - Len(LTrim(PLCap(xLong1))) - 4&
          #End If
        #If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          #Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          #End If
        If Not xLong2 < 0& Then
          For xLong1 = xLong1 - 1& To 0& Step -1&
#If BoldDirs = 98 Then
            If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
              If (PLI(xLong1).DirDepthFlags And PLI_Depth) = xLong2 Then
#Else
            If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
              If (PLIDF(xLong1) And PLI_Depth) = xLong2 Then
#End If
                xMarkSong xLong1, False, False
                Exit For
                End If
              End If
            Next
          End If
      Case 1&
        #If BoldDirs = 98 Then
          xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth)
          #Else
          xLong2 = (PLIDF(xLong1) And PLI_Depth)
          #End If
        Do Until xLong1 = PLCount - 1&
          xLong3 = xLong1 + 1&
#If BoldDirs = 98 Then
          If (PLI(xLong3).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(xLong3).DirDepthFlags And PLI_Depth) > xLong2 Then
#Else
          If (PLIDF(xLong3) And PLI_IsDir) = PLI_IsDir And (PLIDF(xLong3) And PLI_Depth) > xLong2 Then
#End If
            xLong1 = xLong3
            Else
            Exit Do
            End If
          Loop
        For xLong1 = xLong1 + 1& To PLCount - 1&
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
#End If
            Do Until xLong1 = PLCount - 1&
              xLong3 = xLong1 + 1&
#If BoldDirs = 98 Then
              If (PLI(xLong3).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(xLong3).DirDepthFlags And PLI_Depth) > xLong2 Then
#Else
              If (PLIDF(xLong3) And PLI_IsDir) = PLI_IsDir And (PLIDF(xLong3) And PLI_Depth) > xLong2 Then
#End If
                xLong1 = xLong3
                Else
                Exit Do
                End If
              Loop
            xMarkSong xLong1, False, False
            Exit For
            End If
          Next
      Case 2&
        #If BoldDirs = 98 Then
          xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth)
          #Else
          xLong2 = (PLIDF(xLong1) And PLI_Depth)
          #End If
        Do Until xLong1 = 0&
          xLong3 = xLong1 - 1&
#If BoldDirs = 98 Then
          If (PLI(xLong3).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(xLong3).DirDepthFlags And PLI_Depth) < xLong2 Then
#Else
          If (PLIDF(xLong3) And PLI_IsDir) = PLI_IsDir And (PLIDF(xLong3) And PLI_Depth) < xLong2 Then
#End If
            xLong1 = xLong3
            Else
            Exit Do
            End If
          Loop
        For xLong1 = xLong1 - 1& To 0& Step -1&
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
#End If
            Do Until xLong1 = 0&
              xLong3 = xLong1 - 1&
#If BoldDirs = 98 Then
              If (PLI(xLong3).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(xLong3).DirDepthFlags And PLI_Depth) < xLong2 Then
#Else
              If (PLIDF(xLong3) And PLI_IsDir) = PLI_IsDir And (PLIDF(xLong3) And PLI_Depth) < xLong2 Then
#End If
                xLong1 = xLong3
                Else
                Exit Do
                End If
              Loop
            xMarkSong xLong1, False, False
            Exit For
            End If
          Next
      
      Case 3&
        #If BoldDirs = 98 Then
          'xLong2 = (PLIDF(xLong1) And PLI_Depth) - 1&
          'If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth)
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then xLong2 = xLong2 - 1&
          #Else
          'xLong2 = (PLIDF(xLong1) And PLI_Depth) - 1&
          'If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          xLong2 = (PLIDF(xLong1) And PLI_Depth)
          If (PLIDF(xLong1) And PLI_IsDir) = 0& Then xLong2 = xLong2 - 1&
          #End If
        For xLong1 = xLong1 + 1& To PLCount - 1&
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
            Select Case (PLI(xLong1).DirDepthFlags And PLI_Depth)
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
            Select Case (PLIDF(xLong1) And PLI_Depth)
#End If
              Case xLong2
                xMarkSong xLong1, False, False
                Exit For
              Case Is < xLong2
                Exit For
              End Select
            End If
          Next
      Case 4&
        #If BoldDirs = 98 Then
          'xLong2 = (PLIDF(xLong1) And PLI_Depth) - 1&
          'If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth)
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Then xLong2 = xLong2 - 1&
          #Else
          'xLong2 = (PLIDF(xLong1) And PLI_Depth) - 1&
          'If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then xLong2 = xLong2 + 1&
          xLong2 = (PLIDF(xLong1) And PLI_Depth)
          If (PLIDF(xLong1) And PLI_IsDir) = 0& Then xLong2 = xLong2 - 1&
          #End If
        For xLong1 = xLong1 - 1& To 0& Step -1&
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
            Select Case (PLI(xLong1).DirDepthFlags And PLI_Depth)
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
            Select Case (PLIDF(xLong1) And PLI_Depth)
#End If
              Case xLong2
                xMarkSong xLong1, False, False
                Exit For
              Case Is < xLong2
                Exit For
              End Select
            End If
          Next
      End Select
    End If
  'End If
  
'#End If
  End Sub

Private Sub CheckOldItems()
  Dim xLong1 As Long
  
  #If UseFileLogging Then
    Dim xLong2 As Long
  '  MainForm_ctConsoleA_AddLog "CheckOldItems procedur"
  '  xLong2 = PLCurItem
    #End If
  
  If Not PLCurItem < PLCount Then PLCurItem = 0&
  If Not PLNext < PLCount Then PLNext = 0&
  
  If Not PLCount = 0& Then
#If BoldDirs = 98 Then
    If Not UCase(PLI(PLCurItem).Filename) = PLCurSong Then
#Else
    If Not UCase(PLIFile(PLCurItem)) = PLCurSong Then
#End If
      #If UseFileLogging Then
        xLong2 = PLCurItem
        #End If
      xLong1 = xFindSong
      If Not xLong1 = 0& Then
        PLCurItem = xLong1
        End If
      If Not PLCurItem = 0& Then
        Do
#If BoldDirs = 98 Then
          If (PLI(PLCurItem).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
          If (PLIDF(PLCurItem) And PLI_IsDir) = PLI_IsDir Then
#End If
            PLCurItem = PLCurItem + 1&
            Else
            Exit Do
            End If
          Loop
        End If
      #If UseFileLogging Then
        If Not xLong2 = PLCurItem Then
          MainForm_ctConsoleA_AddLog "CheckOldItems procedur (PLCurItem: " & CStr(xLong2) & " -> " & CStr(PLCurItem) & ")"
          End If
        #End If
      End If
    If Not PlNextSong = "" Then
#If BoldDirs = 98 Then
      If Not UCase(PLI(PLNext).Filename) = PlNextSong Then
#Else
      If Not UCase(PLIFile(PLNext)) = PlNextSong Then
#End If
        #If UseFileLogging Then
          xLong2 = PLNext
          #End If
        PLNext = xFindSong(PlNextSong)
        #If UseFileLogging Then
          If Not xLong2 = PLCurItem Then
            MainForm_ctConsoleA_AddLog "CheckOldItems procedur (PLNext: " & CStr(xLong2) & " -> " & CStr(PLNext) & ")"
            End If
          #End If
        If PLNext = 0& Then SetNextSong False
        End If
      End If
    End If
  End Sub

Public Sub xRemoveItems(ByVal NoSubDirs As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xCaret As Long
  Dim xCaretOld As Long

  MainForm_LockMe True
'Dim xCmp As Long
'xCmp = DoEventsEx_GetTickCount

  'If PLCount = 0& Or xAutoDoing Then Exit Sub
  'If SendMessage(xListHWnd(2), &H190&, 0&, 0&) < 0& Then Exit Sub
  If Not (PLCount = 0& Or xAutoDoing) Then
    If Not SendMessage(xListHWnd(2), &H190&, 0&, 0&) = 0 Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Removing items"
        #End If
      xLong1 = -1&
      xLong4 = PLCount - 1&
      PLCount = 0&
      
      If Not LockWinUpd = 0& Then
        LockWindowUpdate xListHWnd(2)
        End If
      
      xCaret = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
      xCaretOld = xCaret
      
      xHiddenAutoDoing = True
      SetAutoDoing True
      Do Until xLong1 = xLong4
        xLong1 = xLong1 + 1&
        If xLong1 = PLCurItem Then PLCurItem = PLCount
        If SendMessage(xListHWnd(2), &H187&, PLCount, 0&) Then
          'If Left(PLItem(xLong1), 1&) = Chr(1&) Then
          If xLong1 = PLCurItem Then PLCurItem = PLCount
          'If xLong1 = PLNext And Not PlNextSong = "" Then PLNext = xLong1
#If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
#End If
            If NoSubDirs Then
              #If BoldDirs = 98 Then
                PLI(PLCount) = PLI(xLong1)
                Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
                #Else
                PLIFile(PLCount) = PLIFile(xLong1)
                PLICap(PLCount) = PLICap(xLong1)
                PLIDF(PLCount) = PLIDF(xLong1)
                PLIPxLg(PLCount) = PLIPxLg(xLong1)
                PLIPxSm(PLCount) = PLIPxSm(xLong1)
                Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
                #End If
              'If xLong1 = PLCurItem Then PLCurItem = PLCount
              If xLong1 = PLNext And Not PlNextSong = "" Then PLNext = xLong1
              PLCount = PLCount + 1&
              Else
              #If BoldDirs = 98 Then
                xLong2 = (PLI(xLong1).DirDepthFlags And PLI_Depth) + 1&
                #Else
                xLong2 = (PLIDF(xLong1) And PLI_Depth) + 1&
                #End If
              If xLong1 = PLNext And Not PlNextSong = "" Then PlNextSong = "": PlNextSongCap = ""
              'RemoveListItemMS
              RemoveFromSearch PLCount
              'PLRemoveShifted1 = PLCount
              'PLRemoveShifted2 = xLong1
'Debug.Print "ShiftMode: 1"
              Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
              If xCaret >= PLCount Then xCaret = xCaret - 1&
              If LockWinUpd = 0& Then
                DoEventsEx_SleepPeriodical
                ElseIf LockWinUpd = 1& Then
                DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                End If
              Do Until xLong1 = xLong4
#If BoldDirs = 98 Then
                If (PLI(xLong1 + 1&).DirDepthFlags And PLI_Depth) >= xLong2 Then
#Else
                If (PLIDF(xLong1 + 1&) And PLI_Depth) >= xLong2 Then
#End If
                  If xLong1 = PLCurItem Then PLCurItem = PLCount
                  If xLong1 = PLNext And Not PlNextSong = "" Then PlNextSong = "": PlNextSongCap = ""
                  'RemoveListItemMS
                  RemoveFromSearch PLCount
'Debug.Print "ShiftMode: 2"
                  'PLRemoveShifted1 = PLCount
                  'PLRemoveShifted2 = xLong1
                  Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
                  If xCaret >= PLCount Then xCaret = xCaret - 1&
                  xLong1 = xLong1 + 1&
                  If xLong1 Mod 64& = 0& Then
                    If LockWinUpd = 0& Then
                      DoEventsEx_SleepPeriodical
                      ElseIf LockWinUpd = 1& Then
                      DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                      End If
                    End If
                  Else
                  Exit Do
                  End If
                Loop
              xLong2 = xLong2 - 2&
              End If
            Else
            'RemoveListItemMS
            RemoveFromSearch PLCount
'Debug.Print "ShiftMode: 3"
            'PLRemoveShifted1 = PLCount
            'PLRemoveShifted2 = xLong1
            Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
            If xCaret >= PLCount Then xCaret = xCaret - 1&
            If LockWinUpd = 0& Then
              DoEventsEx_SleepPeriodical
              ElseIf LockWinUpd = 1& Then
              DoEventsEx_SleepPeriodicalLW xListHWnd(2)
              End If
            If xLong1 = PLNext And Not PlNextSong = "" Then PlNextSong = "": PlNextSongCap = ""
            End If
          Else
          #If BoldDirs = 98 Then
            PLI(PLCount) = PLI(xLong1)
            Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
            #Else
            PLIFile(PLCount) = PLIFile(xLong1)
            PLICap(PLCount) = PLICap(xLong1)
            PLIDF(PLCount) = PLIDF(xLong1)
            PLIPxLg(PLCount) = PLIPxLg(xLong1)
            PLIPxSm(PLCount) = PLIPxSm(xLong1)
            Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
            #End If
          If xLong1 = PLNext And Not PlNextSong = "" Then PLNext = xLong1
          PLCount = PLCount + 1&
          End If
        Loop
      'PLRemoveShifted1 = &H7FFFFFFF
      SetAutoDoing False
      xHiddenAutoDoing = False

      If Not (PLCount = 0& Or xCaret = xCaretOld) Then
        If xCaret > PLCount - 1& Then xCaret = PLCount - 1&
        If xCaret < 0& Then xCaret = 0&
        Call SendMessage(xListHWnd(2), &H19E&, xCaret, 0&)
        End If
      
      CheckEmptyNodes False, -1&
      CheckOldItems
      End If
    End If
  
'Debug.Print "Removed " + CStr(PLCount) + " items in " + CStr(DoEventsEx_GetElapsedTime(xCmp)) + "ms"
  MainForm_LockMe False
  End Sub

Private Sub CheckEmptyNodes(ByVal NoUpdateList As Boolean, ByVal UpdateBeginn As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xCaret As Long
  Dim xCaretOld As Long
  
  If Not LockWinUpd = 0& Then
    If Not NoUpdateList Then LockWindowUpdate xListHWnd(2)
    End If
  
  xCaret = SendMessage(xListHWnd(2), &H19F&, 0&, 0&)
  xCaretOld = xCaret
  
#If 0 Then
  If UpdateBeginn = -1& Then
#Else
  UpdateBeginn = -1&
#End If
  'If NoUpdateList Then
    'If UpdateBeginn = -1& Then
    'xLong1 = -1&
    'xLong2 = PLCount - 1&
    'PLCount = 0&
    xLong1 = UpdateBeginn
    xLong2 = PLCount - 1&
    PLCount = UpdateBeginn + 1&
    Do Until xLong1 = xLong2
      xLong1 = xLong1 + 1&
      'For xLong1 = PLCount - 1& To 0& Step -1&
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
      If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
#End If
        If xLong1 = xLong2 Then
          Do Until PLCount = 0&
#If BoldDirs = 98 Then
            If (PLI(PLCount - 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
            If (PLIDF(PLCount - 1&) And PLI_IsDir) = PLI_IsDir Then
#End If
              PLCount = PLCount - 1&
              If Not NoUpdateList Then
                'RemoveListItemMS
                RemoveFromSearch PLCount
                'PLRemoveShifted1 = PLCount
                'PLRemoveShifted2 = xLong1
                Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
                If xCaret >= PLCount Then xCaret = xCaret - 1&
                If LockWinUpd = 0& Then
                  DoEventsEx_SleepPeriodical
                  ElseIf LockWinUpd = 1& Then
                  DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                  End If
                End If
              Else
              Exit Do
              End If
            Loop
          If Not NoUpdateList Then
            'RemoveListItemMS
            RemoveFromSearch PLCount
            'PLRemoveShifted1 = PLCount
            'PLRemoveShifted2 = xLong1
            Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
            If xCaret >= PLCount Then xCaret = xCaret - 1&
            If LockWinUpd = 0& Then
              DoEventsEx_SleepPeriodical
              ElseIf LockWinUpd = 1& Then
              DoEventsEx_SleepPeriodicalLW xListHWnd(2)
              End If
            End If
          Exit Do
          Else
          #If BoldDirs = 98 Then
            PLI(PLCount) = PLI(xLong1)
            Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
            #Else
            PLIFile(PLCount) = PLIFile(xLong1)
            PLICap(PLCount) = PLICap(xLong1)
            PLIDF(PLCount) = PLIDF(xLong1)
            PLIPxLg(PLCount) = PLIPxLg(xLong1)
            PLIPxSm(PLCount) = PLIPxSm(xLong1)
            Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
            #End If
          PLCount = PLCount + 1&
          Do Until PLCount = 0&
#If BoldDirs = 98 Then
            If (PLI(xLong1 + 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(PLCount - 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
              If (PLI(PLCount - 1&).DirDepthFlags And PLI_Depth) < (PLI(xLong1 + 1&).DirDepthFlags And PLI_Depth) Then
#Else
            If (PLIDF(xLong1 + 1&) And PLI_IsDir) = PLI_IsDir And (PLIDF(PLCount - 1&) And PLI_IsDir) = PLI_IsDir Then
              If (PLIDF(PLCount - 1&) And PLI_Depth) < (PLIDF(xLong1 + 1&) And PLI_Depth) Then
#End If
                Exit Do
                Else
                PLCount = PLCount - 1&
                If Not NoUpdateList Then
                  'RemoveListItemMS
                  RemoveFromSearch PLCount
                  'PLRemoveShifted1 = PLCount
                  'PLRemoveShifted2 = xLong1
                  Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
                  If xCaret >= PLCount Then xCaret = xCaret - 1&
                  If LockWinUpd = 0& Then
                    DoEventsEx_SleepPeriodical
                    ElseIf LockWinUpd = 1& Then
                    DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                    End If
                  End If
                End If
              Else
              Exit Do
              End If
            Loop
          End If
        Else
        #If BoldDirs = 98 Then
          PLI(PLCount) = PLI(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
          #Else
          PLIFile(PLCount) = PLIFile(xLong1)
          PLICap(PLCount) = PLICap(xLong1)
          PLIDF(PLCount) = PLIDF(xLong1)
          PLIPxLg(PLCount) = PLIPxLg(xLong1)
          PLIPxSm(PLCount) = PLIPxSm(xLong1)
          Call SendMessage(xListHWnd(2), &H19A&, PLCount, PLCount)
          #End If
        If PLCurItem = xLong1 Then PLCurItem = PLCount
        If PLNext = xLong1 Then PLNext = PLCount
        PLCount = PLCount + 1&
        End If
      Loop
    'PLRemoveShifted1 = &H7FFFFFFF
#If 0 Then
    Else
    'UpdateBeginn = UpdateBeginn - 1&
    If Not UpdateBeginn < PLCount Then UpdateBeginn = PLCount - 1&
    If UpdateBeginn = PLCount - 1& Then
      xLong1 = PLCount
      Do Until PLCount = 0&
#If BoldDirs = 98 Then
        If (PLI(PLCount - 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
        If (PLIDF(PLCount - 1&) And PLI_IsDir) = PLI_IsDir Then
#End If
          PLCount = PLCount - 1&
          If Not NoUpdateList Then
            'RemoveListItemMS
            RemoveFromSearch PLCount
            'PLRemoveShifted1 = PLCount
            'PLRemoveShifted2 = xLong1
            Call SendMessage(xListHWnd(2), &H182&, PLCount, 0&)
            If xCaret >= PLCount Then xCaret = xCaret - 1&
            If LockWinUpd = 0& Then
              DoEventsEx_SleepPeriodical
              ElseIf LockWinUpd = 1& Then
              DoEventsEx_SleepPeriodicalLW xListHWnd(2)
              End If
            End If
          Else
          Exit Do
          End If
        Loop
      'PLRemoveShifted1 = &H7FFFFFFF
      Else 'If Not UpdateBeginn = 0& Then
'      UpdateBeginn = UpdateBeginn + 1&
#If BoldDirs = 98 Then
      If (PLI(UpdateBeginn).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
#Else
      If (PLIDF(UpdateBeginn) And PLI_IsDir) = PLI_IsDir Then
#End If
        xLong2 = UpdateBeginn
        #If BoldDirs = 98 Then
          xLong1 = (PLI(xLong2).DirDepthFlags And PLI_Depth)
          #Else
          xLong1 = (PLIDF(xLong2) And PLI_Depth)
          #End If
        Do Until xLong2 = 0&
#If BoldDirs = 98 Then
          If (PLI(xLong2 - 1&).DirDepthFlags And PLI_IsDir) = PLI_IsDir And (PLI(xLong2 - 1&).DirDepthFlags And PLI_Depth) >= xLong1 Then
#Else
          If (PLIDF(xLong2 - 1&) And PLI_IsDir) = PLI_IsDir And (PLIDF(xLong2 - 1&) And PLI_Depth) >= xLong1 Then
#End If
            xLong2 = xLong2 - 1&
'            If PLNext > xLong2 Then PLNext = PLNext - 1&
'            If PLCurItem > xLong2 Then PLCurItem = PLCurItem - 1&
            Else
            Exit Do
            End If
          Loop
        xLong3 = UpdateBeginn - xLong2
        If Not xLong3 = 0& Then
          If PLNext >= xLong2 Then PLNext = PLNext - xLong3
          If PLCurItem > xLong2 Then PLCurItem = PLCurItem - xLong3
          If Not NoUpdateList Then
            For xLong1 = 1& To xLong3
              'RemoveListItemMS
              RemoveFromSearch xLong2
              'PLRemoveShifted1 = xLong2
              'PLRemoveShifted2 = xLong1 + xLong3
              Call SendMessage(xListHWnd(2), &H182&, xLong2, 0&)
              If xCaret >= xLong2 Then xCaret = xCaret - 1&
              If LockWinUpd = 0& Then
                If xLong1 Mod 64& = 0& Then DoEventsEx_SleepPeriodical
                ElseIf LockWinUpd = 1& Then
                DoEventsEx_SleepPeriodicalLW xListHWnd(2)
                End If
              Next
            'PLRemoveShifted1 = &H7FFFFFFF
            End If
          'For xLong1 = xLong2 To PLCount - 1& - (UpdateBeginn - xLong2 - 1&)
          For xLong1 = xLong2 To PLCount - xLong3 - 1&
            #If BoldDirs = 98 Then
              PLI(xLong1) = PLI(xLong1 + xLong3)
              Call SendMessage(xListHWnd(2), &H19A&, xLong1, xLong1)
              #Else
              PLIFile(xLong1) = PLIFile(xLong1 + xLong3)
              PLICap(xLong1) = PLICap(xLong1 + xLong3)
              PLIDF(xLong1) = PLIDF(xLong1 + xLong3)
              PLIPxLg(xLong1) = PLIPxLg(xLong1 + xLong3)
              PLIPxSm(xLong1) = PLIPxSm(xLong1 + xLong3)
              Call SendMessage(xListHWnd(2), &H19A&, xLong1, xLong1)
              #End If
            Next
          'If Not NoUpdateList Then If Not CheckSync Then Stop
          PLCount = PLCount - xLong3
          #If UseFileLogging Then
            If Not NoUpdateList Then CheckSync
            #End If
          End If
        End If
      End If
    End If
#End If
  
  If Not (PLCount = 0& Or xCaret = xCaretOld) Then
    If xCaret > PLCount - 1& Then xCaret = PLCount - 1&
    If xCaret < 0& Then xCaret = 0&
    Call SendMessage(xListHWnd(2), &H19E&, xCaret, 0&)
    End If
  
  ' Always required as this func. is called after items are removed,
  ' but not checked before because this func. also may have to do...
  ' -> This func. Always has to do !!!
  If Not NoUpdateList Then
    'LockWindowUpdate 0&
    MainForm_xBuildDirs
    End If
  
  #If UseFileLogging Then
    If Not NoUpdateList Then CheckSync
    #End If
  End Sub

Private Sub InfoProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
  Static xSingleA1(ctSSLayer_SpectrumArrayCountX) As Single
  Static xLong1 As Long
  Static xLong3 As Long
  Static xSingle1 As Single
  Static xSingle2 As Single
  Static xLong4 As Long
  Static xLong5 As Long
  Static xLong6 As Long
  Static xLong7 As Long
  Dim xhPen As Long
  Dim xhPenOld As Long
  Dim hBmp As Long
  Dim hBmpOld As Long
  Dim xType1 As RECT
  Dim xHDC As Long
  
  If Not (xTimer1 Or xTimer3) Then
    xTimer1 = True
    xHDC = GetDC(xStaticIconsHWnd(xStaticIcons_Spectrum))
    If Not xHDC = 0& Then
      If CurMusic = ctSSLayer_MusicTypeNone Then
        Call ctSSLayer_GetSpectrum(xSingleA1(0), ctSSLayer_SpectrumArrayCount, 0&, False)
        Else
        Call ctSSLayer_GetSpectrum(xSingleA1(0), ctSSLayer_SpectrumArrayCount, SongPointer(CurrentSongID), False)
        End If
      For xLong1 = 0& To ctSSLayer_SpectrumArrayCountX
        xSingleA1(xLong1) = xSingleA1(xLong1) / (1 / (xLong1 + 1&)) '* 1.5  '* 0.4
        If xSingleA1(xLong1) > 1 Then xSingleA1(xLong1) = 1
        Next
      
      
      
      hBmp = CreateCompatibleBitmapX(128&, 22&)
      If Not hBmp = 0& Then
        hBmpOld = SelectObject(xSharedDC, hBmp)
        #If Not NoClearSpectrum Then
          xType1.Bottom = 21&
          xType1.Right = 128&
          Call FillRect(xSharedDC, xType1, FillSysColorBack)
          #End If
        xhPen = CreatePen(0&, 0&, FLineColor)
        If Not xhPen = 0& Then
          xhPenOld = SelectObject(xSharedDC, xhPen)
          If xDeluxeSpectrum Then
            xLong3 = GetTickCount And &HFFF&
            xSingle1 = Cos(xLong3 * 3.1415926536 / &H1000&)
            xSingle2 = Sin(xLong3 * 3.1415926536 / &H1000&)
            Call MoveToEx(xSharedDC, 16, 10, 0&)
            Call LineTo(xSharedDC, 112, 10)
            Call MoveToEx(xSharedDC, 13 - xSingle1 * 10, 10 - xSingle2 * 10, 0&)
            Call LineTo(xSharedDC, 13 + xSingle1 * 10, 10 + xSingle2 * 10)
            Call LineTo(xSharedDC, 114 + xSingle1 * 10, 10 + xSingle2 * 10)
            Call LineTo(xSharedDC, 114 - xSingle1 * 10, 10 - xSingle2 * 10)
            Call LineTo(xSharedDC, 13 - xSingle1 * 10, 10 - xSingle2 * 10)
            For xLong1 = 0& To ctSSLayer_SpectrumArrayCountX
              xLong5 = xSingle1 * xSingleA1(xLong1) * 8&
              xLong4 = 16& + xLong1 * 3& - xLong5
              xLong5 = 16& + xLong1 * 3& + xLong5
              xLong7 = xSingle2 * xSingleA1(xLong1) * 8&
              xLong6 = 10& - xLong7
              xLong7 = 10& + xLong7
              Call MoveToEx(xSharedDC, xLong4, xLong6, 0&)
              Call LineTo(xSharedDC, xLong5, xLong7)
              Call MoveToEx(xSharedDC, xLong4 + 1&, xLong6, 0&)
              Call LineTo(xSharedDC, xLong5 + 1&, xLong7)
              Call MoveToEx(xSharedDC, xLong4 + 2&, xLong6, 0&)
              Call LineTo(xSharedDC, xLong5 + 2&, xLong7)
              Next
            Else
            For xLong1 = 0& To ctSSLayer_SpectrumArrayCountX
              xSingleA1(xLong1) = CInt(xSingleA1(xLong1) * 21&)
              If xSingleA1(xLong1) >= xMaxVU(xLong1) Then
                xMaxVU(xLong1) = xSingleA1(xLong1)
                xMaxVUWait(xLong1) = xMaxVUWaitInit
                ElseIf Not xMaxVU(xLong1) = 0& Then
                If xMaxVUWait(xLong1) = 0& Then
                  xMaxVU(xLong1) = xMaxVU(xLong1) - 1&
                  Else
                  xMaxVUWait(xLong1) = xMaxVUWait(xLong1) - 1&
                  End If
                End If
              
              xType1.Left = xLong1 * 4&
              xType1.Top = 21& - xSingleA1(xLong1)
              xType1.Right = xLong1 * 4& + 4&
              xType1.Bottom = 21&
              Call FillRect(xSharedDC, xType1, FillSysColorFore)
              Call MoveToEx(xSharedDC, xLong1 * 4&, 21& - xMaxVU(xLong1), 0&)
              Call LineTo(xSharedDC, xLong1 * 4& + 4&, 21& - xMaxVU(xLong1))
              Next
            End If
          Call SelectObject(xSharedDC, xhPenOld)
          Call DeleteObject(xhPen)
          End If
        Call BitBlt(xHDC, 0&, 0&, 128&, 21&, xSharedDC, 0&, 0&, vbSrcCopy)
        Call SelectObject(xSharedDC, hBmpOld)
        Call DeleteObject(hBmp)
        End If
      Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_Spectrum), xHDC)
      End If
    xTimer1 = False
    End If
  End Sub

Private Sub InfoProcKb(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
  Static vByteA1(255) As Byte
  Static xSingleA1(32) As Single
  Static xLong1 As Long
  Static xByte1 As Byte
  Static xBool1 As Boolean
  'Static xOldLEDState As Long
  Dim xTimer1 As Boolean
  
  If Not xTimer1 Then
    xTimer1 = True
    xByte1 = CByte(1) - xByte1
    Select Case LEDState
      Case 0&
        If xShowSpectrum Then
          If CurMusic = ctSSLayer_MusicTypeNone Then
            If xBool1 Then
              Erase xSingleA1()
              xBool1 = False
              End If
            Else
            Call ctSSLayer_GetSpectrum(xSingleA1(0), ctSSLayer_SpectrumArrayCount, SongPointer(CurrentSongID), False)
            xBool1 = True
            End If
          
  '        Call ctSSLayer_GetSpectrum(xSingleA1())
          
          
          
          Rem Multiplicating higher Frequencies to show a usefull spectrum for choral songs
          Rem and songs that have no bass
          xLong1 = CInt( _
                         ValHighSgn _
                         ( _
                           ValHighSgn _
                             ( _
                             ValHighSgn(xSingleA1(2) * 1.5, xSingleA1(4) * 1.5) _
                             , _
                             ValHighSgn(xSingleA1(7) * 1.25, xSingleA1(11)) _
                             ) _
                           , _
                           ValHighSgn _
                             ( _
                             ValHighSgn(xSingleA1(15) * 1.5, xSingleA1(19) * 2) _
                             , _
                             ValHighSgn(xSingleA1(23) * 2.5, xSingleA1(27) * 3) _
                             ) _
                           ) _
                           * 4)                          '3 is real!
  '        xLong1 = CInt(xSingleA1(0) * 3)
          Select Case xLong1
            Case 0
              vByteA1(&H90) = ConstByte_0
              vByteA1(&H14) = ConstByte_0
              vByteA1(&H91) = ConstByte_0
            Case 1
              vByteA1(&H90) = ConstByte_1
              vByteA1(&H14) = ConstByte_0
              vByteA1(&H91) = ConstByte_0
            Case 2
              vByteA1(&H90) = ConstByte_1
              vByteA1(&H14) = ConstByte_1
              vByteA1(&H91) = ConstByte_0
            Case Else '3
              vByteA1(&H90) = ConstByte_1
              vByteA1(&H14) = ConstByte_1
              vByteA1(&H91) = ConstByte_1
            End Select
          End If
      Case 1& ' nothing / playing
        vByteA1(&H90) = ConstByte_0
        vByteA1(&H14) = ConstByte_0
        vByteA1(&H91) = ConstByte_0
      Case 2&
        vByteA1(&H90) = ConstByte_1
        vByteA1(&H14) = ConstByte_1
        vByteA1(&H91) = ConstByte_1
      Case 3&
        vByteA1(&H90) = xByte1
        vByteA1(&H14) = xByte1
        vByteA1(&H91) = xByte1
      Case Else
        vByteA1(&H90) = ConstByte_0
        xLong1 = (LEDState And &HF0&) / &H10&
        If xLong1 = 2& Then
          vByteA1(&H14) = xByte1
          Else
          vByteA1(&H14) = CByte(xLong1)
          End If
        xLong1 = (LEDState And &HFF00&) / &H100&
        If xLong1 = 2& Then
          vByteA1(&H91) = xByte1
          Else
          vByteA1(&H91) = CByte(xLong1)
          End If
      End Select
    If LEDMode = 1 Then
      Call SetKeyboardState(vByteA1(0))
      ElseIf LEDMode = 2 Then
      Call SetKeyXP(CLng(vByteA1(&H14&)), &H14&, MapVirtualKeyA_H14) 'MapVirtualKeyA(&H14&, 0&))
      Call SetKeyXP(CLng(vByteA1(&H90&)), &H90&, MapVirtualKeyA_H90) 'MapVirtualKeyA(&H14&, 0&))
      Call SetKeyXP(CLng(vByteA1(&H91&)), &H91&, MapVirtualKeyA_H91) 'MapVirtualKeyA(&H14&, 0&))
      End If
    xTimer1 = False
    End If
  End Sub

Private Sub SetKeyXP(ByRef KeyStateDesired As Long, ByRef KeyID As Long, ByRef KeyMap As Long)
  Dim xLong1 As Long
  
  xLong1 = GetKeyState(KeyID)
  If Not xLong1 = KeyStateDesired Then
    keybd_event KeyID, KeyMap, 0&, 0&
    keybd_event KeyID, KeyMap, &H2&, 0&
    End If
  End Sub

Private Function ValHighSgn(ByVal Value1 As Single, ByVal Value2 As Single) As Single
'ValHighSgn = Abs(Value1 - Value2)
'ValHighSgn = (Value1 + Value2 + ValHighSgn) / 2
' Anyway, this is way more faster:
If Value1 > Value2 Then
  ValHighSgn = Value1
  Else
  ValHighSgn = Value2
  End If
End Function

'private Sub SetLEDState(ByVal xByte1 As Byte, ByVal xByte2 As Byte, ByVal xByte3 As Byte)
'Static vByteA1(255) As Byte
'
'vByteA1(&H90) = xByte1
'vByteA1(&H14) = xByte2
'vByteA1(&H91) = xByte3
'Call SetKeyboardState(vByteA1(0))
'
'End Sub

'private Sub ResetVU()
'Erase xMaxVU
'End Sub


Public Sub eTimerEvent(ByVal ptrStream As Long, ByVal xBuff As Long, ByVal xLen As Long, ByVal xParam As Long)
'Static xLong1 As Long
Dim xLong1 As Long
'Dim xLong2 As Long
'Static xLong2 As Long
#If MultiGUITimer Then
  Static xBool1 As Boolean
  #End If

If xLen = 2& Then
  Call DoAnimateReadDir(1&)
  If Not (AskSndReset = 0& Or xAutoIgnoreTime = -1&) Then
    xLong1 = (DoEventsEx_GetElapsedTime(xAutoIgnoreTime) \ 1000&)
    If xLong1 < AutoIgnoreWaitTime Then
      Call SetWindowText(AskSndReset, Replace(LoadResEx_String(2075&), "$", CStr(AutoIgnoreWaitTime - xLong1)))
      Else
      If Not AskSndResetButton = 0& Then
        Call SendMessage(AskSndResetButton, &HF5&, 0&, 0&)
        End If
        'frmStartup.cmdRes_Click 2090
      End If
    End If


  Else
  'If IsDialoging Then
  '  call DoAnimateReadDir(1&)
  '  End If
  If WantsShutdownDialog And Not (xAutoDoing Or PlayerIsLocked) Then
    'WantsShutdownDialog = False
    StartShutDownStuff
    End If
  
  #If MultiGUITimer Then
  If (Not xTimer2 And Not xBool1) And DoEventsEx_IsTickCountElapsed(xLong1, GUIMinWaitInterval) Then
    #Else
    If Not xTimer2 And DoEventsEx_IsTickCountElapsed(xLong1, GUIMinWaitInterval) Then
    #End If
  'If Not xTimer2 And Abs(Timer * 100 - xLong1) > GUIMinWaitInterval Then
      #If MultiGUITimer Then
        If xTimer2 Then
          xBool1 = True
          Else
          xTimer2 = True
          End If
        #Else
        xTimer2 = True
        #End If
    'If xParam = 1 Then
    '  #If CallBackAddLog Then
    '    MainForm_ctConsoleA_AddLog "Stream_EndSongCallback event"
    '    #End If
    '    xTimer2 = True
    '    End If
    If InterfaceMode = Interface_Mode_Timer Then
      SetWindowText TimerListUpdateHWnd, Replace(LoadResEx_String(2600&), "$", GetTimeFormatNow)
      'If xTimerEditing Then
      '  frmTimerEdit.Caption = Replace(LoadResEx_String(4200&), "$", getformatedtime)
      '  End If
      End If


    MainForm_eTimerTime
    
    
    If Not hwndShutDownDialog = 0& Then
      xLong1 = DoEventsEx_GetElapsedTime(ShutDownInitTime)
#If MultiGUITimer Then
      If xLong1 >= ShutDownTimerMax And Not (xBool1 Or xAutoDoing Or PlayerIsLocked) Then
#Else
      If vShutDownDTimer >= xLong1 And Not (xAutoDoing Or PlayerIsLocked) Then
#End If
        QuitMode = QuitModePending
        Call SendMessage(hwndShutDownDialog, &H10, 0&, 0&)
        ElseIf Not hwndShutDownProgress = 0& Then
        Call SendMessage(hwndShutDownProgress, 1026&, CLng(Fix(xLong1 / ShutDownTimerMaxDiv)), 0&)
        End If
      End If
    'If xParam = 1 Then
    '  MainForm_EndSongCallback ptrStream
    '  Else
'      MainForm_eTimerTime
    '  End If
    
    xLong1 = DoEventsEx_GetTickCount
    #If MultiGUITimer Then
      If xBool1 Then
        xBool1 = False
        Else
        xTimer2 = False
        End If
      #Else
      xTimer2 = False
      #End If
    End If
  End If



  End Sub

Public Function xTimerChange(ByVal Index As Long, ByVal xTimer As Long, Optional ByVal xEvents As String = vbNullChar) As Long
Dim xChar1 As String

If Index = -1 Then
  xTimerEvTime(xTimerEvCount) = xTimer
  xTimerEv(xTimerEvCount) = xEvents
  Index = xTimerEvCount
  xTimerEvCount = xTimerEvCount + 1
  Else
  If Not xEvents = vbNullChar Then
    xTimerEv(Index) = xEvents
    End If
  xTimerEvTime(Index) = xTimer
  End If

Do
  If Index = 0 Then Exit Do
  If xTimerEvTime(Index) >= xTimerEvTime(Index - 1) Then Exit Do
  xTimerChange = xTimerEvTime(Index)
  xChar1 = xTimerEv(Index)
  xTimerEv(Index) = xTimerEv(Index - 1)
  xTimerEv(Index - 1) = xChar1
  xTimerEvTime(Index) = xTimerEvTime(Index - 1)
  xTimerEvTime(Index - 1) = xTimerChange
  Index = Index - 1
  Loop
Do
  If Index = xTimerEvCount - 1 Then Exit Do
  If xTimerEvTime(Index) < xTimerEvTime(Index + 1) Then Exit Do
  Exit Do
  xTimerChange = xTimerEvTime(Index)
  xChar1 = xTimerEv(Index)
  xTimerEv(Index) = xTimerEv(Index + 1)
  xTimerEv(Index + 1) = xChar1
  xTimerEvTime(Index) = xTimerEvTime(Index + 1)
  xTimerEvTime(Index + 1) = xTimerChange
  Index = Index + 1
  Loop

xTimerChange = Index
End Function

Public Function xTimerDisplay(ByVal Index As Long, ByVal Mode As Long, Optional ByRef PlayListFile As String, Optional ByRef CheckIsOk As Boolean) As String
  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 xChar2 As String
  
  xLong1 = InStr(xTimerEv(Index), "=")
  If Not xLong1 = 0& Then
    'xChar1 = xTimerEv(Index) & TimerSepp
    'Else
    xTimerDisplay = Trim(Left(xTimerEv(Index), xLong1 - 1&))
    'Debug.Print "#1: "; xTimerDisplay
    'xChar1 = Mid(xTimerEv(Index), xLong1 + 1&) & TimerSepp
    'xChar1 = xTimerEv(Index) & TimerSepp
    End If
  'CheckIsOk = True
  
  If Len(xTimerDisplay) = 0& Or CheckIsOk Then
    xChar1 = xTimerEv(Index) & TimerSepp
    Do
      xLong2 = InStr(xLong1 + 1&, xChar1, TimerSepp)
      If xLong2 = 0& Then
        If xLong1 = 0& Then CheckIsOk = False
        Exit Do
        Else
        If xLong5 = xTimerEvMaxSubEvents Then
          Else
          xLong5 = xLong5 + 1&
          xChar2 = Mid(xChar1, xLong1 + 1&, xLong2 - xLong1 - 1&)
          xLong3 = InStr(xChar2, ",")
          If xLong3 = 0& Then
            xLong4 = Val(xChar2)
            If xLong4 < 1& Or xLong4 > 299& Then
              xTimerDisplay = xTimerDisplay + "; ???"
              CheckIsOk = False
              ElseIf Not ValidSetupKey(xLong4) Then
              CheckIsOk = False
              Else
              xTimerDisplay = xTimerDisplay + "; " + LoadResEx_String(3700& + xLong4)
              If HasSubFile(xLong4) Then
                CheckIsOk = False
                'Else
                End If
              End If
            Else
            xLong4 = Val(Left(xChar2, xLong3 - 1&))
            If xLong4 < 1& Or xLong4 > 299& Then
              xTimerDisplay = xTimerDisplay + "; ???"
              CheckIsOk = False
              ElseIf Not ValidSetupKey(xLong4) Then
              CheckIsOk = False
              Else
              xTimerDisplay = xTimerDisplay + "; " + LoadResEx_String(3700& + xLong4)
              If HasSubFile(xLong4) Then
                xChar2 = Trim(Mid(xChar2, xLong3 + 1&))
                If Len(xChar2) = 0& Then
                  CheckIsOk = False
                  PlayListFile = PlayListFile & ", ???"
                  Else
                  PlayListFile = PlayListFile & ", " & xChar2
                  End If
                Else
                CheckIsOk = False
                End If
              End If
            End If
          End If
        xLong1 = xLong2
        End If
      Loop
    xTimerDisplay = Mid(xTimerDisplay, 3&)
    PlayListFile = Mid(PlayListFile, 3&)
    'Debug.Print CheckIsOk, xTimerEv(Index), xTimerDisplay
    End If
    
  
  
  
  GrabError
  xLong1 = (xTimerEvTime(Index) - Timer \ 60& + 1440&) Mod 1440&
  If xLong1 = 0& And xTimerEvTime(Index) + 1& = LastDoneTimer Then
    xLong1 = 1440&
    End If
  If Mode = 2 Then
    xTimerDisplay = Replace(LoadResEx_String(2651&), "$A", xTimerDisplay)
    xTimerDisplay = Replace(xTimerDisplay, "$T", Right("00" + CStr(Int(xTimerEvTime(Index) / 60&)), 2&) + ":" + Right("00" + CStr(xTimerEvTime(Index) Mod 60&), 2&))
    xTimerDisplay = Replace(xTimerDisplay, "$M", Right("00" + CStr(Int(xLong1 / 60&)), 2&) + ":" + Right("00" + CStr(xLong1 Mod 60&), 2&))
    ElseIf Mode = 1 Then
    xTimerDisplay = Right("00" + CStr(Int(xTimerEvTime(Index) / 60&)), 2&) + ":" + Right("00" + CStr(xTimerEvTime(Index) Mod 60&), 2&) & _
                    vbTab & xTimerDisplay & vbTab & _
                    Replace(LoadResEx_String(2655&), "$", _
                    Right("00" + CStr(Int(xLong1 / 60&)), 2&) + ":" + Right("00" + CStr(xLong1 Mod 60&), 2&) _
                    )
    If Not Len(PlayListFile) = 0& Then
      xTimerDisplay = xTimerDisplay & vbTab & PlayListFile
      End If
    End If
  End Function

Public Function HasSubFile(ByVal EventID As Long) As Boolean
  Select Case EventID
    Case KbdC_FAddFileS, KbdC_FAddDir, KbdC_FAddPList, KbdC_FLoadPL, KbdC_FSavePLas, _
         KbdC_OTimerImport, KbdC_OTimerExport, KbdC_OPresetSave, KbdC_OPresetLoad
      HasSubFile = True
    End Select
  End Function

Public Sub ResortTimers()
Dim xLong1 As Long
Dim xLong2 As Long
Dim xLong4 As Long
Dim xLong5 As Long
Dim xLong6 As Long
Dim xChar1 As String

GrabError
xLong2 = Timer \ 60
If Not xTimerEvIR = -1 Then
  If (xTimerEvTime(xTimerEvIR) - xLong2 + 1440) Mod 1440 > 150 Then
    For xLong4 = xTimerEvTime(xTimerEvIR) To xTimerEvCount - 2
      xTimerEv(xLong4) = xTimerEv(xLong4 + 1)
      xTimerEvTime(xLong4) = xTimerEvTime(xLong4 + 1)
      Next
    xTimerEvIR = -1
    xTimerEvCount = xTimerEvCount - 1
    End If
  End If

For xLong1 = 0 To xTimerEvCount - 2
  xLong5 = xLong1
  xLong6 = (xTimerEvTime(xLong5) - xLong2 + 1440) Mod 1440
  If xLong6 = 0 And xTimerEvTime(xLong5) + 1 = LastDoneTimer Then
    xLong6 = 1440
    End If
  For xLong4 = xLong1 + 1 To xTimerEvCount - 1
    If (xTimerEvTime(xLong4) - xLong2 + 1440) Mod 1440 < xLong6 Then
      xLong5 = xLong4
      xLong6 = (xTimerEvTime(xLong5) - xLong2 + 1440) Mod 1440
      If xLong6 = 0 And xTimerEvTime(xLong5) + 1 = LastDoneTimer Then
        xLong6 = 1440
        End If
      End If
    Next
  If Not xLong5 = xLong1 Then
    xChar1 = xTimerEv(xLong1)
    xLong6 = xTimerEvTime(xLong1)
    xTimerEv(xLong1) = xTimerEv(xLong5)
    xTimerEvTime(xLong1) = xTimerEvTime(xLong5)
    xTimerEv(xLong5) = xChar1
    xTimerEvTime(xLong5) = xLong6
    If xTimerEvIR = xLong1 Then
      xTimerEvIR = xLong5
      ElseIf xTimerEvIR = xLong5 Then
      xTimerEvIR = xLong1
      End If
    End If
  Next

MainForm_SetTimerImage
End Sub

Public Function AppBase(Optional ByVal ExtensionToAdd As String = vbNullChar) As String
  Static xLong1 As Long
  Static xChar1 As String
  Dim xChar2 As String
  
  Static xChar3 As String
  Static xChar4 As String
  Static xLong3 As Long
  
  If xChar4 = "" Or Left(ExtensionToAdd, 1&) = Chr(2&) Then
    If Left(ExtensionToAdd, 1) = Chr(2&) Then
      xChar3 = xChar1
      xLong3 = xLong1
      xChar2 = Mid(ExtensionToAdd, 2&)
      Else
      #If 0 Then 'InIDE Then
        xChar2 = App.Path
        If Not Right(xChar2, 1&) = "\" Then xChar2 = xChar2 + "\"
        xChar2 = Chr(34&) + xChar2 + App.EXEName + ".exe" + Chr(34&) + " " + Command
        'xLong1 = InStrRev(xChar2, ".")
        #Else
        If IsUnicode Then
          xChar2 = PeekS_Wide(GetCommandLineW)
          Else
          xChar2 = PeekS_Ansi(GetCommandLineA)
          End If
        #End If
      End If
    If Left(xChar2, 1&) = Chr(34&) Then
      xChar2 = Mid(xChar2, 2&)
      xLong1 = Len(xChar2)
      If Not xLong1 = 0& Then
        xChar2 = Left(xChar2, xLong1 - 1&)
        End If
      xLong1 = InStr(xChar2, Chr(34&))
      Else
      xLong1 = InStr(xChar2, " ")
      End If
    If Not xLong1 = 0& Then
      xChar2 = Left(xChar2, xLong1 - 1&)
      End If
    'Stop
    #If InIDE Then
      #Else
      xChar2 = GetFullPathName(xChar2)
      #End If
'Stop

    xLong1 = InStrRev(xChar2, "\")
    AppBase = Mid(xChar2, xLong1 + 1&)
    xChar2 = Left(xChar2, xLong1)
    xLong1 = InStrRev(AppBase, ".")
    If xLong1 = 0& Then
      xChar1 = AppBase & "."
      Else
      xChar1 = Left(AppBase, xLong1)
      End If
    Select Case xChar1
      Case LCase(xChar1)
        xLong1 = 1&
      Case UCase(xChar1)
        xLong1 = 2&
      Case Else
        xLong1 = 0&
      End Select
    xChar1 = xChar2 & xChar1
    If xChar4 = "" Then xChar4 = xChar2 & AppBase
    If xChar3 = "" Then
      xChar3 = xChar1
      xLong3 = xLong1
      End If
    End If
  
  Select Case Left(ExtensionToAdd, 1&)
    Case vbNullChar
      AppBase = xChar4
      Exit Function
    Case Chr(1&)
      ExtensionToAdd = Mid(ExtensionToAdd, 2&)
      Select Case ExtensionToAdd
        Case LCase(ExtensionToAdd), UCase(ExtensionToAdd)
          If xLong1 = 2& Then
            AppBase = xChar3 & UCase(ExtensionToAdd)
            Else
            AppBase = xChar3 & LCase(ExtensionToAdd)
            End If
        Case Else
          AppBase = xChar3 & ExtensionToAdd
        End Select
      Exit Function
    Case Chr(2&)
      ExtensionToAdd = Mid(ExtensionToAdd, 2&)
    Case Chr(3&)
      xLong1 = InStrRev(xChar1, "\")
      'If Not xLong1 = 0& Then
      'If xLong1 = 3& Then
       ' AppBase = Left(xChar1, xLong1 - 1&)
        'Else
        If Not xLong1 = 0& Then
        AppBase = Left(xChar1, xLong1)
        End If
      Exit Function
    End Select
  Select Case ExtensionToAdd
    Case LCase(ExtensionToAdd), UCase(ExtensionToAdd)
      If xLong1 = 2& Then
        AppBase = xChar1 & UCase(ExtensionToAdd)
        Else
        AppBase = xChar1 & LCase(ExtensionToAdd)
        End If
    Case Else
      AppBase = xChar1 & ExtensionToAdd
    End Select
  End Function

Public Function GetKeyCaption(ByVal KeyCode As Long) As String
  'Dim xLong1 As Long
  
  KeyCode = KeyCode And &HFF&
  If Not KeyCode = 0& Then
    If NonRealKey(KeyCode) Then
      Select Case KeyCode
        Case 5&
          GetKeyCaption = LoadResEx_String(3995&)
        Case 6&
          GetKeyCaption = LoadResEx_String(3996&)
        'Case &HE7&
        '  GetKeyCaption = "##Mouse Wheel up"
        'Case &HE8&
        '  GetKeyCaption = "##Mouse Wheel down"
        End Select
      Else
      GetKeyCaption = GetKeyNameText(&H1 + (MapVirtualKey(KeyCode, 0&) And &HFF&) * &H10000)
      'Debug.Print GetKeyCaption
      If Len(GetKeyCaption) = 0& Then
        GetKeyCaption = "#" & Hex(KeyCode)
        ElseIf UnassignedKey(KeyCode) Then
        GetKeyCaption = Replace(StrConv(GetKeyCaption, vbProperCase), " ", "") + " (#" & Hex(KeyCode) + ")"
        Else
        GetKeyCaption = Replace(StrConv(GetKeyCaption, vbProperCase), " ", "")
        End If
      'Debug.Print GetKeyCaption
      End If
    End If
  End Function

Public Function GetDefControls() As String
  GetDefControls = KeyBoardVersionID & LoadResEx_Data("CONTROLS", "BIN", LoadResEx_LanguageFile_hModule)
  End Function

Public Sub GetControls(ByVal xChar1 As String, ByRef xKeyArray() As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  
  If Left(xChar1, KeyBoardVersionIDLen) = KeyBoardVersionID Then
    xChar1 = Mid(xChar1, KeyBoardVersionIDLenP) & String(KbdCountX, ",")
    For xLong1 = 0& To KbdCountX
      If ValidSetupKey(xLong1) Then
      'If Not (xLong1 = KbdSplit) Then
        xLong2 = InStr(xChar1, ",")
        xLong3 = CLng(Val("&h" & Left(xChar1, xLong2 - 1&)))
        Select Case xLong3
          Case Is < 32&
            xKeyArray(xLong1) = 0&
          Case Else
            xKeyArray(xLong1) = xLong3
          End Select
        xChar1 = Mid(xChar1, xLong2 + 1&)
        End If
      Next
    End If
  End Sub

Public Function SimpleDir(ByVal xChar1 As String) As String
Dim xLong1 As Long

xLong1 = InStrRev(xChar1, "\") - 1&
If Not xLong1 = -1& Then
  SimpleDir = Left(xChar1, xLong1)
  End If
End Function

Public Function SimpleFile(ByVal xChar1 As String) As String
  SimpleFile = Mid(xChar1, InStrRev(xChar1, "\") + 1&)
  End Function

Private Function GetTrackNumber(ByVal Filename As String) As String
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  xLong1 = Len(Filename) - 4&
  Do While xLong2 < xLong1
    xLong2 = xLong2 + 1&
    If Not InStr("0123456789", Mid(Filename, xLong2, 1&)) = 0& Then
      Filename = Mid(Filename, xLong2)
      xLong1 = xLong1 - xLong2 + 2&
      xLong2 = 1&
      Do While xLong2 < xLong1
        xLong2 = xLong2 + 1&
        If InStr("0123456789", Mid(Filename, xLong2, 1&)) = 0& Then
          Exit Do
          End If
        Loop
      If Not xLong2 = 1& Then
        GetTrackNumber = Left(Filename, xLong2 - 1&)
        End If
      End If
    Loop
    
  'xChar1 = Mid(xChar1, 9&)
  'xChar1 = Left(xChar1, InStr(xChar1 & ".", ".") - 1&)
  ''If Val(xChar2) > 0& Then
  End Function

Private Function ProcClassWnd(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'  If hWnd = 0& Then
'    ProcClassWnd = Message
'    Else
    Select Case Message
      Case &HF& 'WM_PAINT = &HF
        IRDraw
        GoTo ForceProc
        'If IsUnicode Then
        '  ProcClassWnd = DefWindowProcW(hwnd, Message, wParam, lParam)
        '  Else
        '  ProcClassWnd = DefWindowProcA(hwnd, Message, wParam, lParam)
        '  End If
      Case &H1A& ', &H1B& 'WM_SETTINGCHANGE
        'Const DBT_DEVICEARRIVAL As Long = &H8000
        'Const DBT_DEVICEQUERYREMOVE As Long = &H8001
        'Const DBT_DEVICEQUERYREMOVEFAILED As Long = &H8002
        'Const DBT_DEVICEREMOVECOMPLETE As Long = &H8004
        'Const WM_DEVMODECHANGE As Long = &H1B
        If wParam = 47 Then
          CheckValidScreen True
          IRPos True
          End If
        'Debug.Print "SetChange"; wParam, lParam
      Case &H7E& ' screenchange
        CheckValidScreen True
        IRPos True
      Case &H200& 'Const WM_MOUSEMOVE = &H200
        Call MainForm_TrayIconHandling(lParam And &HFFFF)
      Case &H113& 'Const WM_TIMER = &H113
        IRPos False
      Case 2& 'Forgotten :)
      '  ProcClassWnd = 0&
        
        'Debug.Print ProcClassWnd
        'ProcClassWnd = 0&
      
      Case &H15& 'syscolorchange
        GetWhiteBackGrnd 'False
        MainForm_UpdateMyFont
      Case &H31A&
        DoThemeUpdates
        If InterfaceMode = Interface_Mode_None Then
          DrawWhiteDC xStaticIconsHWnd(xStaticIcons_DirList), xListHWnd(0), 1&, True
          End If
        DrawWhiteDC xStaticIconsHWnd(xStaticIcons_CurSong), xListHWnd(1), 0&, True
      Case &H4A& ' senddata
        ProcClassWnd = MainForm_GetMessageA_CopyData(lParam)
        'ProcClassWnd =
      Case &H218& '&H48& 'WM_PowerMsgs
        If wParam = 4& Or wParam = 5& Then
          MainForm_SetStandby True
          ElseIf wParam = 7& Or wParam = 8& Or wParam = &H12& Then
          If LEDMode = 0& Or Not wParam = &H12& Then
            MainForm_SetStandby False
            End If
          End If
      Case &H312& 'wm_HotKey
        #If NewKeyMode Then
          wParam = KbdSplitN + wParam
'Debug.Print "#HotKey: ", wParam
          If Not wParam = RGK_xOldKey Then
'Debug.Print "#HotKeyFirst", wParam, RGK_xOldKey
            RGK_xOldKeyTime = DoEventsEx_GetTickCount
            RGK_xOldKeyTimeWait = RGK_xOldKeyTimeWaitLong
            If RGK_xOldKey = KbdC_GShowTitle And RGK_xOldKeyTimeWait = RGK_xOldKeyTimeWaitShort Then
              RGK_xOldKey = wParam
              Call MainForm_ProcessKey(wParam)
              MainForm_HideIRWin
              Else
              RGK_xOldKey = wParam
              Call MainForm_ProcessKey(wParam)
              End If
'Debug.Print "LP: ", RGK_xOldKey
'Else
'Debug.Print "#HotKeyAgain"
            End If
          #Else
          Call MainForm_ProcessKey(KbdSplitN + wParam)
          #End If
      Case &H400&, &H111&
        'wParam = 205&
'Debug.Print "WM_COMMAND", wParam, lParam
        Select Case wParam
          Case 1&, 2&, 200& To 205&, 220& To 223&, 230& To 240&, _
               300& To 307&, 318&, 319&, 320&, 330& To 331&, 340&, 360&, 390& To 392&, _
               400& To 403&, 410& To 412&, 420& To 421&, 430& To 432&, 440& To 442&, _
                           450& To 453&, 460& To 467&, _
               510&, 512&, 513&, 515& To 518&, 590&, 600&, 601&, 610&
            ProcClassWnd = MainForm_DoCommand(wParam) ', lParam
          Case 800&, 801&, 804&, 805&, 810& To 817&
            ProcClassWnd = SMQueryString(wParam, lParam)
'          Case 100&
'            MainForm_GetMessageA_ReqData lParam
          'Case 110&, 115&
          '  ProcClassWnd = SMQueryString(lParam, wParam = 115&)
          Case -2&
            MainForm_EndSongCallback lParam
          Case -1&
            Select Case lParam
              Case 1&
                MainForm_DelayedDragDrop
              Case 2&, 4& To 6&
                If hwndShutDownDialog = 0& Then
                  If lParam = 2& Then
                    QuitModePending = 1&
                    Else
                    QuitModePending = lParam - 2&
                    End If
                  If (PlayerIsLocked Or xAutoDoing) Then
                    'Call PostMessageA(xStaticIconsHWnd(xStaticIcons_ShowIR), &H400&, -1&, 2&)
                    WantsShutdownDialog = True
                    Else
                    StartShutDownStuff
                    End If
                  End If
              Case 3&
                PostGetMessage
              'Case 4& to 6& see 1&
              End Select
          End Select
      Case &HD& ' WM_GETTEXT
        ProcClassWnd = FastCopy(hWnd, wParam, lParam)
      Case &HE& ' WM_GETTEXTLENGTH = &HE
        ProcClassWnd = FastCopy(hWnd, wParam, ptrTemp)
        'If xPStateIconPlaying And Not (xPStateIconSndcard Or xPStateIconQuit Or CurMusic = ctSSLayer_MusicTypeNone Or xPStateIconStartup) Then
        '  ProcClassWnd = Len(GetCurSong(False))
        '  End If
      Case 15&
        GoTo ForceProc
        'If IsUnicode Then
        '  ProcClassWnd = DefWindowProcW(hwnd, Message, wParam, lParam)
        '  Else
        '  ProcClassWnd = DefWindowProcA(hwnd, Message, wParam, lParam)
        '  End If
      Case Else
        If Not xTimer3 Then
ForceProc:
          If IsUnicode Then
            ProcClassWnd = DefWindowProcW(hWnd, Message, wParam, lParam)
            Else
            ProcClassWnd = DefWindowProcA(hWnd, Message, wParam, lParam)
            End If
          End If
      End Select
  '  End If
  End Function

Private Sub IRDraw()
'  Const xBarHeightX2 = xBarHeight * 2.5
'  Const xBarHeightX3 = xBarHeight * 4&
'  Const xBarHeightX4 = xBarHeightX3 + xBarHeight
  Dim hdc As Long
  Dim hFontOld As Long
  #If SelfPathElipse Then
    #Else
    Dim xLong1 As Long
    #End If
  Dim xLong2 As Long
  Dim xLong3 As Long
  #If SelfPathElipse Then
    Dim xType1 As POINTAPI
    #Else
    Dim xType1 As RECT
    Dim xChar1 As String
    #End If
  'Const xBarHeight As Long = 35&

'Debug.Print "  #1", PLCurSong
'Debug.Print "  #2", PLCurSongDisplay
'Debug.Print "  #3", PLCurFullURL
'Debug.Print "  #4", CurMusic, ctSSLayer_MusicTypeStream, xSongIsMidi

  hdc = GetDC(xStaticIconsHWnd(xStaticIcons_ShowIR))
  If Not hdc = 0& Then
    #If SelfPathElipse Then
      #ElseIf LoudestSongMethode Then
      If ShowFullTitle And Not LastLoudestTearIS Then
        xLong1 = Len(LastLoudestTearFn)
        Call PokeS(ptrTemp, LastLoudestTearFn, ptrTempSizeXW)
        Else
        xLong1 = Len(LastLoudestTearCp)
        Call PokeS(ptrTemp, LastLoudestTearCp, ptrTempSizeXW)
        End If
      #Else
      If ShowFullTitle And Not (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
        xLong1 = Len(PLCurSong)
        Call PokeS(ptrTemp, PLCurSong, ptrTempSizeXW)
        Else
        xLong1 = Len(PLCurSongDisplay)
        Call PokeS(ptrTemp, PLCurSongDisplay, ptrTempSizeXW)
        End If
      #End If
    If True Then 'xTitlesPos = 1& Or xTitlesPos = 2& Then
      hFontOld = SelectObject(hdc, hFontShIR)
      'xLong1 = Len(PLCurSongDisplay)
      
      If Not ShowIR_IconHWnd = 0& Then
        xLong2 = 38&
        xLong3 = 36&
        If xTitlesPos = 1& Or xTitlesPos = 2& Then
          Else
          Call DrawIcon(hdc, 4&, 0&, ShowIR_IconHWnd)
          End If
        End If
      
      #If SelfPathElipse Then
        #If LoudestSongMethode Then
          If Not NeedIRPathElipse = LastLoudestTearFn Then
            If Not ShowIR_IconHWnd = 0& Then
              xLong2 = 38&
              End If
            If ShowFullTitle And Not LastLoudestTearIS Then
              ShownIRPath = Ellipsis_PathSmartStart(hdc, LastLoudestTearFn, VertIRBarHeight - 6& - xLong2)
              Else
              ShownIRPath = Ellipsis_PathSmartStart(hdc, LastLoudestTearCp, VertIRBarHeight - 6& - xLong2)
              End If
            NeedIRPathElipse = LastLoudestTearFn
            End If
          #Else
          If CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3& Then
            If Not NeedIRPathElipse = PLCurFullURL Then GoTo URLChangedHook
            ElseIf Not NeedIRPathElipse = PLCurSong Then
URLChangedHook:
            If Not ShowIR_IconHWnd = 0& Then
              xLong2 = 38&
              End If
            If ShowFullTitle And Not (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
              ShownIRPath = Ellipsis_PathSmartStart(hdc, PLCurSong, VertIRBarHeight - 6& - xLong2)
              Else
              ShownIRPath = Ellipsis_PathSmartStart(hdc, PLCurSongDisplay, VertIRBarHeight - 6& - xLong2)
              End If
            NeedIRPathElipse = PLCurSong
            End If
          #End If
        #Else
        xType1.Bottom = VertIRBarHeight - 6& - xLong2 '42&
        xType1.Right = xType1.Bottom
        'xLong1 = Len(PLCurSongDisplay)
        
        If IsUnicode Then
          '&H18D20 /// &H14D20
          If DrawTextW(hdc, ptrTemp, xLong1, xType1, &H10D20 + &H4000 * (2& + CLng(ShowFullTitle))) = 0& Then
            GoTo FailToElipseViaApi
            Else
            xChar1 = PeekS_Wide(ptrTemp)
            xLong1 = Len(xChar1)
            End If
          Else
          If DrawTextA(hdc, ptrTemp, xLong1, xType1, &H10D20 + &H4000 * (2& + CLng(ShowFullTitle))) = 0& Then
FailToElipseViaApi:
            #If LoudestSongMethode Then
              If ShowFullTitle And Not LastLoudestTearIS Then
                xChar1 = LastLoudestTearFn
                Else
                xChar1 = LastLoudestTearCp
                End If
              #Else
              If ShowFullTitle And Not (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
                xChar1 = PLCurSong
                Else
                xChar1 = PLCurSongDisplay
                End If
              #End If
            Else
            xChar1 = PeekS_Ansi(ptrTemp)
  'Debug.Print "touched: " + xChar1
            xLong1 = Len(xChar1)
            End If
          End If
        'Call GetTextExtentPoint32Wide(hdc, StrPtr(xChar1), xLong1, xType1)
        #End If
      
      If xTitlesPos = 1& Or xTitlesPos = 2& Then
        If Not ShowIR_IconHWnd = 0& Then
          Call DrawIcon(hdc, 0&, VertIRBarHeight - 36&, ShowIR_IconHWnd)
          End If
        If hFontShIRIsVerticalBitmapFont Then
          #If SelfPathElipse Then
            xLong2 = Len(ShownIRPath)
            Call GetTextExtentPoint32Wide(hdc, StrPtr(ShownIRPath), xLong2, xType1)
            Call TextOutVBF(hdc, VertIRBarHeight - 4& - xLong3 - xType1.x, xType1.x, ShownIRPath)
            #Else
            Call GetTextExtentPoint32Wide(hdc, StrPtr(xChar1), xLong1, xType1)
            Call TextOutVBF(hdc, VertIRBarHeight - 4& - xLong3 - xType1.Left, xType1.Left, xChar1)
            #End If
          Else
          #If SelfPathElipse Then
            Call TextOut(hdc, ShownIRPath, 0&, VertIRBarHeight - 4& - xLong3)   '- 40&) ' - xType2.x)
            #Else
            Call TextOut(hdc, xChar1, 0&, VertIRBarHeight - 4& - xLong3)   '- 40&) ' - xType2.x)
            #End If
          End If
        Else
        #If SelfPathElipse Then
          Call TextOut(hdc, ShownIRPath, 4& + xLong3, 0&)
          #Else
          Call TextOut(hdc, xChar1, 4& + xLong3, 0&)
          #End If
        End If
      Call SelectObject(xSharedDC, hFontOld)
      End If
    Call ReleaseDC(xStaticIconsHWnd(xStaticIcons_ShowIR), hdc)
    End If
  End Sub

Private Sub TextOutVBF(ByVal hdc As Long, ByVal TopPos As Long, ByVal WidthX As Long, ByVal strCap As String)
  Dim hBmp As Long
  Dim hBmpOld As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  'Dim hMem As Long
  Dim xType1 As RECT
  Dim SrcBits() As Long
  Dim DesBits() As Long
  Dim xType2 As BITMAPINFOHEADER
  Const xBarHeight As Long = 35&
  
#If UseFileLogging Then
  If WidthX = 0& Then
    MainForm_ctConsoleA_AddLog "Trying to render blank string!!!"
    Else
#Else
  If Not WidthX = 0& Then
#End If
 
  
    hBmp = CreateCompatibleBitmapX(WidthX, xBarHeight)
    If Not hBmp = 0& Then
      Call SelectObject(xSharedDC, hFontShIR)
      hBmpOld = SelectObject(xSharedDC, hBmp)
      If hBmpOld = 0& Then
        Call DeleteObject(hBmp)
        Else
        xType1.Bottom = xBarHeight
        xType1.Right = WidthX
        Call FillRect(xSharedDC, xType1, &H6&)
        Call TextOut(xSharedDC, strCap, 0&, 0&)
        Call SelectObject(xSharedDC, hBmpOld)
        ReDim SrcBits(0& To WidthX - 1&, 0& To xBarHeight - 1&) As Long
        With xType2
          .biBitCount = 32&
          .biSize = 40&
          .biWidth = WidthX
          .biHeight = -xBarHeight
          .biPlanes = 1&
          End With
        xLong1 = GetDIBits(xSharedDC, hBmp, 0&, xBarHeight, ByVal VarPtr(SrcBits(0&, 0&)), xType2, 0&)
        Call DeleteObject(hBmp)
        If Not xLong1 = 0& Then
          ReDim DesBits(0& To xBarHeight - 1&, 0& To WidthX - 1&) As Long
          For xLong1 = 0& To WidthX - 1&
            For xLong2 = 0& To xBarHeight - 1&
              DesBits(xLong2, xLong1) = SrcBits(xLong1, xLong2)
              Next
            Next
          hBmp = CreateCompatibleBitmapX(xBarHeight, WidthX)
          If Not hBmp = 0& Then
            xType2.biWidth = xBarHeight
            xType2.biHeight = WidthX
            
            If Not SetDIBits(xSharedDC, hBmp, 0&, WidthX, ByVal VarPtr(DesBits(0&, 0&)), xType2, 0&) = 0& Then
              If Not SelectObject(xSharedDC, hBmp) = 0& Then
                Call BitBlt(hdc, 0&, TopPos, xBarHeight, WidthX, xSharedDC, 0&, 0&, vbSrcCopy)
  'Debug.Print "Painting"
                Call SelectObject(xSharedDC, hBmpOld)
                End If
              End If
            Call DeleteObject(hBmp)
            End If
          End If
        End If
      Call SelectObject(xSharedDC, hFontGFix)
      End If
    End If
  End Sub

Private Sub IRPos(ByVal Force As Boolean)
  Const xBarHeight As Long = 35&
  Const xBarHeightX2 As Long = xBarHeight * 2.5
  Const xBarHeightX3 As Long = xBarHeight * 4&
  Const xBarHeightX4 As Long = xBarHeightX3 + xBarHeight

  Dim xLong1 As Long
  Dim xType1 As RECT
  Dim xType2 As RECT

  Call GetWindowRect(GetDesktopWindow(), xType1)
  ''Call SystemParametersInfoA(48&, 0&, xType1, 0&)
  Call GetCursorPos(xType2)
#If SelfPathElipse Then
  NeedIRPathElipse = ""
#End If
  Select Case xTitlesPos
    Case 0& 'top
      If xType2.Top < xBarHeightX2 Then
        xLong1 = 1&
        'Else
        'xLong1 = 0&
        End If
      VertIRBarHeight = xType1.Right
    Case 1& 'left
      If xType2.Left < xBarHeightX2 Then
        xLong1 = 3&
        Else
        xLong1 = 2&
        End If
      Call SystemParametersInfoA(48&, 0&, xType2, 0&)
      VertIRBarHeight = xType2.Bottom - xType2.Top
    Case 2& 'right
      If xType2.Left > xType1.Right - xBarHeightX2 Then
        xLong1 = 5&
        Else
        xLong1 = 4&
        End If
      Call SystemParametersInfoA(48&, 0&, xType2, 0&)
      VertIRBarHeight = xType2.Bottom - xType2.Top
    Case 3& 'bottom
      Call GetWindowRect(GetDesktopWindow(), xType1)
      If xType2.Top > xType1.Bottom - xBarHeightX2 Then
        xLong1 = 7&
        Else
        xLong1 = 6&
        End If
      VertIRBarHeight = xType1.Right
    End Select
  If Force Or Not xLong1 = xCalaymClassAlign Then
'          Debug.Print "AlignMd: ", xLong1, VertIRBarHeight
    Select Case xLong1
      Case 0&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, 0&, 0&, xType1.Right, xBarHeight, &H214&)
      Case 1&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, 0&, xBarHeightX3, xType1.Right, xBarHeight, &H214&)
      Case 2&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, 0&, xType2.Top, xBarHeight, VertIRBarHeight, &H214&)
      Case 3&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, xBarHeightX3, xType2.Top, xBarHeight, VertIRBarHeight, &H214&)
      Case 4&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, xType1.Right - xBarHeight, xType2.Top, xBarHeight, VertIRBarHeight, &H214&)
      Case 5&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, xType1.Right - xBarHeightX4, xType2.Top, xBarHeight, VertIRBarHeight, &H214&)
      Case 6&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, 0&, xType1.Bottom - xBarHeight, xType1.Right, xBarHeight, &H214&)
      Case 7&: Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_ShowIR), 0&, 0&, xType1.Bottom - xBarHeightX4, xType1.Right, xBarHeight, &H214&)
      End Select
    xCalaymClassAlign = xLong1
    End If
  If ShowIR_IconHWnd = 0& Then 'And ShowIR_IconHWnd2 = 0& Then
    ShowIR_Width = xType1.Right - 4&
    Else
    ShowIR_Width = xType1.Right - 44&
    End If
  End Sub

Private Function FastCopy(ByVal hWnd As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  Dim xChar1 As String

' No need to check, cause windows creates a copy of the original buffer, thus crashes sender when trying to copy back...
' Doing it anyway, in case windows does not...
  xLong1 = GetAccessSize(lParam)
  If IsUnicode Then wParam = wParam \ 2&
  If wParam = 0& Then
  '  Debug.Print "Handed!"
    MsgBox "Test"
    End If
  If Not xLong1 < wParam And wParam > 0& Then
      Select Case hWnd
        Case xStaticIconsHWnd(xStaticIcons_DirList)
          xChar1 = DirLblCaption
        Case xStaticIconsHWnd(xStaticIcons_CurSong)
          If xPStateIconPlaying And Not (xPStateIconSndcard Or xPStateIconQuit Or CurMusic = ctSSLayer_MusicTypeNone Or xPStateIconStartup) Then
          'If Not CurMusic = ctSSLayer_MusicTypeNone Then
            xChar1 = PLCurSongDisplaySpaceStr
            End If
        Case xStaticIconsHWnd(xStaticIcons_ShowIR)
          If xPStateIconPlaying And Not (xPStateIconSndcard Or xPStateIconQuit Or CurMusic = ctSSLayer_MusicTypeNone Or xPStateIconStartup) Then
            xChar1 = GetCurSong(False)
            End If
        Case Else
          Exit Function
        End Select
    
    Call PokeS(lParam, xChar1, wParam)
    If Len(xChar1) > wParam Then
      FastCopy = wParam
      Else
      FastCopy = Len(xChar1)
      End If
    'Debug.Print "FastCopy returns: ", FastCopy, xChar1
    End If
  End Function

Private Function GetAccessSize(ByVal hMem As Long) As Long
  Dim xLong1 As Long
  Dim xType1 As MEMORY_BASIC_INFORMATION

  If VirtualQuery(hMem, xType1, 28&) Then
    ' () !!!!:
    xLong1 = xType1.RegionSize + (xType1.BaseAddress - hMem)
    If xLong1 > 0& Then
      If IsBadWritePtr(hMem, xLong1) = 0& Then
        GetAccessSize = xLong1
        End If
      End If
    End If
  End Function

Public Function ValidSetupKey(ByVal KeyID As Long) As Boolean
  Select Case KeyID
    Case KbdSplit, KbdC_FFileMenu, KbdC_FLoadPLMenu, _
         KbdC_CPCMenu, KbdC_CCurSongMenu, _
         KbdC_PPLMenu, KbdC_OOptionsMenu, KbdC_CVolMenu, _
         KbdC_TToolsMenu, KbdC_OAutoRFMenu, KbdC_ODirMenu, KbdC_OTimerMenu, KbdC_OPresetMenu, _
         KbdC_IInfoMenu, KbdC_GVolMenu
    Case Else
      ValidSetupKey = True
    End Select
  End Function

'private Sub ctSSLayer_EndSongCallback(ByRef SongID As Long)
'  Debug.Print "PostMessage"
'  PostMessageA xStaticIconsHWnd(xStaticIcons_ShowIR), &H113&, 100&, SongID
'  End Sub

Public Sub RePosWindow()
  Dim xType1 As RECT
  
  If xMWinPos.Right = -1& Or xMWinPos.Bottom = -1& Then
    Call GetWindowRect(GetDesktopWindow(), xType1)
    xMWinPos.Right = (xType1.Right - xType1.Left) \ 2
    xMWinPos.Bottom = (xType1.Bottom - xType1.Top) * 3 \ 4
    xMWinPos.Left = xType1.Left + (xType1.Right - xType1.Left - xMWinPos.Right) / 2&
    xMWinPos.Top = xType1.Top + (xType1.Bottom - xType1.Top - xMWinPos.Bottom) / 2&
    Call SetWindowPos(MainHWnd, 0&, xMWinPos.Left, xMWinPos.Top, xMWinPos.Right, xMWinPos.Bottom, &H214&)
    Else
    CheckValidScreen False
    End If
'  Debug.Print "RePos1", xMWinPos(1).Left, xMWinPos(1).Top, xMWinPos(1).Bottom, xMWinPos(1).Right
  End Sub

Private Sub CheckValidScreen(ByRef ReRead As Boolean)
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xType1 As RECT
  
'  xType4.Length = 44&
  Const WindowOverlapBottom As Long = 48& '128&
  Const WindowOverlapSide As Long = 96&
  
  Call GetOptMonitorFromPoint(xMWinPos.Left + xMWinPos.Right / 2&, xMWinPos.Top + xMWinPos.Bottom / 2&, xType1)
  xLong2 = xType1.Right - xType1.Left
  xLong3 = xType1.Bottom - xType1.Top
  
  If ReRead Then ResizeRemember
  If xMWinPos.Right > xLong2 Then xMWinPos.Right = xLong2
  If xMWinPos.Bottom > xLong3 Then xMWinPos.Bottom = xLong3
  
  If xMWinPos.Left + xMWinPos.Right < xType1.Left + WindowOverlapSide Then xMWinPos.Left = xType1.Left + WindowOverlapSide - xMWinPos.Right
  If xMWinPos.Left > xLong2 - WindowOverlapSide Then xMWinPos.Left = xLong2 - WindowOverlapSide
  If xMWinPos.Top > xLong3 - WindowOverlapBottom Then xMWinPos.Top = xLong3 - WindowOverlapBottom
  If xMWinPos.Top < xType1.Top Then xMWinPos.Top = xType1.Top
  Call SetWindowPos(MainHWnd, 0&, xMWinPos.Left, xMWinPos.Top, xMWinPos.Right, xMWinPos.Bottom, &H214&)
  
  xType1.Left = 0&
  Call ClientToScreen(xStaticIconsHWnd(xStaticIcons_BAR), xType1)
  xLong2 = xType1.Left
  xType1.Left = 0&
  Call ClientToScreen(xStaticIconsHWnd(xStaticIcons_CurSongPos), xType1)
  ePic1ScaleBonus = xType1.Left - xLong2
  End Sub

'Public Sub GetMyWindowEx()
'  Dim xType1 As WINDOWPLACEMENT
'
'  xType1.Length = 44&
'  Call GetWindowPlacement(MainHWnd, xType1)
'  xMWinPos = xType1.rcNormalPosition
'  xMWinPos.Right = xMWinPos.Right - xMWinPos.Left
'  xMWinPos.Bottom = xMWinPos.Bottom - xMWinPos.Top
''Debug.Print "GetMyWindowEx:", xMWinPos.Left, xMWinPos.Top, xMWinPos.Right, xMWinPos.Bottom
'  End Sub

Public Function GetExtension(ByVal xChar1 As String) As String
  GetExtension = LCase(Mid(xChar1, InStrRev(xChar1, ".") + 1&))
  End Function

Public Function FileExists(ByVal xChar1 As String) As Boolean
  FileExists = (Not GetFileAttributes(xChar1) = -1&)
  End Function

Private Function NonRealKey(ByVal KeyCode As Long) As Boolean
  Select Case KeyCode
    Case 5&, 6& ', &HE7&, &HE8&
      NonRealKey = True
    End Select
  End Function



Public Function UnassignedKey(ByVal KeyCode As Long) As Boolean
  Select Case (KeyCode And &HFF&)
    Case &H88& To &H8F&, &H92& To &HF5&
      UnassignedKey = True
    End Select
  End Function

Public Sub UpdatePriority(ByVal NewPriority As Long)
#If 1 Then
  Select Case NewPriority
    Case -2&: PlayerPriority = &H40& 'idle
    
    Case -1&
      If IsMinWindows2000W Then
        PlayerPriority = &H4000&
        Else
        PlayerPriority = &H20&
        End If
    Case 0&: PlayerPriority = &H20&
    Case 1&
      If IsMinWindows2000W Then
        PlayerPriority = &H8000&
        Else
        PlayerPriority = &H20&
        End If
    Case 2&: PlayerPriority = &H80& 'high
    Case 3&: PlayerPriority = &H100& 'realtime
    End Select
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "updating priority (Process " & Hex(GetCurrentProcessId) & ")"
    #End If
  Call SetPriorityClass(GetCurrentProcess, PlayerPriority)
  PlayerPriority = NewPriority

#Else
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xBool1 As Boolean
  Dim t32stru As THREAD32ENTRY
  
  Select Case NewPriority
    Case -3&: PlayerPriority = &H40: xLong1 = -15&
    Case -2&: PlayerPriority = &H20&: xLong1 = -2&
    
    Case -1&: PlayerPriority = &H20&: xLong1 = -1&
    Case 0&: PlayerPriority = &H20&: xLong1 = 0&
    Case 1&: PlayerPriority = &H20&: xLong1 = 1&
    
    Case 2&: PlayerPriority = &H80: xLong1 = 2&
    Case 3&: PlayerPriority = &H100: xLong1 = 15&
    End Select
  
  #If UseFileLogging Then
    MainForm_ctConsoleA_AddLog "updating priority (Process " & Hex(GetCurrentProcessId) & ")"
    #End If
  
  Call SetPriorityClass(GetCurrentProcess, PlayerPriority)

  PlayerPriority = CreateToolhelp32Snapshot(&H4&, xProcID) 'procid
  t32stru.dwSize = Len(t32stru)
  xBool1 = Thread32First(PlayerPriority, t32stru)
  Do
    If t32stru.th32OwnerProcessID = xProcID Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  updating thread " & Hex(t32stru.th32ThreadID) & " to " & CStr(xLong1)
        #End If
      On Error GoTo Er1
      xLong2 = OpenThread(&H40&, 0, t32stru.th32ThreadID)
      On Error GoTo 0
      If Not xLong2 = 0& Then
        Call SetThreadPriority(xLong2, xLong1)
        CloseHandle_ (xLong2)
        End If
      End If
'      DoEvents
    xBool1 = Thread32Next(PlayerPriority, t32stru)
    Loop Until (xBool1 = False)
Rs1:
  On Error GoTo 0
  Call CloseHandle(PlayerPriority)
  PlayerPriority = NewPriority
  Exit Sub

Er1:
  Resume Rs1
#End If
  End Sub

Public Function CreateCompatibleBitmapX(ByVal Width As Long, ByVal Height As Long) As Long
  Dim xLong1 As Long

  xLong1 = GetDC(0&)
  If Not xLong1 = 0& Then
    CreateCompatibleBitmapX = CreateCompatibleBitmap(xLong1, Width, Height)
    Call ReleaseDC(0&, xLong1)
    End If
  End Function

Private Sub UnTheme()
  If Not hThemeComboDropButtons = 0& Then
    CloseThemeData (hThemeComboDropButtons)
    hThemeComboDropButtons = 0&
    End If
  End Sub

Private Sub DoThemeUpdates()
  Call UnTheme
  On Error GoTo Er1
  If Not IsThemeActive = 0& Then
    On Error GoTo 0
    If Not IsAppThemed = 0& Then
      hThemeComboDropButtons = OpenThemeData(MainHWnd, StrPtr("SCROLLBAR"))
      End If
Rs1:
    On Error GoTo 0
    End If
  Exit Sub

Er1:
  Resume Rs1
  End Sub

'Private Function SMQueryString(ByVal wParam As Long, ByVal lParam As Long) As Long ', ByVal AsUnicode As Boolean) As Long
Private Function SMQueryString(ByVal wParam As Long, ByVal MapFileID As Long) As Long ', ByVal AsUnicode As Boolean) As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim hMapFile As Long
  Dim hMapFileView As Long
  Dim hMapFileSize As Long
  Dim xChar1 As String
  Dim xChar2 As String
  
  hMapFile = OpenFileMappingA(2&, 0&, "calaym_" + CStr(MapFileID))
  'If Not MapFileID = 0& Then
  If Not hMapFile = 0& Then
    hMapFileView = MapViewOfFile(hMapFile, &HF001F, 0, 0, 0&)  'FILE_MAP_ALL_ACCESS
    If hMapFileView Then
      'Call RtlMoveMemory(ByVal VarPtr(hMapFileSize), ByVal hMapFileView, 4&)
      hMapFileSize = GetAccessSize(hMapFileView) \ 2&
      If Not (hMapFileSize < 2&) Then
        Call RtlMoveMemory(ByVal hMapFileView, ByVal VarPtr(xLong1), 2&)
        Select Case wParam
          Case 800&, 801&
            If xPStateIconPlaying Then
              If xPStateIconPaused Then
                xLong1 = &H200&
                Else
                xLong1 = &H100&
                End If
              'If xPStateIconPlaying And Not (xPStateIconSndcard Or xPStateIconQuit Or CurMusic = ctSSLayer_MusicTypeNone Or xPStateIconStartup) Then
              xChar2 = String(4&, vbNullChar)
              If Not (xPStateIconSndcard Or xPStateIconQuit Or CurMusic = ctSSLayer_MusicTypeNone Or xPStateIconStartup Or PLCurSongLength = 0&) Then
                'If CurMusic = ctSSLayer_MusicTypeTracker Then
                '  xLong1 = PLCurSongLength Or &H80000000
                '  Else
                '  xLong1 = PLCurSongLength And &H7FFFFFFF
                '  End If
                Call RtlMoveMemory(ByVal StrPtr(xChar2), ByVal VarPtr(PLCurSongLength), 4&)
                xChar1 = GetCurSong(wParam = 801&)
                End If
              If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
                xLong2 = ctSSLayer_MusicTypeInetStream
                Else
                xLong2 = CurMusic
                End If
              If xOldIconState = -1& Then
                xChar1 = xChar1 + vbNullChar + ChrW(3& + xLong1) + ChrW(xLong2) + xChar2
                Else
                xChar1 = xChar1 + vbNullChar + ChrW(xOldIconState + xLong1) + ChrW(xLong2) + xChar2
                End If
              xLong1 = Len(xChar1)
              If Not xLong1 > hMapFileSize Then
                SMQueryString = xLong1 - 7&
                Call RtlMoveMemory(ByVal hMapFileView, ByVal StrPtr(xChar1), xLong1 * 2&)
                End If
              End If
          Case 804&
            xChar1 = LastLoudestTearFn & vbNullChar
            GoTo SimpleCopy
          Case 805&
            xChar1 = LastLoudestTearCp & vbNullChar
SimpleCopy:
            If LastLoudestTearID = -1& Then
              If xPStateIconPaused Then
                xLong1 = &H200&
                Else
                xLong1 = &H100&
                End If
              If xOldIconState = -1& Then
                SMQueryString = -(3& + xLong1)
                Else
                SMQueryString = -(xOldIconState + xLong1)
                End If
              Else
              xLong1 = Len(xChar1)
              If Not (xLong1 = 1& Or xLong1 > hMapFileSize) Then
                SMQueryString = xLong1 - 1&
                Call RtlMoveMemory(ByVal hMapFileView, ByVal StrPtr(xChar1), xLong1 * 2&)
                End If
              End If
          Case 810& To 817&
            Select Case wParam
              Case 810&
                For xLong1 = 0& To xLastPathCount - 1&
                  xChar1 = xChar1 & vbCrLf & GetTimeSMQ(xLong1) & SimpleFile(xLastPath(xLong1))
                  Next
              Case 811&
                For xLong1 = 0& To xLastPathCount - 1&
                  xChar1 = xChar1 & vbCrLf & GetTimeSMQ(xLong1) & xLastPath(xLong1)
                  Next
              Case 812&
                For xLong1 = 0& To xLastPathCount - 1&
                  xChar1 = xChar1 & vbCrLf & SimpleFile(xLastPath(xLong1))
                  Next
              Case 813&
                For xLong1 = 0& To xLastPathCount - 1&
                  xChar1 = xChar1 & vbCrLf & xLastPath(xLong1)
                  Next
              Case 814&
                For xLong1 = xLastPathCount - 1& To 0& Step -1&
                  xChar1 = xChar1 & vbCrLf & GetTimeSMQ(xLong1) & SimpleFile(xLastPath(xLong1))
                  Next
              Case 815&
                For xLong1 = xLastPathCount - 1& To 0& Step -1&
                  xChar1 = xChar1 & vbCrLf & GetTimeSMQ(xLong1) & xLastPath(xLong1)
                  Next
              Case 816&
                For xLong1 = xLastPathCount - 1& To 0& Step -1&
                  xChar1 = xChar1 & vbCrLf & SimpleFile(xLastPath(xLong1))
                  Next
              Case 817&
                For xLong1 = xLastPathCount - 1& To 0& Step -1&
                  xChar1 = xChar1 & vbCrLf & xLastPath(xLong1)
                  Next
              End Select
            xLong1 = Len(xChar1) - 1&
            If Not (xLong1 = -1& Or xLong1 > hMapFileSize) Then
              xChar1 = Mid(xChar1, 3&) & vbNullChar
              SMQueryString = xLong1 - 1&
              Call RtlMoveMemory(ByVal hMapFileView, ByVal StrPtr(xChar1), xLong1 * 2&)
              End If
          End Select
        End If
      Call UnmapViewOfFile(hMapFileView)
      End If
    Call CloseHandle(hMapFile)
    End If
  End Function

Private Function GetTimeSMQ(ByVal Index As Long)
  GetTimeSMQ = xLastPathTime(Index) + "   "
  End Function

Public Function GetListIndexMS(ByVal hWndListBox As Long, ByRef ReturnValue As Long) As Boolean
  ReturnValue = SendMessage(hWndListBox, &H19F&, 0&, 0&)
  GetListIndexMS = (Not (ReturnValue = -1&))
  End Function

Public Sub SetListIndexMS(ByVal hWndListBox As Long, ByVal ListIndex As Long)
  Call SendMessage(hWndListBox, &H185, 0&, -1&)
  Call SendMessage(hWndListBox, &H185, 1&, ListIndex)
  Call SendMessage(hWndListBox, &H186, 1&, ListIndex)
  'Call SendMessage(hwndlistbox, &H14E&, listindex, 0&)
  Call SendMessage(hWndListBox, &H14E&, ListIndex, 0&)
  End Sub

Public Sub GrabError()
  On Error Resume Next
  GrabErrorVar = Timer
  On Error GoTo 0
  End Sub

Public Sub GetIsAutoRun()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  
  Reg_IsAutoRun = False
  If RegOpenKeyEx(&H80000001, "Software\Microsoft\Windows\CurrentVersion\Run", 1&, xLong1) Then
    xLong2 = 1024&: xChar1 = String(1025&, vbNullChar)
    If RegQueryValueEx_String(xLong1, LoadResEx_String(3500&), xChar1) Then
      Reg_IsAutoRun = True
      End If
    Call RegCloseKey(xLong1)
    End If
  End Sub

Private Function GetCurSong(ByVal Shortened As Boolean) As String
  If CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3& Then
    If Shortened Then
      GetCurSong = PLCurSongDisplay
      Else
      GetCurSong = PLCurFullURL
      End If
    ElseIf Shortened Then
    GetCurSong = SimpleFile(PLCurSong)
    Else
    GetCurSong = PLCurSong
    End If
  End Function

'private Function CreateCompatibleDC(ByVal hdc As Long) As Long
'  CreateCompatibleDC = iCreateCompatibleDC(hdc)
'  Debug.Print "Loaded: ", hdc
'  End Function
'
'private Function DeleteDC(ByVal hdc As Long) As Long
'  DeleteDC = iDeleteDC(hdc)
'  End Function

Private Function RemoveDevicesLock(ByVal lParam As Long, ByVal IsOnQuery As Boolean) As Boolean
  Dim xLong1 As Long
  Dim xType1 As DEV_BROADCAST_VOLUME
  Dim xChar1 As String
  
  If IsDevicePlugging Then
    RemoveDevicesLock = True
    ElseIf Not (PlayerIsLocked Or xAutoDoing Or xPStateIconQuit) Then
    Call RtlMoveMemory(xType1, ByVal lParam, 8&)
    If xType1.dbcv_devicetype = 6& Then
      xLong1 = RemovableDevices_ResolveDriveID(lParam)
      If Not xLong1 = -1& Then
        xChar1 = Chr(xLong1 + 65&)
        End If
      GoTo ByBrdCstHnd
      ElseIf xType1.dbcv_devicetype = 2& Then
      Call RtlMoveMemory(xType1, ByVal lParam, 16&)
      For xLong1 = FirstNonFloppy To 25&
        If (xType1.dbcv_unitmask And (2& ^ xLong1)) Then
          xChar1 = xChar1 + Chr(65 + xLong1)
          End If
        Next
ByBrdCstHnd:
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  AffectedVolumes(s): " + xChar1
        #End If

      If Mid(xRealBaseFile, 2&, 1&) = ":" Then
        If Not InStr(xChar1, UCase(Left(xRealBaseFile, 1&))) = 0& Then
          If xPStateIconQuit Then
            RemoveDevicesLock = True
            ElseIf xAutoUSBQuit Then
  '          Exit Function
  'QuitHack: ' testing
            If xCmdQuitMode = 3& And IsOnQuery Then
              If IsDevicePlugging Then
                If RemoveDevicesLock Then
                  IsDevicePlugging = False
                  HasDevicePlugStoppedSong = False
                  SetAutoDoing False
                  xPStateIconPlugging = False
                  xPStateIconSndcard = False
                  'MainForm_UpdateMainIcon
                  MainForm_LockMe False
                  Call SendMessage(MainHWnd, &H10&, 0&, 0&)
                  If xPStateIconQuit Then
                    'RemoveDevicesLock = True
                    Exit Function
                    End If
                  End If
                Else
                Call SendMessage(MainHWnd, &H10&, 0&, 0&)
                If xPStateIconQuit Then
                  RemoveDevicesLock = True
                  'Exit Function
                  End If
                End If
              End If
            End If
          Exit Function
          End If
        End If
      
      MainForm_LockMe True
      SetAutoDoing True
      xPStateIconSndcard = True
      xPStateIconPlugging = True
      MainForm_UpdateMainIcon
'      DoEventsEx 3000&
      IsDevicePlugging = True
      RemoveDevicesLock = True

      For xLong1 = 0& To xAutoDirCount - 1&
        If Not xAutoDirHWnd(xLong1) = 0& Then
          If Mid(xAutoDir(xLong1), 2&, 1&) = ":" Then
            If Not InStr(xChar1, UCase(Left(xAutoDir(xLong1), 1&))) = 0& Then
              #If UseFileLogging Then
                MainForm_ctConsoleA_AddLog "  Temp. disable AR for " + xAutoDir(xLong1)
                #End If
              Call FindCloseChangeNotification(xAutoDirHWnd(xLong1))
              xAutoDirHWnd(xLong1) = 0&
              End If
            End If
          End If
        Next
'GoTo QuitHack
      Else
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  ignored (no volume!)"
        #End If
      RemoveDevicesLock = True
      End If
    End If
  End Function

Private Function RemoveDevicesAffectsVolumes(ByVal lParam As Long) As Boolean
  Dim xType1 As DEV_BROADCAST_VOLUME
  
  'If IsDevicePlugging Then
    Call RtlMoveMemory(xType1, ByVal lParam, 8&)
    RemoveDevicesAffectsVolumes = (xType1.dbcv_devicetype = 2& Or xType1.dbcv_devicetype = 6&)
  '  End If
  End Function

Private Sub RemoveDevicesUnlock()
  Dim xLong1 As Long
'Debug.Print "Unlock?"
  If IsDevicePlugging Then
'Debug.Print "  Unlock!"
    IsDevicePlugging = False
    HasDevicePlugStoppedSong = False
    SetAutoDoing False
    xPStateIconPlugging = False
    xPStateIconSndcard = False
    If xAutoRefreshTimer(1) > 0& Then
      For xLong1 = 0& To xAutoDirCount - 1&
        If xAutoDirHWnd(xLong1) = 0& Then
          If Mid(xAutoDir(xLong1), 2&, 1&) = ":" Then
            #If UseFileLogging Then
              MainForm_ctConsoleA_AddLog "  Re-Enable AR for " + xAutoDir(xLong1)
              #End If
            xAutoDirHWnd(xLong1) = FindFirstChangeNotification(xAutoDir(xLong1), 1&, 3&)
            End If
          End If
        Next
      End If
    MainForm_UpdateMainIcon
    MainForm_LockMe False
    End If
  End Sub

Private Sub RemoveDevices(ByVal lParam As Long, ByVal AffectPL As Boolean)
  Dim xLong1 As Long
  Dim xChar1 As String
  Dim xType1 As DEV_BROADCAST_VOLUME

  If IsDevicePlugging Or Not (PlayerIsLocked Or xAutoDoing Or xPStateIconQuit) Then
    Call RtlMoveMemory(xType1, ByVal lParam, 8&)
    
    If xType1.dbcv_devicetype = 6& Then
      'Stop
      xLong1 = RemovableDevices_ResolveDriveID(lParam)
      If Not xLong1 = -1& Then
        xChar1 = Chr(xLong1 + 65&)
        xType1.dbcv_unitmask = 2& ^ xLong1
        GoTo ByBrdCstHnd
        End If
      ElseIf xType1.dbcv_devicetype = 2& Then
      Call RtlMoveMemory(xType1, ByVal lParam, 16&)
      For xLong1 = FirstNonFloppy To 25&
        If (xType1.dbcv_unitmask And (2& ^ xLong1)) Then
          xChar1 = xChar1 + Chr(65& + xLong1)
          End If
        Next
ByBrdCstHnd:
      IsDevicePlugging = True
      HasDevicePlugged = True
      MainForm_LockMe True
      xPStateIconSndcard = True
      xPStateIconPlugging = True
      MainForm_UpdateMainIcon
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  AffectedDevices: " + xChar1
        #End If
      
      If Not (CurMusic = ctSSLayer_MusicTypeNone) And xPStateIconPlaying Then
        If Mid(PLCurSong, 2&, 2&) = ":\" Then
'          Debug.Print "Mask:", Hex(xType1.dbcv_unitmask)
          If Not (xType1.dbcv_unitmask And (2& ^ (Asc(UCase(Left(PLCurSong, 1&))) - 65&))) = 0& Then
            'If xPStateIconPaused Then
            '  xPStateIconPlaying = False
            '  xReAutoPlay = False
            '  ReAutoPlayDR = False
            '  Else
            '  HasDevicePlugStoppedSong = True
            '  End If
            'MainForm_xEndSong (Not xPStateIconPaused), False, False
            Call StoreResume(False, 2000&)
            HasDevicePlugStoppedSong = True
#If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  CurrentSong: affected"
            Else
            MainForm_ctConsoleA_AddLog "  CurrentSong: not affected"
#End If
            End If
#If UseFileLogging Then
          Else
          MainForm_ctConsoleA_AddLog "  CurrentSong: not affected"
#End If
          End If
#If UseFileLogging Then
        Else
        MainForm_ctConsoleA_AddLog "  CurrentSong: not affected"
#End If
        End If
      If AffectPL Then
        If Mid(PlNextSong, 2&, 2&) = ":\" Then
          If Not (xType1.dbcv_unitmask And (2& ^ (Asc(UCase(Left(PlNextSong, 1&))) - 65&))) = 0& Then
            SetNextSong False
#If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  NextSong: affected"
            Else
            MainForm_ctConsoleA_AddLog "  NextSong: not affected"
#End If
            End If
#If UseFileLogging Then
          Else
          MainForm_ctConsoleA_AddLog "  NextSong: not affected"
#End If
          End If
        End If
      
      If AffectPL And (xRemoveMissingSongs Or vKioskMode Or Not xAutoDirCount = 0&) Then
        #If UseFileLogging Then
          MainForm_ctConsoleA_AddLog "  Removing affected songs from list"
          #End If
        MenuEventCDDrive xChar1, False, xRemoveMissingSongs Or vKioskMode
        End If
  
      If HasDevicePlugStoppedSong Or CurMusic = ctSSLayer_MusicTypeNone Then 'And xPStateIconPlaying Then
        Call Tear_WaitFor(-2&)
        Else
        Call Tear_WaitFor(-3&)
        End If
      'Stop
      End If
    End If
  End Sub

Private Sub RemoveDevicesAutoAdd(ByVal lParam As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xType1 As DEV_BROADCAST_VOLUME

  If Not (PlayerIsLocked Or xAutoDoing Or xPStateIconQuit) Then
    Call RtlMoveMemory(xType1, ByVal lParam, 8&)
    If xType1.dbcv_devicetype = 2& Then
      Call RtlMoveMemory(xType1, ByVal lParam, 16&)
      For xLong1 = FirstNonFloppy To 25&
        If (xType1.dbcv_unitmask And (2& ^ xLong1)) Then
          xChar2 = Chr(65 + xLong1)
          If Not InStr(CDDriveLetters, xChar2) = 0& Then
            xChar1 = xChar1 + Chr(65 + xLong1)
            End If
          End If
        Next
ByBrdCstHnd:
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "  AffectedDevices: " + xChar1
        #End If
      If Not Len(xChar1) = 0& Then
        'ReAutoPlayDR = False
        MainForm_LockMe True
        xPStateIconRefreshing = True
        SetAutoDoing True
        MainForm_UpdateMainIcon
        
        If vKioskMode Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  Refresh (drives: " + xChar1 + ")"
            #End If
          MenuEventCDDrive xChar1, True, True
          Else
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "Trying AutoRefresh on new device (drives: " + xChar1 + ")"
            #End If
          AbortReading = False
          xLong2 = PLCount
          For xLong1 = 0& To xAutoDirCount - 1&
            If Mid(xAutoDir(xLong1), 2&, 1&) = ":" Then
              If Not InStr(xChar1, UCase(Left(xAutoDir(xLong1), 1&))) = 0& Then
                If Not xAutoDirHWnd(xLong1) = 0& Then
                  Call FindCloseChangeNotification(xAutoDirHWnd(xLong1))
                  End If
                #If UseFileLogging Then
                  MainForm_ctConsoleA_AddLog "  adding " + xAutoDir(xLong1)
                  #End If
                ReadDirectory xAutoDir(xLong1), 0&, True, PLI_IsAuto
                If xAutoRefreshTimer(1) > 0& Then
                  xAutoDirHWnd(xLong1) = FindFirstChangeNotification(xAutoDir(xLong1), 1&, 3&)
                  End If
                If AbortReading Then Exit For
                End If
              End If
            Next
          If Not PLCount = xLong2 Then
            RealPLAdd xLong2
            End If
          End If
        
        xPStateIconRefreshing = False
        MainForm_UpdateMainIcon
        SetAutoDoing False
        MainForm_LockMe False
        CheckReplay
        CheckOldItems
        xAutoRefreshTimerEventTime(0) = DoEventsEx_GetTickCount
        xAutoRefreshTimerEventTime(1) = xAutoRefreshTimerEventTime(0)
        InitResearch False
        
        #If XPDeviceRemoveDetect Then
          #If XPDeviceRemoveDetectPST Then
            PendingDeviceRegB = True
            PendingDeviceRegT = DoEventsEx_GetTickCount
            #Else
            Call RemovableDevices_UnregisterAll
            Call RemovableDevices_RegisterAll(MainHWnd)
            #End If
          #End If
        End If
      '#If UseFileLogging Then
      '  MainForm_ctConsoleA_AddLog "  AutoAddDevices: " + xChar1
      '  #End If
      End If
    End If
  End Sub
  
Public Sub DrawPLItem(ByVal lParam As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xDrawMode As Long
  Dim xType1 As DRAWITEMSTRUCT
  
  Dim xType2 As RECT
  Dim xType3 As RECT
  Dim xULCheck As Long
  Dim xULColor As Long
  Dim ThisTimeShift As Long
  Dim xFntOld As Long
  
  '11/12 = 44/48
  Call RtlMoveMemory(xType1, ByVal lParam, 48&)
'Dim xOrgOrgID As Long
'xOrgOrgID = xType1.itemID
'xOrgOrgID = SendMessage(xType1.hwndItem, LB_GETTOPINDEX, 0&, 0&)
'If xType1.itemAction = 4& Then
  'If Not xType1.itemID = -1& Then
  '  If IsUnicode Then
  '    If SendMessageW(xType1.hwndItem, &H198&, xType1.itemID, xType1.rcItem) = -1& Then Exit Sub
  '    Else
  '    If SendMessageA(xType1.hwndItem, &H198&, xType1.itemID, xType1.rcItem) = -1& Then Exit Sub
  '    End If
  '  End If
'      If IsUnicode Then
'        Call DefWindowProcW(xType1.hwndItem, &H2B&, 0&, lParam)
'        Else
'        Call DefWindowProcA(xType1.hwndItem, &H2B&, 0&, lParam)
'        End If
  
'  GoTo NoDraw
'  Exit Sub
'  End If

  If Not xType1.hdc = 0& Then
    Select Case xType1.hwndItem
      Case xListHWnd(0)
        If Not xType1.ItemID = -1& Then
          xType1.ItemID = xDirPath(xType1.ItemID)
          'Debug.Print "Cap:", PLI(xType1.itemID).Caption
          If ShowFullPath Then
            If PLIconFolder = 0& Then
              xDrawMode = PLIconFolderFake
              Else
              xDrawMode = PLIconFolder
              End If
            End If
'Debug.Print "Drawing " + PLI(xType1.itemID).Caption + " in " + CStr(xDrawMode), ShowFullPath
          GoTo IndirectDirs
          End If
      Case xListHWnd(1)
        If Not xType1.ItemID = -1& Then
          'xType1.itemID = xDirPath(xType1.itemID)
          If PLIconFolder = 0& Then
            xDrawMode = PLIconFileFake
            Else
            xDrawMode = PLIconFile
            End If
          GoTo IndirectDirs
          End If
      Case Interface_Control(Interface_Control_Search_Results)
        ThisTimeShift = PLODIconTopShift
        If xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo Then
          xDrawMode = -2&
          Else
          xDrawMode = -1&
          End If
'        GoTo IndirectDirsFonter
        GoTo IndirectDirs
      Case Interface_Control(Interface_Control_Radio_List)
        ThisTimeShift = PLODIconTopShift
        'If xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo Then
        If PLIconURL = 0& Then
          xDrawMode = -5&
          Else
          xDrawMode = -4& 'pliconurl
          End If
'        GoTo IndirectDirsFonter
        GoTo IndirectDirs
      Case xListHWnd(2)
        xDrawMode = 0&
        xULCheck = PLI_IsDir
        ThisTimeShift = PLODIconTopShift
        
'Debug.Print "ItemData: ", xType1.itemData, "for", xType1.itemID
        If Not xType1.ItemID = -1& Then
'Dim xOrgID As Long
'xOrgID = xType1.itemID
          xType1.ItemID = xType1.itemData
'IndirectDirsFonter:
#If BoldDirs = 98 Then
          If (PLI(xType1.itemData).DirDepthFlags And PLI_IsDir) = 0& Then
#Else
          If (PLIDF(xType1.itemData) And PLI_IsDir) = 0& Then
#End If
            xFntOld = SelectObject(xType1.hdc, hFontPLLg)
            End If
          End If
IndirectDirs:
        
        xLong1 = GetBkColor(xType1.hdc)
        xLong2 = GetTextColor(xType1.hdc)
        xType3 = xType1.rcItem
        
        If (xType1.itemState And &H1&) Then
#If ListFocusedLists Then
          If xType1.hwndItem = xCurFocusHWnd Then
#End If
            Call FillRect(xType1.hdc, xType1.rcItem, 14&)
            Call SetBkColor(xType1.hdc, GetSysColor(13&))
            Call SetTextColor(xType1.hdc, GetSysColor(14&))
#If ListFocusedLists Then
            Else
            Call FillRect(xType1.hdc, xType1.rcItem, 16&)
            Call SetBkColor(xType1.hdc, GetSysColor(15&))
            Call SetTextColor(xType1.hdc, GetSysColor(8&))
            End If
#End If
          Else
          Call FillRect(xType1.hdc, xType1.rcItem, 6&)
          End If
          
        If Not xType1.ItemID = -1& Then
          Select Case xDrawMode
            Case 0&
              #If BoldDirs = 98 Then
                xLong3 = 24& + 20& * (PLI(xType1.ItemID).DirDepthFlags And PLI_Depth)
                #Else
                xLong3 = 24& + 20& * (PLIDF(xType1.ItemID) And PLI_Depth)
                #End If
              xType1.rcItem.Right = xType1.rcItem.Right - xLong3
              If Not xType1.rcItem.Right < 1& Then
                xType1.rcItem.Left = xType1.rcItem.Left + xLong3
                'PLICap(xType1.itemID)
'Debug.Print "  DragLessOk: ", PLI(xType1.itemID).Caption
#If BoldDirs = 98 Then
                'Call DrawText(xType1.hdc, PLI(xType1.itemID).Caption + " - " + CStr(xOrgOrgID) + "," + CStr(xOrgID) + "," + CStr(xType1.itemID) + "," + CStr(xType1.CtlType) + "," + CStr(xType1.hwndItem) + "," + CStr(xType1.itemAction) + "," + CStr(xType1.hdc) + "," + CStr(xType1.itemID) + "," + CStr(xType1.itemState), xType1.rcItem, &H924&)  '&H104&)
                Call DrawText(xType1.hdc, PLI(xType1.ItemID).Caption, xType1.rcItem, &H924&)   '&H104&)
                If (PLI(xType1.ItemID).DirDepthFlags And xULCheck) = PLI_IsDir Then
                  xType2 = xType1.rcItem
                  Call DrawText(xType1.hdc, PLI(xType1.ItemID).Caption, xType2, &HD24&)
#Else
                Call DrawText(xType1.hdc, PLICap(xType1.ItemID), xType1.rcItem, &H924&) '&H104&)
                If (PLIDF(xType1.ItemID) And xULCheck) = PLI_IsDir Then
                  xType2 = xType1.rcItem
                  Call DrawText(xType1.hdc, PLICap(xType1.ItemID), xType2, &HD24&)
#End If
SearchPostUnderline:
                  xType2.Top = xType2.Bottom - 1&
                  xType2.Left = xType2.Left - 3&
                  xType2.Right = xType2.Right + 4&
                  'If xType2.Left < xType1.rcItem.Left - xLong3 Then xType2.Left = xType1.rcItem.Left - xLong3
                  'If xType2.Right > xType1.rcItem.Right Then xType2.Right = xType1.rcItem.Right
                  
#If ListFocusedLists Then
                  If (xType1.itemState And &H1&) And xType1.hwndItem = xCurFocusHWnd Then
#Else
                  If (xType1.itemState And &H1&) Then
#End If
                    xULColor = 15&
                    Else
                    xULColor = 9&
                    End If
                  Call FillRect(xType1.hdc, xType2, xULColor)
                  Else
                  xULCheck = 0&
                  End If
                xType1.rcItem.Left = xType1.rcItem.Left - xLong3
                End If
            
            Case PLIconFolder, PLIconFolderFake ', PLIconURL
'                If Not xType1.itemID < 'PLRemoveShifted1 Then xType1.itemID = xType1.itemID + ('PLRemoveShifted2 - 'PLRemoveShifted1) + 1&
              'If PLIconFolder = 0& Then
              '  Call TextOut(xType1.hdc, PLIFile(xType1.itemID), 4& + xType1.rcItem.Left, xType1.rcItem.Top)
              '  Else
              xType1.rcItem.Left = xType1.rcItem.Left + 24&
#If BoldDirs = 98 Then
                'Call TextOut(xType1.hdc, PLI(xType1.itemID).Filename, 24& + xType1.rcItem.Left, xType1.rcItem.Top)
                Call DrawText(xType1.hdc, PLI(xType1.ItemID).Filename, xType1.rcItem, &H924&)
#Else
                'Call TextOut(xType1.hdc, PLIFile(xType1.itemID), 24& + xType1.rcItem.Left, xType1.rcItem.Top)
                Call DrawText(xType1.hdc, PLIFile(xType1.ItemID), xType1.rcItem.Left, &H924&)
#End If
              xType1.rcItem.Left = xType1.rcItem.Left - 24&
              '  End If
            Case PLIconFile, PLIconFileFake
              xType1.rcItem.Left = xType1.rcItem.Left + 4&
              Call DrawText(xType1.hdc, xLastPathTime(xType1.ItemID), xType1.rcItem, &H924&)
              xType1.rcItem.Left = xType1.rcItem.Left + 20& + xLastPathTimeSpace
              Call DrawText(xType1.hdc, xLastPathCap(xType1.ItemID), xType1.rcItem, &H924&)
              xType1.rcItem.Left = xType1.rcItem.Left - 24& - xLastPathTimeSpace
            Case -1&
              xType1.rcItem.Left = xType1.rcItem.Left + 24&
              xDrawMode = xSearchSortDir(xSearchSortAlias(xType1.ItemID))
              If xDrawMode = -3& Then
                xFntOld = SelectObject(xType1.hdc, hFontPLLg)
                Call DrawText(xType1.hdc, xSearchSortCap(xSearchSortAlias(xType1.ItemID)), xType1.rcItem, &H924&) '&H104&)
                xType1.rcItem.Left = xType1.rcItem.Left - 24&
                Else
                Call DrawText(xType1.hdc, xSearchSortCap(xSearchSortAlias(xType1.ItemID)), xType1.rcItem, &H924&) '&H104&)
#If BoldDirs = 98 Then
                xType2 = xType1.rcItem
                Call DrawText(xType1.hdc, xSearchSortCap(xSearchSortAlias(xType1.ItemID)), xType2, &HD24&)
#Else
                xType2 = xType1.rcItem
                Call DrawText(xType1.hdc, xSearchSortCap(xSearchSortAlias(xType1.ItemID)), xType2, &HD24&)
#End If
                xType1.rcItem.Left = xType1.rcItem.Left - 24&
                xULCheck = PLI_IsDir
                GoTo SearchPostUnderline
                End If
            
            Case -2&
              Call SetTextColor(xType1.hdc, GetSysColor(17&))
              xType1.rcItem.Left = xType1.rcItem.Left + 4&
              Call DrawText(xType1.hdc, xSearchError, xType1.rcItem, &H924&)
              xType1.rcItem.Left = xType1.rcItem.Left - 4&
            Case -4&
              'Call SetTextColor(xType1.hdc, GetSysColor(17&))
              xType1.rcItem.Left = xType1.rcItem.Left + 24&
              Call DrawText(xType1.hdc, xRadioCap(xType1.ItemID), xType1.rcItem, &H924&)
              xType1.rcItem.Left = xType1.rcItem.Left - 24&
              'xDrawMode = PLIconURL
            Case -5&
              'Call SetTextColor(xType1.hdc, GetSysColor(17&))
              xType1.rcItem.Left = xType1.rcItem.Left + 4&
              Call DrawText(xType1.hdc, xRadioCap(xType1.ItemID), xType1.rcItem, &H924&)
              xType1.rcItem.Left = xType1.rcItem.Left - 4&
              'xDrawMode = PLIconURL
            End Select
          End If
        
NoDrawTxt:
        If Not xType1.ItemID = -1& Then
          Select Case xDrawMode
            Case 0&
#If BoldDirs = 98 Then
              If (PLI(xType1.ItemID).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
                If PLIconFolder = 0& Then
                  xLong3 = 4& + 20& * (PLI(xType1.ItemID).DirDepthFlags And PLI_Depth)
#Else
              If (PLIDF(xType1.ItemID) And PLI_IsDir) = PLI_IsDir Then
                If PLIconFolder = 0& Then
                  xLong3 = 4& + 20& * (PLIDF(xType1.ItemID) And PLI_Depth)
#End If
PostFakeIconDraw:
                  xLong4 = xType1.rcItem.Right
                  xType1.rcItem.Left = xType1.rcItem.Left + xLong3
                  If xType1.rcItem.Left + 16& < xType1.rcItem.Right Then
                    xType1.rcItem.Right = xType1.rcItem.Left + 16&
                    ElseIf xType1.rcItem.Left > xType1.rcItem.Left Then
                    GoTo NoDraw
                    End If
                  
                  'xType1.rcItem.Right = xType1.rcItem.Left + 20& + xType1.rcItem.Left + 20& * (PLIDF(xType1.itemID) And PLI_Depth)
                  'Call DrawText(xType1.hdc, Chr(187&), xType1.rcItem, &H926&)
                  Call DrawText(xType1.hdc, ">", xType1.rcItem, &H926&)
                  If xULCheck = PLI_IsDir Then
                    xType2 = xType1.rcItem
                    Call DrawText(xType1.hdc, ">", xType2, &HD26&)
                    xType2.Top = xType2.Bottom - 1&
                    xType2.Right = (xType2.Right - xType2.Left)
                    xType2.Left = xType1.rcItem.Right - xType2.Right - 3&
                    xType2.Right = xType1.rcItem.Right + 4&
                    'If xType2.Left < xType1.rcItem.Left Then xType2.Left = xType1.rcItem.Left
                    'If xType2.Right > xLong4 Then xType2.Right = xLong4
                    Call FillRect(xType1.hdc, xType2, xULColor)
                    End If
                  'Call TextOut(xType1.hdc, Chr(187&), 4& + xType1.rcItem.Left + 20& * (PLIDF(xType1.itemID) And PLI_Depth), xType1.rcItem.Top)
                  xType1.rcItem.Right = xLong4
                  Else
#If BoldDirs = 98 Then
                  Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLI(xType1.ItemID).DirDepthFlags And PLI_Depth), xType1.rcItem.Top + ThisTimeShift, PLIconFolder, 16&, 16&, 0&, 0&, 3&)
#Else
                  Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLIDF(xType1.ItemID) And PLI_Depth), xType1.rcItem.Top + ThisTimeShift, PLIconFolder, 16&, 16&, 0&, 0&, 3&)
#End If
                  End If
                ElseIf Not PLIconFolder = 0& Then
#If BoldDirs = 98 Then
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLI(xType1.ItemID).DirDepthFlags And PLI_Depth), xType1.rcItem.Top + ThisTimeShift, PLIconFile, 16&, 16&, 0&, 0&, 3&)
                'Call DrawIcon(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLI(xType1.itemID).DirDepthFlags And PLI_Depth), xType1.rcItem.Top, PLFolderIcon)
#Else
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLIDF(xType1.ItemID) And PLI_Depth), xType1.rcItem.Top + ThisTimeShift, PLIconFile, 16&, 16&, 0&, 0&, 3&)
                'Call DrawIcon(xType1.hdc, 4& + xType1.rcItem.Left + 20& * (PLIDF(xType1.itemID) And PLI_Depth), xType1.rcItem.Top, PLFolderIcon)
#End If
                End If
            Case PLIconFolder, PLIconFolderFake
              If PLIconFolder = 0& Then
                xLong3 = 4&
                GoTo PostFakeIconDraw
                Else
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left, xType1.rcItem.Top + ThisTimeShift, PLIconFolder, 16&, 16&, 0&, 0&, 3&)
                End If
            Case PLIconFile, PLIconFileFake
              If Not PLIconFolder = 0& Then
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left + xLastPathTimeSpace, xType1.rcItem.Top, PLIconFile, 16&, 16&, 0&, 0&, 3&)
                End If
            Case -3&
              If Not PLIconFolder = 0& Then
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left, xType1.rcItem.Top + ThisTimeShift, PLIconFile, 16&, 16&, 0&, 0&, 3&)
                End If
            Case -4&
              If Not PLIconFolder = 0& Then
                Call DrawIconEx(xType1.hdc, 4& + xType1.rcItem.Left, xType1.rcItem.Top + ThisTimeShift, PLIconURL, 16&, 16&, 0&, 0&, 3&)
                End If
            End Select
          If Not xFntOld = 0& Then
            Call SelectObject(xType1.hdc, xFntOld)
            End If
          End If
        Call SetBkColor(xType1.hdc, xLong1)
        Call SetTextColor(xType1.hdc, xLong2)
#If MainMenuCloseButton Then
      Case Else
        'Private Const ODT_MENU As Long = 1
        If xType1.CtlType = 1& Then
          Call MainMenuCloseButton_DrawItem(lParam)
          End If
#End If
      End Select
NoDraw:
    If (xType1.itemState And &H10&) Then
      Call DrawFocusRect(xType1.hdc, xType3)
      'If IsUnicode Then
      '  Call DefWindowProcW(hwnd, Message, wParam, lParam)
      '  Else
      '  Call DefWindowProcA(hwnd, Message, wParam, lParam)
      '  End If
      End If
    End If
  End Sub

'Commonfiles
'commondir
'DialogTimer
'MessageBox
'winhelp?

Private Sub RealPLAdd(ByVal xFirst As Long)
  Dim xLong1 As Long
  Dim xDC1 As Long
  Dim xDC2 As Long
  Dim xDCD As Long
  Dim xDCB As Long
  Dim xFntD As Long
  Dim xFntB As Long
  Dim xFntS As Long
  
  'If Not xFirst = xLast - 1& Then
    xDC1 = CreateCompatibleDC(0&)
    If xDC1 = 0& Then
      xDCD = xSharedDC
      xDCB = xSharedDC
      Else
      xDCD = xDC1
      xFntD = SelectObject(xDC1, hFontPLLg)
      xDC2 = CreateCompatibleDC(0&)
      If xDC2 = 0& Then
        xDCB = xDC1
        Else
        xDCB = xDC2
        xFntB = SelectObject(xDC2, hFontPLLgB)
        End If
      End If
    
    If Not LockWinUpd = 0& Then
      LockWindowUpdate xListHWnd(2)
      End If
    
    xFntS = SelectObject(xSharedDC, hFontPLSm)
    
    If ShowFullPath Then
      For xLong1 = xFirst To PLCount - 1&
        #If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
            PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCB, PLI(xLong1).Caption)
            PLI(xLong1).pxWidthSm = GetTextOutWidth(xSharedDC, PLI(xLong1).Filename)
            Else
            PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCD, PLI(xLong1).Caption)
            End If
          #Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
            PLIPxLg(xLong1) = GetTextOutWidth(xDCB, PLICap(xLong1))
            PLIPxSm(xLong1) = GetTextOutWidth(xSharedDC, PLIFile(xLong1))
            Else
            PLIPxLg(xLong1) = GetTextOutWidth(xDCD, PLICap(xLong1))
            End If
          #End If
        
        Call SendMessage(xListHWnd(2), &H180&, 0&, xLong1)
        If xLong1 Mod 128& = 0& Then
          If LockWinUpd = 0& Then
            DoEventsEx_SleepPeriodical
            ElseIf LockWinUpd = 1& Then
            DoEventsEx_SleepPeriodicalLW xListHWnd(2)
            End If
          End If
        Next
      Else
      For xLong1 = xFirst To PLCount - 1&
        #If BoldDirs = 98 Then
          If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
            PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCB, PLI(xLong1).Caption)
            PLI(xLong1).pxWidthSm = GetTextOutWidth(xSharedDC, PLI(xLong1).Caption) + (PLI(xLong1).DirDepthFlags And PLI_Depth) * 20&
            Else
            PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCD, PLI(xLong1).Caption)
            End If
          #Else
          If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
            PLIPxLg(xLong1) = GetTextOutWidth(xDCB, PLICap(xLong1))
            PLIPxSm(xLong1) = GetTextOutWidth(xSharedDC, PLICap(xLong1)) + (PLIDF(xLong1) And PLI_Depth) * 20&
            Else
            PLIPxLg(xLong1) = GetTextOutWidth(xDCD, PLICap(xLong1))
            End If
          #End If
        
'Debug.Print "Cap: ", PLI(xLong1).Caption
'Debug.Print "Vals: ", PLI(xLong1).DirDepthFlags And PLI_Depth, PLI(xLong1).pxWidthLg, PLI(xLong1).pxWidthSm
        
        Call SendMessage(xListHWnd(2), &H180&, 0&, xLong1)
        If xLong1 Mod 128& = 0& Then
          If LockWinUpd = 0& Then
            DoEventsEx_SleepPeriodical
            ElseIf LockWinUpd = 1& Then
            DoEventsEx_SleepPeriodicalLW xListHWnd(2)
            End If
          End If
        Next
      End If
    
    Call SelectObject(xSharedDC, xFntS)
    If Not xDC1 = 0& Then
      Call SelectObject(xDC1, xFntD)
      Call DeleteDC(xDC1)
      If Not xDC2 = 0& Then
        Call SelectObject(xDC2, xFntB)
        Call DeleteDC(xDC2)
        End If
      End If
    
    'If Not LockWinUpd = 0& Then
    '  LockWindowUpdate 0&
    '  End If
    MainForm_xBuildDirs
    'End If
  End Sub

Public Sub MainForm_xBuildDirs()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong5 As Long
  Dim xLong6 As Long
  'Dim xType1 As POINTAPI
  
  If xDirList < 0& And InterfaceMode = Interface_Mode_None Then
    ShowWindow xListHWnd(0), 0&
    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_DirList), 1&, 0&
    End If
  'eDropDown(0&).Clear
  Call SendMessage(xListHWnd(0), &H184&, 0&, 0&)
  xDirCount = 0&
  If PLCount = 0& Then
    Call SendMessage(xListHWnd(2), &H194&, 0&, 0&)
    Call SendMessage(xListHWnd(0), &H194&, 0&, 0&)
    If Not LEDMode = 0& And LEDState = &H10& Then
      LEDState = 3&
      End If
    Else
    If Not LockWinUpd = 0& Then
      LockWindowUpdate xListHWnd(0)
      End If
    For xLong1 = 0& To PLCount - 1&
#If BoldDirs = 98 Then
      xLong2 = PLI(xLong1).pxWidthLg + (PLI(xLong1).DirDepthFlags And PLI_Depth) * 20&
      
'Debug.Print "Cap: ", PLI(xLong1).Caption
'Debug.Print "Vals: ", PLI(xLong1).DirDepthFlags And PLI_Depth, PLI(xLong1).pxWidthLg, PLI(xLong1).pxWidthSm
      
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
        If PLI(xLong1).pxWidthSm > xLong6 Then xLong6 = PLI(xLong1).pxWidthSm
#Else
      xLong2 = PLIPxLg(xLong1) + (PLIDF(xLong1) And PLI_Depth) * 20&
      If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
        If PLIPxSm(xLong1) > xLong6 Then xLong6 = PLIPxSm(xLong1)
#End If
        Call SendMessage(xListHWnd(0&), &H180&, 0&, xLong1)
        xDirPath(xDirCount) = xLong1
        xDirCount = xDirCount + 1&
        End If
      If xLong2 > xLong5 Then xLong5 = xLong2
      Next
    xDirPath(xDirCount) = PLCount
    Call SendMessage(xListHWnd(2), &H194&, 28& + lWidth + xLong5, 0&)
    Call SendMessage(xListHWnd(0), &H194&, 28& + lWidth + xLong6, 0&)
    If Not LEDMode = 0& And LEDState = 3& Then
      MainForm_UpdateMainIcon
      End If
    End If
    
  If Not LockWinUpd = 0& Then
    LockWindowUpdate 0&
    End If
    
  If xDirCount < 2& Then
    EnableWindow xListHWnd(0), 0&
    
    MainForm_EnableApiMenu 420&, False
    EnableWindow xListHWnd(0), 0&
    If xDirCount = 0& Then
      'eLabel(8&).Caption = ""
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "ClearDirCap due xBuildDirs"
        #End If
      MainForm_SetDirCaption ""
      End If
    Else
    EnableWindow xListHWnd(0), 1&
'    eButton(0&).Enabled = True
    'mTMenu(8).Enabled = True
    MainForm_EnableApiMenu 420&, True
    EnableWindow xListHWnd(0), 1&
    End If
  
  xWarnOverload = (PLCount > PLCountWarn)
  If (xWarnOverload Xor xOldWarnOverload) Then
    'Debug.Print "#######", xOldWarnOverload,xOldWarnOverload
    Call MainForm_EnableApiMenu(10&, xOldWarnOverload)
    Call MainForm_EnableApiMenu(11&, xOldWarnOverload)
    Call MainForm_EnableApiMenu(12&, xOldWarnOverload)
    #If NoDroppingIfOverloaded Then
      If xWarnOverload Then
        Call RevokeDragDrop(MainHWnd)
        Else
        Call RegisterDragDrop(MainHWnd, ObjPtr(DragClass))
        End If
      #End If
    'If xOldIconState < 10& Then
    '  If AllTears_TypesCount < 3& Then
    '    If xWarnOverload Then
    '      DoAnimateSetIcon 1480&
    '      Else
    '      DoAnimateSetIcon -1&
    '      End If
    '    End If
    '  End If
    xOldWarnOverload = xWarnOverload
    'If xOldWarnOverload Then
      MainForm_UpdateMainIcon
    '  Else
    '  UpdateAnimIcon
    '  End If
    End If
  
  MainForm_CheckDir True
  End Sub

Public Sub MainForm_ReGetWidth()
  Dim xLong1 As Long
  Dim xDC1 As Long
  Dim xDC2 As Long
  Dim xDCD As Long
  Dim xDCB As Long
  Dim xFntD As Long
  Dim xFntB As Long
  Dim xFntS As Long
  
  xDC1 = CreateCompatibleDC(0&)
  If xDC1 = 0& Then
    xDCD = xSharedDC
    xDCB = xSharedDC
    Else
    xDCD = xDC1
    xFntD = SelectObject(xDC1, hFontPLLg)
    xDC2 = CreateCompatibleDC(0&)
    If xDC2 = 0& Then
      xDCB = xDC1
      Else
      xDCB = xDC2
      xFntB = SelectObject(xDC2, hFontPLLgB)
      End If
    End If
  
  xFntS = SelectObject(xSharedDC, hFontPLSm)
  
  If ShowFullPath Then
    For xLong1 = 0& To PLCount - 1&
      #If BoldDirs = 98 Then
        If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
          PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCB, PLI(xLong1).Caption)
          PLI(xLong1).pxWidthSm = GetTextOutWidth(xSharedDC, PLI(xLong1).Filename)
          Else
          PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCD, PLI(xLong1).Caption)
          End If
        #Else
        If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
          PLIPxLg(xLong1) = GetTextOutWidth(xDCB, PLICap(xLong1))
          PLIPxSm(xLong1) = GetTextOutWidth(xSharedDC, PLIFile(xLong1))
          Else
          PLIPxLg(xLong1) = GetTextOutWidth(xDCD, PLICap(xLong1))
          End If
        #End If
      Next
    Else
    For xLong1 = 0& To PLCount - 1&
      #If BoldDirs = 98 Then
        If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
          PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCB, PLI(xLong1).Caption)
          PLI(xLong1).pxWidthSm = GetTextOutWidth(xSharedDC, PLI(xLong1).Caption) + (PLI(xLong1).DirDepthFlags And PLI_Depth) * 20&
          Else
          PLI(xLong1).pxWidthLg = GetTextOutWidth(xDCD, PLI(xLong1).Caption)
          End If
        #Else
        If (PLIDF(xLong1) And PLI_IsDir) = PLI_IsDir Then
          PLIPxLg(xLong1) = GetTextOutWidth(xDCB, PLICap(xLong1))
          PLIPxSm(xLong1) = GetTextOutWidth(xSharedDC, PLICap(xLong1)) + (PLIDF(xLong1) And PLI_Depth) * 20&
          Else
          PLIPxLg(xLong1) = GetTextOutWidth(xDCD, PLICap(xLong1))
          End If
        #End If
      Next
    End If
  
  Call SelectObject(xSharedDC, xFntS)
  If Not xDC1 = 0& Then
    Call SelectObject(xDC1, xFntD)
    Call DeleteDC(xDC1)
    If Not xDC2 = 0& Then
      Call SelectObject(xDC2, xFntB)
      Call DeleteDC(xDC2)
      End If
    End If
  End Sub

Public Sub SearchForm_UpdateFont(ByVal HWndFont As Long, ByVal UpdateFlag As Long)
  Dim xLong1 As Long

  For xLong1 = 0& To Interface_Control_Search_LastFontApply
    Call SendMessage(Interface_Control(xLong1), &H30&, HWndFont, UpdateFlag)
    Next
  '&HD3
  'If Not Interface_Control(Interface_Control_Search_Txt) = 0& Then
  '  Call SendMessage(Interface_Control(Interface_Control_Search_Txt), &HD3&, 3&, &H20002 + ScrollComboWidth * &H10000)
  '  End If
  'Call SendMessage(HWndButton(Interface_Control_Search_Results), &H30&, HWndFont, 0&)
  End Sub

Public Sub SearchForm_Interface()
  Dim xLong1 As Long

  SearchFormDraggingIs = False
  
  Interface_Icon = LoadResEx_Icon(PLIconSearch_ResID, 32&)
  If Not Interface_Icon = 0& Then
    Interface_Control(Interface_Control_Search_Image) = CreateWindowEx(0&, W_STATIC, "", &H56000003, 0&, 4&, 32&, 32&, MainHWnd, 0&, 0&)
    If Interface_Control(Interface_Control_Search_Image) = 0& Then
      Call DestroyIcon(Interface_Icon)
      Interface_Icon = 0&
      Else
      Call SendMessage(Interface_Control(Interface_Control_Search_Image), &H170&, Interface_Icon, 0&)
      End If
    End If
  
  Interface_Control(Interface_Control_Search_CapLabel) = CreateWindowEx(0&, W_STATIC, LoadResEx_String(2300&), &H56000000, 4&, 3&, 417&, 17&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Search_Txt) = CreateWindowEx(&H200&, W_EDIT, "", &H56010090, 6&, 17&, 400&, 14&, MainHWnd, 0&, 0&)
  Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, 0&, 0&, 0&, 0&, &H213&)

  Interface_Control(Interface_Control_Search_Results) = CreateWindowEx(&H200&, W_LISTBOX, "", &H56300918, 0&, 48&, 425&, 160&, MainHWnd, 0&, 0&)

  Interface_Control(Interface_Control_Search_Check1) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2301&), &H56010003, 8&, 232&, 121&, 17&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Search_Check2) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2302&), &H56010003, 8&, 232&, 121&, 17&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Search_Play) = CreateWindowEx(0&, W_BUTTON, "", &H56010000 - CLng(xSearchAutoPlay), 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Search_Select) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2304&), &H56010000 - CLng(Not xSearchAutoPlay), 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Search_Return) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2305&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  
  SetAccel Interface_Control_Search_Txt, 2300&
  For xLong1 = 0& To Interface_Control_Search_Return
    If Not xLong1 = Interface_Control_Search_Play Then SetAccel xLong1, 2301& + xLong1
    Call HookWindow(Interface_Control(xLong1), AddressOf SearchForm_ButtonProc)
    Next
  
  SearchForm_UpdateFont hFontGFix, 1&
  Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H30&, hFontPLLgB, 0&)

  Call HookWindow(Interface_Control(Interface_Control_Search_Txt), AddressOf SearchForm_TextProc)
  Call HookWindow(Interface_Control(Interface_Control_Search_Results), AddressOf SearchForm_ResultListProc)

  SearchForm_CheckBoxTxtChange
  UpdateSearchVals True
  
  'eButton(3).Enabled = Not (xSearchOldQueriesCount = 0&)
  If Len(CPListText) = 0& Then
    MainForm_TrySearch False
    Else
    SendMessage_String Interface_Control(Interface_Control_Search_Txt), &HC&, 0&, CPListText
    SendMessage Interface_Control(Interface_Control_Search_Txt), &HB1, 0&, -1&
    CPListMustDo = False
    InitCPList
    End If
  
  ' bla bla bla
  'If Not xLastSearchExFontUpToDate Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H1A0&, 0&, PLODHeight)
  '  End If
  
  'SetToolTip xStaticIcons_DirList, LoadResEx_String(2309&), ""
  
  If Not Interface_Control(Interface_Control_Search_Txt) = 0& Then SetFocusAPI Interface_Control(Interface_Control_Search_Txt)
  'InterfaceChangePostResize
  End Sub

Private Sub SearchForm_UnInterface()
  CPListMustDo = False
  
  If Not Interface_Icon = 0& Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Image), &H170&, 0&, 0&)
    Call DestroyIcon(Interface_Icon)
    Interface_Icon = 0&
    End If
  'Interface_Control(Interface_Control_Search_Results) = 0&
  End Sub

Private Sub UnInterface()
  Dim xLong1 As Long
  
  Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_DirList), 0&, 0&, 0&, 0&, 19&, &H215&)
  'For xLong1 = 0& To Interface_Control_HookLast
    'If Not Interface_Control_Hook(xLong1) = 0& Then
    '  Call SetWindowLong(Interface_Control(xLong1), -4&, Interface_Control_Hook(xLong1))
    '  Interface_Control_Hook(xLong1) = 0&
      'End If
  '  Next
  'If Not Interface_Icon = 0& Then
  '  Call DestroyIcon(Interface_Icon)
  '  Interface_Icon = 0&
  '  End If
  For xLong1 = 0& To Interface_Control_Last
    If Not Interface_Control(xLong1) = 0& Then
      Call DestroyWindow(Interface_Control(xLong1))
      Interface_Control(xLong1) = 0&
      End If
    Next
  For xLong1 = 0& To Interface_Control_AccelLast
    Interface_Control_Accel(xLong1) = 0&
    Next
  SetForegroundWindow MainHWnd
  SetFocusAPI MainHWnd
  End Sub

Private Function SearchForm_ButtonProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  
  If Message = &H100& Then
    If wParam = 9& Then
      LoadResEx_Window_ManualTab MainHWnd, hWnd
      Exit Function
      ElseIf wParam = 27& Then
      ToggleInterface Interface_Mode_None
      Exit Function
      End If
    ElseIf Message = &H105& Then 'And (lParam And &H40000000) = 0& Then
    Call SearchForm_TryAccel(wParam)
    End If
  For xLong1 = 0& To Interface_Control_Search_LastHook
    If Interface_Control(xLong1) = hWnd Then
      SearchForm_ButtonProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Exit For
      End If
    Next
'lblSkipDef:
  End Function

Private Function SearchForm_TextProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  'Dim xLong1 As Long
  
  Select Case Message
    Case &H100&
      'down
      Select Case wParam
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
          Exit Function
        Case 27&
          ToggleInterface Interface_Mode_None
          Exit Function
        '33=pgup, 34=pgdown, 36=home, 35=end
        Case 13&
          If CPListMustDo Then
            MainForm_TrySearch False
            Else
            Call SearchForm_TrySCommand(Interface_Control(Interface_Control_Search_Select + CLng(xSearchAutoPlay)), 0&)
            End If
          Exit Function
        Case 33&, 34&, 38&, 40& '33=pgup
          If Not (xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo) Then
            Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H100&, wParam, lParam)
            End If
          Exit Function
'        Case 93&
'          'SearchForm_TextProc = 1&
'          exit function
        End Select
'    Case &H102&
'      'press
'      Select Case wParam
'        Case 9&, 13& ', 93&
'          'SearchForm_TextProc = 1&
'          'exit function
'        End Select
    Case &H101&
      'up
      Select Case wParam
        Case 9&, 13&
          Exit Function
        'Case 93&
        '  If IsCtrlPressed Then
        '    ShowWindow Interface_Control(Interface_Control_Search_Drop), 5&
        '    SetFocus Interface_Control(Interface_Control_Search_Drop)
        '    SearchForm_TextProc = 1&
        '    GoTo lblSkipDef
        '    ElseIf Not IsShiftPressed Then
        '    ShowPopupList
        '    SearchForm_TextProc = 1&
        '    GoTo lblSkipDef
        '    End If
        End Select
    Case &H105&
'Debug.Print "F3"
      'If (lParam And &H40000000) = 0& Then
        Call SearchForm_TryAccel(wParam)
      '  End If
    End Select
  SearchForm_TextProc = HookedWindowProc(hWnd, Message, wParam, lParam)
'
'lblSkipDef:
  End Function

Private Function SearchForm_ResultListProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  
  Select Case Message
    Case &H200& 'move
      If SearchFormDraggingIs And Not (lParam = SearchFormDraggingLParam) Then
        If MainForm_TrySearchNow() Then
          Load OLEHelperForm
          OLEHelperForm.OLEDrag
          Unload OLEHelperForm
          App.TaskVisible = False
          End If
        End If
      SearchFormDraggingIs = False
    Case &H201&, &H204 'down
      If DragOut Then
        If DragDetectWinLikeSL(lParam) Then
          SearchFormDraggingIs = True
          SearchFormDraggingWParam = wParam
          SearchFormDraggingLParam = lParam
          SearchFormDraggingWMsgPlus = Message
          Exit Function
          End If
        End If
    Case &H202&, &H205& 'up
      If SearchFormDraggingIs Then
        Call HookedWindowProc(hWnd, SearchFormDraggingWMsgPlus, SearchFormDraggingWParam, SearchFormDraggingLParam)
        SearchFormDraggingIs = False
        End If
      If Message = &H205& Then
        SearchForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
        If Not (PlayerIsLocked Or xAutoDoing Or xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo) Then
          xLong1 = TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), Interface_Control(Interface_Control_Search_Results), ((lParam And &HFFFF0000) \ &H10000), False, 0&, 1&)  'true
          If Not xLong1 = -1& Then
            If MainForm_TrySearchNow Then
              MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(8), 0&)
              End If
            End If
          End If
        Exit Function
        End If
    Case &H100&
      SearchForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Select Case wParam
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
        Case 13&
          Call SearchForm_TrySCommand(Interface_Control(Interface_Control_Search_Select + CLng(xSearchAutoPlay)), 0&)
        Case 27&
          ToggleInterface Interface_Mode_None
        Case 93& 'menukey
          If Not (PlayerIsLocked Or xAutoDoing Or xSearchCount = 0& Or xSearchCount = xSearchCountOver Or CPListMustDo) Then
            'xLong1 = TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), Interface_Control(Interface_Control_Search_Results), ((lParam And &HFFFF0000) \ &H10000), False, 0&, 1&)  'true
'MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(5), xListHWnd(2))
            'If Not xLong1 = -1& Then
              If MainForm_TrySearchNow Then
                MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(8), Interface_Control(Interface_Control_Search_Results))
                End If
            '  End If
            End If
        End Select
      Exit Function
    Case &H105&
'Debug.Print "F5"
      'If (lParam And &H40000000) = 0& Then
        Call SearchForm_TryAccel(wParam)
      '  End If
    Case &H203& ' WM_LBUTTONDBLCLK
      SearchForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Call SearchForm_TrySCommand(Interface_Control(Interface_Control_Search_Select + CLng(xSearchAutoPlay)), 0&)
      Exit Function
#If ListFocusedLists Then
    Case 7&
      xCurFocusHWnd = hWnd
    Case 8&
      xCurFocusHWnd = wParam
#End If
    End Select
    'Case Else
      SearchForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
    'End Select
'NoProc:
  End Function

Private Sub SearchForm_TrySCommand(ByVal hWnd As Long, ByVal wParam As Long)
  Dim xChar1 As String
  
  If Not IsWindowEnabled(hWnd) = 0& Then
    Select Case hWnd
      Case Interface_Control(Interface_Control_Search_Play)
        If MainForm_TrySearchNowEx Then
          If xSearchAutoClose Then ToggleInterface Interface_Mode_None
          'If hwnd = Interface_Control(Interface_Control_Search_play) Then
            MainForm_PlayListDoubleClickEvent
          '  End If
          End If
      Case Interface_Control(Interface_Control_Search_Select)
        If MainForm_TrySearchNow Then
          If xSearchAutoClose Then ToggleInterface Interface_Mode_None
          End If
      Case Interface_Control(Interface_Control_Search_Check1)
        xSearchAutoPlay = Not (SendMessage(hWnd, &HF0&, 0&, 0&) = 0&)
        UpdateSearchVals False
      Case Interface_Control(Interface_Control_Search_Check2)
        xSearchAutoClose = Not (SendMessage(hWnd, &HF0&, 0&, 0&) = 0&)
      Case Interface_Control(Interface_Control_Search_Return)
        ToggleInterface Interface_Mode_None
        'SearchForm_TrySCommand = True
      Case Interface_Control(Interface_Control_Search_Txt)
        If (wParam And &HFFFF0000) = &H3000000 Then
          xChar1 = GetWindowText(hWnd)
          If Not CPListText = xChar1 Then
            CPListText = xChar1
            InitCPList
            End If
          End If
      End Select
    End If
  End Sub

Private Sub SearchForm_Resize(ByVal xRight As Long, ByVal xBottom As Long, ByVal xTop As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long

'  If xRight < 0& Then xRight = 0&
  
  If Interface_Icon = 0& Then
    xLong1 = 4&
    xLong2 = xRight - 8&
    Else
    xLong1 = 44&
    xLong2 = xRight - 44& ' 4 32 4 x 4
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Image), 0&, 4&, 3& + xTop, 32&, 32&, &H215&)
    End If
  If xLong1 < 0& Then xLong1 = 0&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_CapLabel), 0&, xLong1, 3& + xTop, xLong2, 14&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Txt), 0&, xLong1, 18& + xTop, xLong2, 20&, &H214&)
  
  
  'Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Txt), 0&, 0&, 16&, xRight, 19&, &H216&)
  'Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_DirList), 0&, 0&, 20& + xTop, xRight, 19&, &H214&)

  xLong1 = (xRight - eSPTop4) / 3&
  If xLong1 < 0& Then xLong1 = 0&
  
  xBottom = xTop + xBottom - 25& - eSPTop '- 20&
  If xBottom - 16& - eSPTop < 36& + xTop + 16& Then
    xBottom = 36& + xTop + 16& + eSPTop + 16&
    End If
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Play), 0&, eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Select), 0&, xLong1 + 2& * eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Return), 0&, (xLong1 + eSPTop) * 2& + eSPTop, xBottom, xLong1, 25&, &H214&)
  
  xLong1 = (xRight - eSPTop4) / 2&
  If xLong1 < 0& Then xLong1 = 0&
  xBottom = xBottom - 16& - eSPTop '28&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Check1), 0&, eSPTop, xBottom, xLong1, 16&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Check2), 0&, xLong1 + 2& * eSPTop, xBottom, xLong1, 16&, &H214&)
  
  'xBottom -4&

  'xLong1 = xBottom - 28& - 40&
  xLong1 = xBottom - 48& - xTop
  If xLong1 < 0& Then xLong1 = 0&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Search_Results), 0&, 0&, 43& + xTop, xRight, xLong1, &H214&)
  End Sub

Public Sub SearchForm_CheckBoxTxtChange()
  Call SetWindowText(Interface_Control(Interface_Control_Search_SpecialCheck), LoadResEx_String(2310& + PlDClickAction))
  SetAccel Interface_Control_Search_SpecialCheck, 2310& + PlDClickAction
  End Sub

Public Sub UpdateSearchVals(ByVal FullApply As Boolean)
'Public Const WS_DISABLED = &H8000000
  ShowWindow Interface_Control(Interface_Control_Search_Play), 0&
'  Debug.Print "From: ", Hex(GetWindowLong(Interface_Control(Interface_Control_Search_play), -16&))
  Call SetWindowLong(Interface_Control(Interface_Control_Search_Play), -16&, (GetWindowLong(Interface_Control(Interface_Control_Search_Play), -16&) And &HFFFFFFFE) - CLng(xSearchAutoPlay))
'  Debug.Print "To:   ", Hex(GetWindowLong(Interface_Control(Interface_Control_Search_play), -16&)), Hex((GetWindowLong(Interface_Control(Interface_Control_Search_play), -16&) And &HFFFFFFFE) - CLng(xSearchAutoPlay))
  'Call SendMessage(Interface_Control(Interface_Control_Search_play), &H14&, 0&, 0&)
  'Call SendMessage(Interface_Control(Interface_Control_Search_play), &HF&, 0&, 0&)
  'Call SetWindowLong(Interface_Control(Interface_Control_Search_select), -16&, &H56010000 - CLng(Not xSearchAutoPlay))
  'Call SetWindowPos(Interface_Control(Interface_Control_Search_play), 0&, 0&, 0&, 0&, 0&, &H277&)
  ShowWindow Interface_Control(Interface_Control_Search_Play), 1&
  
  ShowWindow Interface_Control(Interface_Control_Search_Select), 0&
  Call SetWindowLong(Interface_Control(Interface_Control_Search_Select), -16&, (GetWindowLong(Interface_Control(Interface_Control_Search_Select), -16&) And &HFFFFFFFE) - CLng(Not xSearchAutoPlay))
  'Call SendMessage(Interface_Control(Interface_Control_Search_select), &H14&, 0&, 0&)
  'Call SendMessage(Interface_Control(Interface_Control_Search_select), &HF&, 0&, 0&)
  'Call SetWindowPos(Interface_Control(Interface_Control_Search_select), 0&, 0&, 0&, 0&, 0&, &H277&)
  ShowWindow Interface_Control(Interface_Control_Search_Select), 1&
  
  
  If FullApply Then
    Call SendMessage(Interface_Control(Interface_Control_Search_Check1), &HF1&, -CLng(xSearchAutoPlay), 0&)
    Call SendMessage(Interface_Control(Interface_Control_Search_Check2), &HF1&, -CLng(xSearchAutoClose), 0&)
    'EnableWindow Interface_Control(Interface_Control_Search_return), 1& + CLng(xSearchAutoClose)
    SearchForm_CheckBoxTxtChange
    End If
'  eOptions(1).Enabled = Not xSearchAutoClose
  End Sub


Public Sub InitCPList()
  'CPListText = etext.Text
  If Not CPListMustDo Then
    xSearchError = LoadResEx_String(2320&)
    CPListMustDo = True
    CPListTime = DoEventsEx_GetTickCount
    Call SendMessage(Interface_Control(Interface_Control_Search_Results), &H184&, 0&, 0&)
    EnableWindow Interface_Control(Interface_Control_Search_Results), 0&
    EnableWindow Interface_Control(Interface_Control_Search_Play), 0&
    EnableWindow Interface_Control(Interface_Control_Search_Select), 0&
    'EnableWindow Interface_Control(Interface_Control_Search_Return), 0&
    #If True Then
      Call SendMessage_String(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, -1&)
      #Else
      Call SendMessage_String(Interface_Control(Interface_Control_Search_Results), &H180&, 0&, xSearchError)
      #End If
    End If
  End Sub

Private Sub SetAccel(ByVal Index As Long, ByVal ResID As Long)
  Dim xChar1 As String
  
  xChar1 = Replace(LoadResEx_String(ResID), "&&", "")
  ResID = InStr(xChar1, "&")
  If ResID = Len(xChar1) Then
    ResID = 0&
    ElseIf Not ResID = 0& Then
    ResID = Asc(UCase(Mid(xChar1, ResID + 1&, 1&)))
    End If
  Interface_Control_Accel(Index) = ResID
  End Sub

Private Sub SearchForm_TryAccel(ByVal wParam As Long)
  Dim xLong1 As Long
  
  'If wParam = 40& Or wParam = 38& Then
  If wParam = 93& Then
    If MainForm_TrySearchNow Then
      MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(8), Interface_Control(Interface_Control_Search_Results))
      End If
    Else
    For xLong1 = 0& To Interface_Control_Search_LastAccel
      If Interface_Control_Accel(xLong1) = wParam Then
        SetFocusAPI Interface_Control(Interface_Control_Search_Txt)
        If Not xLong1 = Interface_Control_Search_Txt Then
          CheckboxAccelerate Interface_Control(xLong1)
          Call SearchForm_TrySCommand(Interface_Control(xLong1), 0&)
          End If
        Exit For
        End If
      Next
    End If
  End Sub

Private Sub ToggleInterface(ByVal NewID As Long)
  If Not NewID = InterfaceMode Then
    Select Case InterfaceMode
      Case Interface_Mode_Search: SearchForm_UnInterface
      Case Interface_Mode_Timer:  TimerListForm_UnInterface
      Case Interface_Mode_Radio:  RadioForm_UnInterface
      Case Else:                  InterfacePlayer 0&
      End Select
    UnInterface
    InterfaceMode = NewID
    Select Case InterfaceMode
      Case Interface_Mode_Search: SearchForm_Interface
      Case Interface_Mode_Timer:  TimerListForm_Interface
      Case Interface_Mode_Radio:  RadioForm_Interface
      Case Else:                  InterfacePlayer 5&
      End Select
    DrawMenuBar MainHWnd
    MainForm_ResizePlayer False
    BestFocus
    End If
  End Sub

Private Sub InterfacePlayer(ByVal ShowMode As Long)
  'Dim xLong1 As Long
  '
  If Not HWndSplitter = 0& Then Call ShowWindow(HWndSplitter, ShowMode)
  If Not HWndSplitterMove = 0& Then Call ShowWindow(HWndSplitterMove, ShowMode)
  If Not xListHWnd(2) = 0& Then Call ShowWindow(xListHWnd(2), ShowMode)
  If Not IsWindowVisible(xListHWnd(1)) = 0& Then
    Call ShowWindow(xListHWnd(0), 0&)
    DrawWhiteDCButton xStaticIconsHWnd(xStaticIcons_CurSong), 0&, 0&
    End If
'    If Not xListHWnd(0) = 0& Then Call ShowWindow(xListHWnd(0), 0&)
  
  If ShowMode = 0& And CurShownToolTip = xStaticIcons_DirList Then
    CurShownToolTip = -1&
    If ToolTipInitTime = -1& Then ShowWindow xStaticIconsHWnd(xStaticIcons_ToolTip), 0&
    End If
'  If Not ShowMode = 0& Then
'    SetToolTip xStaticIcons_DirList, LoadResEx_String(1105&), _
                                   Replace(Replace(Replace(Replace(LoadResEx_String(1106&) _
                                   , "$S", ExtKbdEx(KbdC_PGotoDir)) _
                                   , "$T", ExtKbdEx(KbdC_ODirToggle)) _
                                   , "$I", ExtKbdEx(KbdC_ODirInc)) _
                                   , "$D", ExtKbdEx(KbdC_ODirDec))
'    End If
  
  If xDirList <= 0& Then
    ShowWindow xStaticIconsHWnd(xStaticIcons_DirList), ShowMode
    Else
    ShowWindow xStaticIconsHWnd(xStaticIcons_DirList), 0&
    End If
  If ShowMode = 0& Then 'And xDirList > 0& Then
    ShowWindow xListHWnd(0), 0&
    ElseIf xDirList > 0& Then
    ShowWindow xListHWnd(0), 5&
    End If
  If ShowMode = 0& Or xMenu(0) = 0& Then
    SetMenu MainHWnd, 0&
    Else
    SetMenu MainHWnd, xMenu(0)
    End If
  End Sub

Private Sub BestFocus()
  Select Case InterfaceMode
    Case Interface_Mode_Search: SetFocusAPI Interface_Control(Interface_Control_Search_Txt)
    Case Interface_Mode_Timer:  SetFocusAPI Interface_Control(Interface_Control_Timer_List)
    Case Interface_Mode_Radio:  SetFocusAPI Interface_Control(Interface_Control_Radio_Txt)
    Case Else:                  SetFocusAPI xListHWnd(2)
    End Select
  End Sub

Private Sub xFindNextItemExPL(ByVal xStep As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  If Not PLCount = 0& Then
    Call GetListIndexMS(xListHWnd(2), xLong1)
    If xLong1 = -1& Then xLong1 = 0&

    xLong2 = xLong1
    Do
      xLong1 = (xLong1 + xStep + PLCount) Mod PLCount
#If BoldDirs = 98 Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = 0& Or xLong1 = xLong2 Then
#Else
      If (PLIDF(xLong1) And PLI_IsDir) = 0& Or xLong1 = xLong2 Then
#End If
        Exit Do 'Function
        End If
      Loop
    xMarkSong xLong1, False, False
    xFollowCurSongLast = DoEventsEx_GetTickCount
    xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
    End If
  End Sub








Public Sub TimerListForm_Interface()
  Dim xLong1 As Long

  TimerListForm_xMenu = LoadResEx_Menu(2600&)
  #If MainMenuCloseButton Then
    MainMenuCloseButton_Add TimerListForm_xMenu, 2609&
    #End If
  TimerListForm_xMenu2 = LoadResEx_Menu(2610&)
  If Not TimerListForm_xMenu = 0& Then
    SetMenu MainHWnd, TimerListForm_xMenu
    DrawMenuBar MainHWnd
    End If
    
  Interface_Icon = LoadResEx_Icon(PLIconTimer_ResID, 32&)
  If Not Interface_Icon = 0& Then
    Interface_Control(Interface_Control_Timer_Image) = CreateWindowEx(0&, W_STATIC, "", &H56000003, 0&, 4&, 32&, 32&, MainHWnd, 0&, 0&)
    If Interface_Control(Interface_Control_Timer_Image) = 0& Then
      Call DestroyIcon(Interface_Icon)
      Interface_Icon = 0&
      Else
      Call SendMessage(Interface_Control(Interface_Control_Timer_Image), &H170&, Interface_Icon, 0&)
      End If
    End If

  TimerListUpdateHWnd = CreateWindowEx(0&, W_STATIC, Replace(LoadResEx_String(2600&), "$", GetTimeFormatNow), &H56000000, 4&, 3&, 417&, 17&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Timer_CapLabel) = TimerListUpdateHWnd


  TimerListForm_xPopupMenu2 = LoadResEx_Menu_MakePopupMenu(TimerListForm_xMenu2)
  '#If DontHookForTesting = 0 Then
  '  xOldProcTM = SetWindowLongAnsi(Me.hwnd, -4&, AddressOf MenuHookTM)
  '  #End If
  If xSecureMode Then Call EnableMenuItem(TimerListForm_xMenu, 2601&, 3&)
  If xSecureMode Then Call EnableMenuItem(TimerListForm_xMenu2, 2601&, 3&)
    
  Call SetWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, 0&, 0&, 0&, 0&, &H213&)
    
  Interface_Control(Interface_Control_Timer_List) = CreateWindowEx(&H200&, W_LISTBOX, "", &H56311988, 0&, 0&, 409&, 220&, MainHWnd, 0&, 0&)
  
  For xLong1 = 0& To Interface_Control_Timer_Close
    Interface_Control(xLong1) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2605& + xLong1), &H56010000, 8& + 80& * xLong1, 224&, 80&, 25&, MainHWnd, 0&, 0&)
    If Not Interface_Control(xLong1) = 0& Then
      SetAccel xLong1, 2605& + xLong1
      Call HookWindow(Interface_Control(xLong1), AddressOf TimerListForm_ButtonProc)
      End If
    Next
  SetAccel Interface_Control_Timer_List, 2600&


  TimerListForm_UpdateTimerListWinFont hFontGFix
  
  If Not Interface_Control(Interface_Control_Timer_List) = 0& Then
    Call HookWindow(Interface_Control(Interface_Control_Timer_List), AddressOf TimerListForm_ListProc)
    End If
  
  
  TimerListForm_ListUpdate
  
  If Not Interface_Control(Interface_Control_Timer_List) = 0& Then SetFocusAPI Interface_Control(Interface_Control_Timer_List)
  End Sub


Public Sub TimerListForm_UnInterface()
  MainForm_SetTimerImage
  '#If DontHookForTesting = 0 Then
  '  Call SetWindowLongAnsi(Me.hwnd, -4&, xOldProcTM)
  '  #End If
  EnableWindow MainHWnd, 1&
  
  
  If Not Interface_Icon = 0& Then
    Call SendMessage(Interface_Control(Interface_Control_Timer_Image), &H170&, 0&, 0&)
    Call DestroyIcon(Interface_Icon)
    Interface_Icon = 0&
    End If
  
  'Call LoadResEx_WindowIcon(TimerListMainHWnd, -1&)
  'DestroyWindow TimerListMainHWnd
  'If TimerListForm_xMenu Then DestroyMenu TimerListForm_xMenu
  If TimerListForm_xMenu2 Then DestroyMenu TimerListForm_xMenu2
  If TimerListForm_xPopupMenu2 Then DestroyMenu TimerListForm_xPopupMenu2
  'TimerListMainHWnd = 0&
  TimerListUpdateHWnd = 0&
  TimerListForm_xLocked = False
  SetForegroundWindow MainHWnd
  SetFocusAPI MainHWnd
  End Sub







Private Function TimerListForm_ListProc(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&
          Call TimerListForm_TrySCommandEx(MainHWnd, 2&)
        Case 93&
          If (lParam And &H40000000) = 0& Then
            'TimerListForm_MenuEvent TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), hWnd, , True, TimerListForm_xMenu2, 1&)
            TimerListForm_MenuEvent GetAPIPopupMenu(MainHWnd, TimerListForm_xMenu2, Interface_Control(Interface_Control_Timer_List))
            End If
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
          Exit Function
        Case 27&
          ToggleInterface Interface_Mode_None
          Exit Function
        End Select
      'Exit Function
    Case &H105& 'syskeyup
      'If (lParam And &H40000000) = 0& Then
        Call TimerListForm_TryAccel(wParam)
      '  End If
    Case &H205& 'rightmouseup
      If Not TimerListForm_xLocked Then
        TimerListForm_MenuEvent TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), hWnd, ((lParam And &HFFFF0000) \ &H10000), True, TimerListForm_xMenu2, 1&)
        End If
    Case &H203 ' l-dblclick
      If Not xTimerEvCount = 0& Then
        Call TimerListForm_TrySCommandEx(MainHWnd, 1&)
        'eButton_Click 1&
        End If
    Case Else
      TimerListForm_ListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
    End Select
  End Function


Private Sub TimerListForm_LoadTimerDlg()
  Dim xChar1 As String
  
  xChar1 = CalaymLoadSingleFile(LoadResEx_String(1605&) + vbNullChar + "*.cpt", LoadResEx_String(1600&), TimerDir, MainHWnd, False)
  If Not xChar1 = "" Then
    MainForm_LoadTimers MainHWnd, xChar1, False
    End If
  End Sub

Public Sub TimerListForm_Resize(ByVal xRight As Long, ByVal xBottom As Long, ByVal xTop As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Const eSpX As Long = 4&
  Const eSpX_6 As Long = eSpX * 6&
  
  If Interface_Icon = 0& Then
    xLong1 = 4&
    xLong2 = xRight - 8&
    Else
    xLong1 = 44&
    xLong2 = xRight - 44& ' 4 32 4 x 4
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Timer_Image), 0&, 4&, 3& + xTop, 32&, 32&, &H215&)
    End If
  If xLong1 < 0& Then xLong1 = 0&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Timer_CapLabel), 0&, xLong1, 3& + xTop, xLong2, 32&, &H214&)
  'Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_DirList), 0&, 0&, 20& + xTop, xRight, 19&, &H214&)


  xLong1 = (xRight - eSpX_6) / 5&
  If xLong1 < 0& Then xLong1 = 0&
  
  xBottom = xBottom - 25& - eSpX
  For xLong2 = 0& To 4&
    Call TrDeferWindowPos(Interface_Control(xLong2), 0&, (xLong1 + eSpX) * xLong2 + eSpX, xBottom + xTop, xLong1, 25&, &H214&)
    Next
  
  xBottom = xBottom - eSpX - 44&
  If xBottom < 0& Then xBottom = 0&
  
  'Debug.Print Interface_Control(Interface_Control_Timer_List), 0&, xTop, xRight, xBottom
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Timer_List), 0&, 0&, 40& + xTop, xRight, xBottom, &H214&)
  End Sub

Private Sub TimerListForm_ButtonEvent(ByVal hWnd As Long) ', ByVal wParam As Long)
  ' -> TimerListForm_TrySCommandEx
  Dim xLong1 As Long
  
  For xLong1 = 0& To Interface_Control_Timer_Close
    If Interface_Control(xLong1) = hWnd Then
      TimerListForm_TrySCommandEx MainHWnd, xLong1
      Exit For
      End If
    Next
  End Sub

Public Sub TimerListForm_TrySCommandEx(ByVal hWndOwner As Long, ByVal Index As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xLong4 As Long
  
  If Not TimerListForm_xLocked Then
    If Not IsWindowEnabled(Interface_Control(Index)) = 0& Then
      Select Case Index
        Case 0&
          xTimerEvMode = -1&
          ShowTimerEditor
          TimerListForm_DoneTimerEditing
        Case 1&
          If GetListIndexMS(Interface_Control(Interface_Control_Timer_List), xTimerEvMode) Then
          'If eList.SelCount = 1 Then
            'xTimerEvMode = eList.ListIndex
            'If Not xTimerEvMode = -1& Or xTimerEvMode = xTimerEvIR Then
            If Not xTimerEvMode = xTimerEvIR Then
              'TimerListForm_xLocked = true
              'TimerListForm_ApplyLockState
              ShowTimerEditor
              TimerListForm_DoneTimerEditing
              End If
            End If
        Case 2&
          xTimerEditing = True
          xLong1 = xTimerEvCount - 1&
          xTimerEvCount = 0&
          Call SelectObject(xSharedDC, hFontGSys)
          For xLong1 = 0& To xLong1
            If SendMessage(Interface_Control(Interface_Control_Timer_List), &H187&, xTimerEvCount, 0&) = 0& Then
              If xLong1 = xTimerEvIR Then xTimerEvIR = xTimerEvCount
              xTimerEvTime(xTimerEvCount) = xTimerEvTime(xLong1)
              xTimerEv(xTimerEvCount) = xTimerEv(xLong1)
              
              xChar1 = xTimerDisplay(xLong1, 0&, xChar2)
              xLong4 = GetTextOutWidth(xSharedDC, xChar1)
              If xLong4 > xLong2 Then xLong2 = xLong4
              If Not xChar2 = "" Then
                xLong4 = GetTextOutWidth(xSharedDC, xChar2)
                If xLong4 > xLong3 Then xLong3 = xLong4
                End If
              xTimerEvCount = xTimerEvCount + 1&
              Else
              If xLong1 = xTimerEvIR Then xTimerEvIR = -1&
              'eList.RemoveItem xTimerEvCount
              Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H182&, xTimerEvCount, 0&)
              End If
            Next
          
          'If xTimerEvCount = 0& Then
          '  Call EnableMenuItem(TimerListForm_xMenu, 2602&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2602&, 3&)
          '  eButton(1).Enabled = False
          '  eButton(2).Enabled = False
          '  eButton(3).Enabled = False
          '  Call EnableMenuItem(TimerListForm_xMenu, 2612&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2612&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu, 2613&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2613&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu, 2614&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2614&, 3&)
          '  End If
          'xTimerEditing = (xTimerEvCount < xTimerEvMax Or xTimerEvCount = xTimerEvMax And Not xTimerEvIR = -1&)
          'eButton(0).Enabled = xTimerEditing 'xBool1
          'If xTimerEditing Then
          '  Call EnableMenuItem(TimerListForm_xMenu, 2610&, 0&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2610&, 0&)
          '  Else
          '  Call EnableMenuItem(TimerListForm_xMenu, 2610&, 3&)
          '  Call EnableMenuItem(TimerListForm_xMenu2, 2610&, 3&)
          '  End If
          TimerListForm_DoneTimerEditing
          TimerListForm_UpdateTimerListWidths xLong2, xLong3
          MainForm_SetTimerImage
          xTimerEditing = False
        Case 3&
          If Not xTimerEvIR = -1& Then
            If CalaymMessageBox(hWndOwner, LoadResEx_String(1619&), LoadResEx_String(1618&), 68&) = vbNo Then
              xTimerEvIR = -1&
              End If
            End If
          Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H184&, 0&, 0&)
          If xTimerEvIR = -1& Then
            xTimerEvCount = 0&
            'TimerListForm_DoneTimerEditing
            'Call EnableMenuItem(TimerListForm_xMenu, 2602&, 3&)
            'eButton(0).Enabled = True
            'eButton(1).Enabled = False
            'eButton(2).Enabled = False
            'eButton(3).Enabled = False
            'Call EnableMenuItem(TimerListForm_xMenu, 2611&, 0&)
            'Call EnableMenuItem(TimerListForm_xMenu2, 2611&, 0&)
            'Call EnableMenuItem(TimerListForm_xMenu, 2612&, 3&)
            'Call EnableMenuItem(TimerListForm_xMenu2, 2612&, 3&)
            'Call EnableMenuItem(TimerListForm_xMenu, 2613&, 3&)
            'Call EnableMenuItem(TimerListForm_xMenu2, 2613&, 3&)
            'Call EnableMenuItem(TimerListForm_xMenu, 2614&, 3&)
            'Call EnableMenuItem(TimerListForm_xMenu2, 2614&, 3&)
            Else
            xTimerEv(0&) = xTimerEv(xTimerEvIR)
            xTimerEvTime(0&) = xTimerEvTime(xTimerEvIR)
            xChar1 = xTimerDisplay(0&, 1&)
            Call SendMessage_String(Interface_Control(Interface_Control_Timer_List), &H180&, 0&, xChar1)
            'eList.AddItem xChar1
            xChar1 = xTimerDisplay(0&, 0&, xChar2)
            Call SelectObject(xSharedDC, hFontGFix)
            xLong1 = GetTextOutWidth(xSharedDC, xChar1)
            If xChar2 = "" Then
              xLong1 = 0&
              Else
              xLong1 = GetTextOutWidth(xSharedDC, xChar2)
              End If
            xTimerEvCount = 1&
            xTimerEvIR = 0&
            End If
          TimerListForm_DoneTimerEditing
          TimerListForm_UpdateTimerListWidths xLong1, xLong1
          MainForm_SetTimerImage
        Case 4&
          ToggleInterface Interface_Mode_None
        End Select
      End If
    End If
  End Sub

Private Function TimerListForm_ButtonProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  
  If Message = &H100& Then
    If wParam = 9& Then
      LoadResEx_Window_ManualTab MainHWnd, hWnd
      Exit Function
      ElseIf wParam = 27& Then
      ToggleInterface Interface_Mode_None
      Exit Function
      End If
    ElseIf Message = &H105& Then 'And (lParam And &H40000000) = 0& Then
    Call TimerListForm_TryAccel(wParam)
    End If
  
  For xLong1 = 0& To Interface_Control_Timer_LastHook
    If Interface_Control(xLong1) = hWnd Then
      TimerListForm_ButtonProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Exit For
      End If
    Next
'lblSkipDef:
  End Function







Private Sub TimerListForm_TryAccel(ByVal wParam As Long)
  Dim xLong1 As Long
  
  If wParam = 40& Or wParam = 38& Then
    ShowWindow Interface_Control(Interface_Control_Timer_List), 5&
    SetFocusAPI Interface_Control(Interface_Control_Timer_List)
    ElseIf wParam = 93& And Not TimerListForm_xLocked Then
    'TimerListForm_MenuEvent GetAPIPopupMenu(MainHWnd, TimerListForm_xMenu2, Interface_Control(Interface_Control_Timer_List), 11&)
    xLong1 = GetAPIPopupMenu(MainHWnd, TimerListForm_xMenu2, Interface_Control(Interface_Control_Timer_List))
    If Not xLong1 = 0& Then
      TimerListForm_ListProc Interface_Control(Interface_Control_Timer_List), &H111&, 0&, xLong1
      'TimerListForm_TryAccel = (Not (InterfaceMode = Interface_Mode_Timer))
      End If
    Else
    For xLong1 = 0& To Interface_Control_Timer_Close
      If Interface_Control_Accel(xLong1) = wParam Then
        'SetFocusapi Interface_Control(xLong1)
        SetFocusAPI Interface_Control(Interface_Control_Timer_List)
        If Not xLong1 = Interface_Control_Timer_List Then
          'checkboxaccelerate Interface_Control(xLong1)
          Call TimerListForm_TrySCommandEx(MainHWnd, xLong1)
          End If
        'If Not xLong1 = 5& Then
        '  SetFocusapi Interface_Control(Interface_Control_Timer_list)
        '  End If
        Exit For
        End If
      Next
    End If
  End Sub


Public Sub TimerListForm_UpdateTimerListWinFont(ByVal xFont As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xChar1 As String

  For xLong1 = 0& To Interface_Control_Timer_LastFont
    Call SendMessage(Interface_Control(xLong1), &H30&, xFont, 1&)
    Next
  xTimerDisplayExtraLen = 0&
  Call SelectObject(xSharedDC, xFont)
  For xLong1 = 0& To 9&
    xChar1 = String(2&, Chr(48& + xLong1)) + ":" + String(2&, Chr(48& + xLong1))
    xLong2 = GetTextOutWidth(xSharedDC, xChar1) + xTimerDisplaySpace1
    If xLong2 > xTimerDisplayExtraLen Then xTimerDisplayExtraLen = xLong2
    
    xChar1 = Replace(LoadResEx_String(2655&), "$", xChar1)
    xLong2 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong2 > xLong3 Then xLong3 = xLong2
    Next
  xTimerDisplayExtraLenT = xTimerDisplayExtraLen + xLong3 + xTimerDisplaySpace2
  'Debug.Print "WidthInitial:", xTimerDisplayExtraLen, xLong3, xTimerDisplayExtraLenT
  End Sub

Private Sub TimerForm_PrePreset()
  If InterfaceMode = Interface_Mode_Timer Then
    'Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_RESETCONTENT, 0&, 0&)
    If Not xTimerEditingHWnd = 0& Then Call EndDialog(xTimerEditingHWnd, 1&)
    End If
  End Sub

Private Sub TimerListForm_UpdateTimerListWidths(ByVal xLen As Long, ByVal xLenPL As Long)
  Static xLongA1(2) As Long

  'Debug.Print "WidthsFilled:", xLen, xLenPL

  If xLen = 0& Then
    'xLongA1(0) = 0&
    Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H192&, 0&, 0&)  'xLongA1(0))
    Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H194&, 0&, 0&)
    Else
    xLongA1(0) = xTimerDisplayExtraLen * xGlobalDialogUnitsGFix '* 4
    xLongA1(1) = (xTimerDisplayExtraLen + xLen + xTimerDisplaySpace2) * xGlobalDialogUnitsGFix '* 4
    If xLenPL = 0& Then
      Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H192&, 2&, ByVal VarPtr(xLongA1(0)))
      Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H194&, xLen + 4& + xTimerDisplayExtraLenT, 0&)
      Else
      xLongA1(2) = (xTimerDisplayExtraLenT + xLen + xTimerDisplaySpace2) * xGlobalDialogUnitsGFix  '* 4
      Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H192&, 3&, ByVal VarPtr(xLongA1(0)))
      Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H194&, xLen + 4& + xTimerDisplayExtraLenT + xLenPL + xTimerDisplaySpace2, 0&)
      End If
    End If
  End Sub

Public Sub TimerListForm_ListUpdate()
  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 xChar2 As String
  'Dim xType1 As POINTAPI
  
  If xTimerEvCount = 0& Then
    xLong2 = -1&
    Else
    Call GetListIndexMS(Interface_Control(Interface_Control_Timer_List), xLong2)
    End If
  
  ResortTimers
  
  Call SendMessage(Interface_Control(Interface_Control_Timer_List), &H184&, 0&, 0&)
  #If TimeTestDebug Then
    MainForm_ctConsoleA_AddLog "TimeDebug: Refreshing list"
    #End If
  Call SelectObject(xSharedDC, hFontGFix)
  For xLong1 = 0& To xTimerEvCount - 1&
    xChar1 = xTimerDisplay(xLong1, 1&)
    Call SendMessage_String(Interface_Control(Interface_Control_Timer_List), &H180&, 0&, xChar1)
    xChar1 = xTimerDisplay(xLong1, 0&, xChar2)
    xLong5 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong5 > xLong3 Then xLong3 = xLong5
    If Not xChar2 = "" Then
      xLong5 = GetTextOutWidth(xSharedDC, xChar2)
      If xLong5 > xLong4 Then xLong4 = xLong5
      End If
    'If xLong3 > xLong4 Then xLong4 = xLong3
    #If TimeTestDebug Then
      MainForm_ctConsoleA_AddLog "           " & "(" & CStr(xTimerEvTime(xLong1)) & "): " & xTimerDisplay(xLong1, 2&)
      #End If
    Next
  
  If Not xTimerEvCount = 0& Then
    If xLong2 = -1& Then xLong2 = 0&
    SetListIndexMS Interface_Control(Interface_Control_Timer_List), xLong2
    End If
  
  TimerListForm_DoneTimerEditing
  GrabError
  xTimerEvUpdateTimer = Timer \ 60&
  TimerListForm_UpdateTimerListWidths xLong3, xLong4
  MainForm_SetTimerImage
  End Sub

Private Sub TimerListForm_DoneTimerEditing()
  Dim xBool1 As Boolean
  
  xBool1 = ((xTimerEvCount < xTimerEvMax Or xTimerEvCount = xTimerEvMax And Not xTimerEvIR = -1&)) And Not (PlayerIsLocked Or TimerListForm_xLocked)
  EnableWindow Interface_Control(Interface_Control_Timer_Add), -CLng(xBool1)
  Call EnableMenuItem(TimerListForm_xMenu, 2600&, 3& * (1& + CLng(xBool1)))
  Call EnableMenuItem(TimerListForm_xMenu2, 2600&, 3& * (1& + CLng(xBool1)))
  xBool1 = Not (xTimerEvCount = 0& Or PlayerIsLocked Or TimerListForm_xLocked)
  EnableWindow Interface_Control(Interface_Control_Timer_Edit), -CLng(xBool1)
  EnableWindow Interface_Control(Interface_Control_Timer_Remove), -CLng(xBool1)
  EnableWindow Interface_Control(Interface_Control_Timer_RemoveAll), -CLng(xBool1)
  If xBool1 Then
    Call EnableMenuItem(TimerListForm_xMenu, 2601&, 0&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2012&, 0&)
    Call EnableMenuItem(TimerListForm_xMenu, 2602&, 0&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2602&, 0&)
    Call EnableMenuItem(TimerListForm_xMenu, 2603&, 0&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2603&, 0&)
    If Not xSecureMode Then Call EnableMenuItem(TimerListForm_xMenu, 2609&, 0&)
    If Not xSecureMode Then Call EnableMenuItem(TimerListForm_xMenu2, 2609&, 0&)
    Else
    Call EnableMenuItem(TimerListForm_xMenu, 2601&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2601&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu, 2602&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2602&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu, 2603&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2603&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu, 2609&, 3&)
    Call EnableMenuItem(TimerListForm_xMenu2, 2609&, 3&)
    End If
  End Sub

Public Sub TimerListForm_ApplyLockState()
  Dim xLong1 As Long
  Dim xLong2 As Long

  'If InterfaceMode = interface_mode_timer Then
    If PlayerIsLocked Or TimerListForm_xLocked Then xLong2 = 3&
    xLong2 = xLong2 + &H400&
    For xLong1 = 0& To GetMenuItemCount(TimerListForm_xMenu) - 1&
      EnableMenuItem TimerListForm_xMenu, xLong1, xLong2
      Next
    DrawMenuBar MainHWnd
    If PlayerIsLocked Or TimerListForm_xLocked Then
      For xLong1 = 0& To Interface_Control_Timer_RemoveAll 'Interface_Control_Timer_List
        If Not xLong1 = Interface_Control_Timer_Close Then Call EnableWindow(Interface_Control(xLong1), 0&)
        Next
      Else
      Call EnableWindow(Interface_Control(Interface_Control_Timer_Add), -CLng((xTimerEvCount < xTimerEvMax Or xTimerEvCount = xTimerEvMax And Not xTimerEvIR = -1&)))
      For xLong1 = Interface_Control_Timer_Edit To Interface_Control_Timer_RemoveAll
        Call EnableWindow(Interface_Control(xLong1), 1& + CLng(xTimerEvCount = 0&))
        Next
      'Call EnableWindow(Interface_Control(Interface_Control_Timer_Close), 1&)
      'Call EnableWindow(Interface_Control(Interface_Control_Timer_List), 1&)
      End If
    'End If
  End Sub

Public Sub TimerListForm_ReDetectWidth()
  Dim xLong1 As Long
  Dim xLong3 As Long
  Dim xLong4 As Long
  Dim xLong5 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  
  Call SelectObject(xSharedDC, hFontGFix)
  For xLong1 = 0& To xTimerEvCount - 1&
    xChar1 = xTimerDisplay(xLong1, 0&, xChar2)
    xLong5 = GetTextOutWidth(xSharedDC, xChar1)
    If xLong5 > xLong3 Then xLong3 = xLong5
    If Not xChar2 = "" Then
      xLong5 = GetTextOutWidth(xSharedDC, xChar2)
      If xLong5 > xLong4 Then xLong4 = xLong5
      End If
    Next
  
  TimerListForm_UpdateTimerListWidths xLong3, xLong4
  End Sub

Private Sub TimerListForm_MenuEvent(ByVal wParam As Long)
  Select Case wParam
    Case 2608&
      TimerListForm_LoadTimerDlg
    Case 2609&
      MainForm_SaveTimers MainHWnd, "", False
    Case 2600& To 2604&
      TimerListForm_TrySCommandEx MainHWnd, wParam - 2600&
    Case 2699&
      ToggleInterface Interface_Mode_None
    End Select
  End Sub

Private Sub RadioForm_UpdateFont(ByVal HWndFont As Long, ByVal UpdateFlag As Long)
  Dim xLong1 As Long

  For xLong1 = 0& To Interface_Control_Radio_LastFontApply
    Call SendMessage(Interface_Control(xLong1), &H30&, HWndFont, UpdateFlag)
    Next
  RadioForm_DetectListWidth
  End Sub

Private Sub RadioForm_Interface()
  Dim xLong1 As Long

  If CanIRadio Then
    Interface_Icon = LoadResEx_Icon(PLIconRadio_ResID1, 32&)
    Else
    Interface_Icon = LoadResEx_Icon(PLIconRadio_ResID2, 32&)
    End If
  If Not Interface_Icon = 0& Then
    Interface_Control(Interface_Control_Radio_Image) = CreateWindowEx(0&, W_STATIC, "", &H56000003, 0&, 4&, 32&, 32&, MainHWnd, 0&, 0&)
    If Interface_Control(Interface_Control_Radio_Image) = 0& Then
      Call DestroyIcon(Interface_Icon)
      Interface_Icon = 0&
      Else
      Call SendMessage(Interface_Control(Interface_Control_Radio_Image), &H170&, Interface_Icon, 0&)
      End If
    End If
  
  If CanIRadio Then
    Interface_Control(Interface_Control_Radio_CapLabel) = CreateWindowEx(0&, W_STATIC, LoadResEx_String(2800&), &H56000000, 4&, 3&, 417&, 17&, MainHWnd, 0&, 0&)
    Interface_Control(Interface_Control_Radio_Txt) = CreateWindowEx(&H200&, W_EDIT, "", &H56010090, 6&, 17&, 400&, 14&, MainHWnd, 0&, 0&)
    Interface_Control(Interface_Control_Radio_Go) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2801&), &H56010000, 400&, 17&, 14&, 14&, MainHWnd, 0&, 0&)
    Else
    Interface_Control(Interface_Control_Radio_CapLabel) = CreateWindowEx(0&, W_STATIC, LoadResEx_String(2820&), &H56000000, 4&, 3&, 417&, 31&, MainHWnd, 0&, 0&)
    End If
  
  Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_Anim), 0&, 0&, 0&, 0&, 0&, &H213&)
  Interface_Control(Interface_Control_Radio_List) = CreateWindowEx(&H200&, W_LISTBOX, "", &H56300918, 0&, 48&, 425&, 160&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_Play) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2802&), &H56010001, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_OpenHP) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2803&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_AutoCloseCheck) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2804&), &H56010003, 8&, 232&, 121&, 17&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_Add) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2805&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_Edit) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2806&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_Remove) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2807&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  Interface_Control(Interface_Control_Radio_Return) = CreateWindowEx(0&, W_BUTTON, LoadResEx_String(2808&), &H56010000, 8&, 208&, 129&, 25&, MainHWnd, 0&, 0&)
  
  'SetAccel Interface_Control_Radio_List, 2800&
  If CanIRadio Then SetAccel Interface_Control_Radio_Txt, 2800&
  For xLong1 = 0& To Interface_Control_Radio_Return
    SetAccel xLong1, 2801& + xLong1
    Call HookWindow(Interface_Control(xLong1), AddressOf RadioForm_ButtonProc)
    Next
  
  RadioForm_UpdateFont hFontGFix, 1&
  Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H30&, hFontPLLg, 0&)
  Call HookWindow(Interface_Control(Interface_Control_Radio_Txt), AddressOf RadioForm_TextProc)
  Call HookWindow(Interface_Control(Interface_Control_Radio_List), AddressOf RadioForm_ResultListProc)

  RadioForm_UpdateRadioVals
  
  'If Not UrlsAllowed Then
  '  Call EnableWindow(Interface_Control(Interface_Control_Radio_OpenHP), 0&)
  '  End If
  
  If CanIRadio And Not Len(xRadioLastEnteredURL) = 0& Then
    SendMessage_String Interface_Control(Interface_Control_Radio_Txt), &HC&, 0&, xRadioLastEnteredURL
    SendMessage Interface_Control(Interface_Control_Radio_Txt), &HB1, 0&, -1&
    End If
  
  ' bla bla bla
  'If Not xLastRadioExFontUpToDate Then
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H1A0&, 0&, PLODHeight)
  '  End If
  
  'SetToolTip xStaticIcons_DirList, LoadResEx_String(2809&), ""
  
'  If Interface_Control(Interface_Control_Radio_List) = 0& Or xRadioCount = 0& Or Not (Interface_Control(Interface_Control_Radio_List) = 0& Or Len(Len(xRadioLastEnteredURL) = 0&)) Then
'    SetFocusAPI Interface_Control(Interface_Control_Radio_Txt)
'    ElseIf Not Interface_Control(Interface_Control_Radio_List) = 0& Then
'    SetFocusAPI Interface_Control(Interface_Control_Radio_List)
'    End If
  If Len(xRadioLastEnteredURL) = 0& And Not (xRadioCount = 0& Or Interface_Control(Interface_Control_Radio_List) = 0&) = 0& Then
    SetFocusAPI Interface_Control(Interface_Control_Radio_List)
    ElseIf Not Interface_Control(Interface_Control_Radio_Txt) = 0& Then
    SetFocusAPI Interface_Control(Interface_Control_Radio_Txt)
    End If
  
  xRadioMenu = LoadResEx_Menu(2800&)
  xRadioMenu2 = LoadResEx_Menu(2810&)
  xRadioPopupMenu2 = LoadResEx_Menu_MakePopupMenu(xRadioMenu2)
  If Not xRadioMenu = 0& Then
    SetMenu MainHWnd, xRadioMenu
    DrawMenuBar MainHWnd
    End If
  
  RadioForm_ReListRadios True
  'InterfaceChangePostResize
  End Sub

Private Sub RadioForm_UnInterface()
  If Not Interface_Icon = 0& Then
    Call SendMessage(Interface_Control(Interface_Control_Radio_Image), &H170&, 0&, 0&)
    Call DestroyIcon(Interface_Icon)
    Interface_Icon = 0&
    End If
  If xRadioMenu Then DestroyMenu xRadioMenu
  If xRadioMenu2 Then DestroyMenu xRadioMenu2
  If xRadioPopupMenu2 Then DestroyMenu xRadioPopupMenu2
  End Sub

Private Sub RadioForm_EnableButtons()
  Call EnableWindow(Interface_Control(Interface_Control_Radio_List), -CLng(Not xRadioCount = 0&))
  Call EnableWindow(Interface_Control(Interface_Control_Radio_Play), -CLng(CanIRadio And Not xRadioCount = 0&))
  Call EnableWindow(Interface_Control(Interface_Control_Radio_OpenHP), -CLng(xRadioCount > 0& And UrlsAllowed))
  Call EnableWindow(Interface_Control(Interface_Control_Radio_Add), -CLng(xRadioCount < xRadioCountMax))
  Call EnableWindow(Interface_Control(Interface_Control_Radio_Edit), -CLng(Not xRadioCount = 0&))
  Call EnableWindow(Interface_Control(Interface_Control_Radio_Remove), -CLng(Not xRadioCount = 0&))
  
  If xRadioCount = 0& Then
    EnableMenuItem xRadioMenu, 2802&, 3&
    EnableMenuItem xRadioMenu2, 2802&, 3&
    EnableMenuItem xRadioMenu, 2803&, 3&
    EnableMenuItem xRadioMenu2, 2803&, 3&
    EnableMenuItem xRadioMenu, 2806&, 3&
    EnableMenuItem xRadioMenu2, 2806&, 3&
    EnableMenuItem xRadioMenu, 2807&, 3&
    EnableMenuItem xRadioMenu2, 2807&, 3&
    If Not Interface_Control(Interface_Control_Radio_Txt) = 0& Then
      Call SetFocusAPI(Interface_Control(Interface_Control_Radio_Txt))
      End If
    Else
    If CanIRadio Then
      EnableMenuItem xRadioMenu, 2802&, 0&
      EnableMenuItem xRadioMenu2, 2802&, 0&
      Else
      EnableMenuItem xRadioMenu, 2802&, 3&
      EnableMenuItem xRadioMenu2, 2802&, 3&
      End If
    If UrlsAllowed Then
      EnableMenuItem xRadioMenu, 2803&, 0&
      EnableMenuItem xRadioMenu2, 2803&, 0&
      Else
      EnableMenuItem xRadioMenu, 2803&, 3&
      EnableMenuItem xRadioMenu2, 2803&, 3&
      End If
    EnableMenuItem xRadioMenu, 2806&, 0&
    EnableMenuItem xRadioMenu2, 2806&, 0&
    EnableMenuItem xRadioMenu, 2807&, 0&
    EnableMenuItem xRadioMenu2, 2807&, 0&
    End If
  If xRadioCount < xRadioCountMax Then
    EnableMenuItem xRadioMenu, 2805&, 0&
    EnableMenuItem xRadioMenu2, 2805&, 0&
    Else
    EnableMenuItem xRadioMenu, 2805&, 3&
    EnableMenuItem xRadioMenu2, 2805&, 3&
    End If
  End Sub

Private Sub RadioForm_RemoveSelectedsFromList()
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  For xLong1 = 0& To xRadioCount - 1&
    If SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETSEL, xLong2, 0&) > 0& Then
      Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_DELETESTRING, xLong2, 0&)
      'RemoveListItemMS Interface_Control(Interface_Control_Radio_List), xLong1
      Else
      xRadioCap(xLong2) = xRadioCap(xLong1)
      xRadioURL(xLong2) = xRadioURL(xLong1)
      xRadioHP(xLong2) = xRadioHP(xLong1)
      Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETITEMDATA, xLong2, xLong2)
      xLong2 = xLong2 + 1&
      End If
    Next
  RadioForm_DetectListWidth
  xRadioCount = xLong2
  RadioForm_EnableButtons
  End Sub

Private Sub RadioForm_DetectListWidth()
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim xFntS As Long
  
  xFntS = SelectObject(xSharedDC, hFontPLLg)
  For xLong1 = 0& To xRadioCount - 1&
    xLong2 = GetTextOutWidth(xSharedDC, xRadioCap(xLong1))
    If xLong2 > xLong3 Then xLong3 = xLong2
    Next
  If Not PLIconURL = 0& Then xLong3 = xLong3 + 20&
  Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H194&, 8& + lWidth + xLong3, 0&)
  Call SelectObject(xSharedDC, xFntS)
  End Sub

Private Sub RadioForm_EditUpdateRadioWithList(ByVal Caption As String, ByVal URL As String, ByVal Homepage As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  
  xLong2 = SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETCARETINDEX, 0&, 0&)
  If Not xLong2 = -1& Then
    If SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETSEL, xLong2, 0&) > 0& Then
      Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_DELETESTRING, xLong2, 0&)
      'RemoveListItemMS Interface_Control(Interface_Control_Radio_List), xLong2
      For xLong1 = xLong2 To xRadioCount - 2&
        xRadioCap(xLong1) = xRadioCap(xLong1 + 1&)
        xRadioURL(xLong1) = xRadioURL(xLong1 + 1&)
        xRadioHP(xLong1) = xRadioHP(xLong1 + 1&)
        Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETITEMDATA, xLong1, xLong1)
        Next
      xRadioCount = xRadioCount - 1&
      Call RadioForm_AddRadioEx(Caption, URL, Homepage, True)
      'RadioForm_EditUpdateRadioWithList = RadioForm_AddRadioEx(Caption, URL, Homepage)
      End If
    End If
  End Sub

Public Sub RadioForm_AddRadioEx(ByVal Caption As String, ByVal URL As String, ByVal Homepage As String, ByVal ShowInterface As Boolean)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xChar1 As String
  
  If xRadioCount < xRadioCountMax Then
    URL = Trim(URL)
    If Not Len(URL) = 0& Then
      Caption = Trim(Caption)
      If Len(Caption) = 0& Then Caption = URL
      xChar1 = LCase(Caption)
      For xLong1 = 0& To xRadioCount - 1&
        If LCase(xRadioCap(xLong1)) > xChar1 Then Exit For
        Next
      For xLong2 = xRadioCount To xLong1 + 1& Step -1&
        xRadioCap(xLong2) = xRadioCap(xLong2 - 1&)
        xRadioURL(xLong2) = xRadioURL(xLong2 - 1&)
        xRadioHP(xLong2) = xRadioHP(xLong2 - 1&)
        If ShowInterface Then
          Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETITEMDATA, xLong2, xLong2)
          End If
        Next
      xRadioCap(xLong1) = Caption
      xRadioURL(xLong1) = URL
      xRadioHP(xLong1) = Homepage
      xRadioCount = xRadioCount + 1&
      If ShowInterface Then
        Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_INSERTSTRING, xLong1, xLong1)
        Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETSEL, 0&, -1&)
        Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETSEL, 1&, xLong1)
        Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETCARETINDEX, xLong1, False)
        RadioForm_EnableButtons
        RadioForm_DetectListWidth
        End If
      'RadioForm_AddRadioEx = True
      End If
    End If
  End Sub

Private Sub RadioForm_PrePreset()
  If InterfaceMode = Interface_Mode_Radio Then
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_RESETCONTENT, 0&, 0&)
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), &H194&, 0&, 0&)
    If Not xRadioEditingHWnd = 0& Then Call EndDialog(xRadioEditingHWnd, 1&)
    End If
  End Sub

Private Sub RadioForm_ReListRadios(ByVal ReSelect As Boolean)
  Dim xLong1 As Long
  Dim xChar1 As String

  If ReSelect Then
    If CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3& Then
      xChar1 = RealURL(PLCurFullURL)
      If Len(xChar1) = 0& Then
        ReSelect = False
        End If
      Else
      ReSelect = False
      End If
    End If

  Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_RESETCONTENT, 0&, 0&)
  For xLong1 = 0& To xRadioCount - 1&
    Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_INSERTSTRING, xLong1, xLong1)
'Debug.Print CStr(ReSelect) + "#" + xChar1 + "#" + xRadioURL(xLong1)
    If ReSelect And StrComp(xRadioURL(xLong1), xChar1, vbTextCompare) = 0& Then
      Call SendMessage(Interface_Control(Interface_Control_Radio_List), LB_SETSEL, 1&, xLong1)
      End If
    Next
  RadioForm_EnableButtons
  RadioForm_DetectListWidth
  End Sub

Private Function RadioForm_ButtonProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  
  If Message = &H100& Then
    If wParam = 9& Then
      LoadResEx_Window_ManualTab MainHWnd, hWnd
      Exit Function
      ElseIf wParam = 27& Then
      ToggleInterface Interface_Mode_None
      Exit Function
      End If
    ElseIf Message = &H105& Then 'And (lParam And &H40000000) = 0& Then
    Call RadioForm_TryAccel(wParam)
    End If
  For xLong1 = 0& To Interface_Control_Radio_LastHook
    If Interface_Control(xLong1) = hWnd Then
      RadioForm_ButtonProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Exit For
      End If
    Next
'lblSkipDef:
  End Function

Private Function RadioForm_ResultListProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    'Case &H200& 'move
    'Case &H201&, &H204 'down
    Case &H203& 'dblclick
      RadioForm_TrySCommand Interface_Control(Interface_Control_Radio_Play), 0&
    Case &H205& 'up
      'Debug.Print "RadioRClick"
      RadioForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      'Debug.Print PlayerIsLocked, xAutoDoing, xRadioMenu2
      If Not (PlayerIsLocked Or xAutoDoing) Then ' Or xRadioCount = 0&) Then
        RadioForm_MenuEvent TrackListBox(xStaticIconsHWnd(xStaticIcons_ShowIR), hWnd, ((lParam And &HFFFF0000) \ &H10000), True, xRadioMenu2, 1&)
        End If
      Exit Function
    Case &H100&
      RadioForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Select Case wParam
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
        Case 13&
          Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Play), 0&)
        Case 27&
          ToggleInterface Interface_Mode_None
        Case 93& 'menukey
          If Not (PlayerIsLocked Or xAutoDoing) Then
            'Debug.Print "Radio menu key"
            RadioForm_MenuEvent GetAPIPopupMenu(MainHWnd, xRadioMenu2, Interface_Control(Interface_Control_Radio_List))
            End If
        End Select
      Exit Function
    Case &H105&
      'If (lParam And &H40000000) = 0& Then
        Call RadioForm_TryAccel(wParam)
      '  End If
    Case &H203& ' WM_LBUTTONDBLCLK
      RadioForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
      Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Play), 0&)
      Exit Function
#If ListFocusedLists Then
    Case 7&
      xCurFocusHWnd = hWnd
    Case 8&
      xCurFocusHWnd = wParam
#End If
    End Select
    'Case Else
      RadioForm_ResultListProc = HookedWindowProc(hWnd, Message, wParam, lParam)
    'End Select
'NoProc:
  End Function

Private Sub RadioForm_MenuEvent(ByVal MenuID As Long)
  Select Case MenuID
    Case 2802&: Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Play), 0&)
    Case 2803&: Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_OpenHP), 0&)
    Case 2805&: Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Add), 0&)
    Case 2806&: Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Edit), 0&)
    Case 2807&: Call RadioForm_TrySCommand(Interface_Control(Interface_Control_Radio_Remove), 0&)
    Case 2899&: ToggleInterface Interface_Mode_None
    End Select
  End Sub

Private Sub RadioForm_TrySCommand(ByVal hWnd As Long, ByVal wParam As Long)
'Debug.Print "TiemrSCommand", hwnd, Interface_Control(Interface_Control_Radio_AutoCloseCheck)
  If Not IsWindowEnabled(hWnd) = 0& Then
    Select Case hWnd
      'Case Interface_Control(Interface_Control_Radio_Txt)
      '  If (wParam And &HFFFF0000) = &H3000000 Then
      '    RadioForm_StartManualURL
      '    End If
      Case Interface_Control(Interface_Control_Radio_Go)
        RadioForm_StartManualURL
      Case Interface_Control(Interface_Control_Radio_Play)
'        Debug.Print "Radio Play now!"
  '      If MainForm_TryRadioNowEx Then
  '        If xRadioAutoClose Then ToggleInterface Interface_Mode_None
  '        'If hwnd = Interface_Control(Interface_Control_Radio_play) Then
  '          MainForm_PlayListDoubleClickEvent
  '        '  End If
  '        End If
        RadioForm_StartListURL False
      Case Interface_Control(Interface_Control_Radio_OpenHP)
'        Debug.Print "Radio homepage!"
        RadioForm_StartListURL True
      Case Interface_Control(Interface_Control_Radio_Add)
'        Debug.Print "Radio add"
        xRadioEditingEdit = -1&
        RadioForm_ShowRadioEditor 2800&
      Case Interface_Control(Interface_Control_Radio_Edit)
'        Debug.Print "Radio edit"
        xRadioEditingEdit = SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETCARETINDEX, 0&, 0&)
        If Not xRadioEditingEdit = -1& Then
          If SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETSEL, xRadioEditingEdit, 0&) > 0& Then
            'xRadioEditingEdit = True
            RadioForm_ShowRadioEditor 2810&
            End If
          End If
      Case Interface_Control(Interface_Control_Radio_Remove)
'        Debug.Print "Radio remove"
        RadioForm_RemoveSelectedsFromList
      Case Interface_Control(Interface_Control_Radio_AutoCloseCheck)
        xRadioAutoClose = Not (SendMessage(hWnd, &HF0&, 0&, 0&) = 0&)
      Case Interface_Control(Interface_Control_Radio_Return)
'        Debug.Print "Radio closeinterface"
        ToggleInterface Interface_Mode_None
        'RadioForm_TrySCommand = True
      End Select
    End If
  End Sub

Private Sub RadioForm_Resize(ByVal xRight As Long, ByVal xBottom As Long, ByVal xTop As Long)
  Dim xLong1 As Long
  Dim xLong2 As Long

'  If xRight < 0& Then xRight = 0&
  
  If Interface_Icon = 0& Then
    xLong1 = 4&
    xLong2 = xRight - 8&
    Else
    xLong1 = 44&
    xLong2 = xRight - 44& ' 4 32 4 x 4
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Image), 0&, 4&, 3& + xTop, 32&, 32&, &H215&)
    End If
  If xLong1 < 0& Then xLong1 = 0&
  If CanIRadio Then
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_CapLabel), 0&, xLong1, 3& + xTop, xLong2, 14&, &H214&)
    xLong2 = xLong2 - 40&
    If xLong2 < 0& Then xLong2 = 0&
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Txt), 0&, xLong1, 18& + xTop, xLong2, 20&, &H214&)
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Go), 0&, xLong1 + xLong2, 18& + xTop, 40&, 20&, &H214&)
    Else
    Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_CapLabel), 0&, xLong1, 3& + xTop, xLong2, 31&, &H214&)
    End If
  'Call TrDeferWindowPos(xStaticIconsHWnd(xStaticIcons_DirList), 0&, 0&, 20& + xTop, xRight, 19&, &H214&)

  xLong1 = (xRight - eSPTop5) / 4&
  If xLong1 < 0& Then xLong1 = 0&
  
  xBottom = xTop + xBottom - 25& - eSPTop '- 20&
  If xBottom - 16& - eSPTop < 36& + xTop + 16& Then
    xBottom = 36& + xTop + 16& + eSPTop + 16&
    End If
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Add), 0&, eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Edit), 0&, xLong1 + 2& * eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Remove), 0&, (xLong1 + eSPTop) * 2& + eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Return), 0&, (xLong1 + eSPTop) * 3& + eSPTop, xBottom, xLong1, 25&, &H214&)
  
  xBottom = xBottom - 25& - eSPTop '28&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_Play), 0&, eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_OpenHP), 0&, xLong1 + 2& * eSPTop, xBottom, xLong1, 25&, &H214&)
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_AutoCloseCheck), 0&, (xLong1 + eSPTop) * 3& + eSPTop, xBottom + 5&, xLong1, 16&, &H214&)
  
  'xBottom -4&

  'xLong1 = xBottom - 28& - 40&
  xLong1 = xBottom - 48& - xTop
  If xLong1 < 0& Then xLong1 = 0&
  Call TrDeferWindowPos(Interface_Control(Interface_Control_Radio_List), 0&, 0&, 43& + xTop, xRight, xLong1, &H214&)
  End Sub

Private Sub RadioForm_UpdateRadioVals()
  Call SendMessage(Interface_Control(Interface_Control_Radio_AutoCloseCheck), &HF1&, -CLng(xRadioAutoClose), 0&)
  End Sub

Private Sub RadioForm_TryAccel(ByVal wParam As Long)
  Dim xLong1 As Long
  
  'If wParam = 40& Or wParam = 38& Then
  If wParam = 93& Then
    'If MainForm_TryRadioNow Then
    '  MainForm_MenuEvent GetAPIPopupMenu(xStaticIconsHWnd(xStaticIcons_ShowIR), xMenu(8), Interface_Control(Interface_Control_Radio_List))
    '  End If
'    Debug.Print "Radio menu key 2"
    Else
    For xLong1 = 0& To Interface_Control_Radio_LastAccel
      If Interface_Control_Accel(xLong1) = wParam Then
'Debug.Print "Accelerator:", xLong1
        SetFocusAPI Interface_Control(Interface_Control_Radio_List)
        If Not xLong1 = Interface_Control_Radio_List Then
          CheckboxAccelerate Interface_Control(xLong1)
          Call RadioForm_TrySCommand(Interface_Control(xLong1), 0&)
          End If
        Exit For
        End If
      Next
    End If
  End Sub

Private Function RadioForm_TextProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  'Dim xLong1 As Long
  
  Select Case Message
    Case &H100&
      'down
      Select Case wParam
        Case 9&
          LoadResEx_Window_ManualTab MainHWnd, hWnd
          Exit Function
        Case 27&
          ToggleInterface Interface_Mode_None
          Exit Function
        '33=pgup, 34=pgdown, 36=home, 35=end
        Case 13&
          RadioForm_StartManualURL
          Exit Function
        End Select
'    Case &H102&
'      'press
'      Select Case wParam
'        Case 9&, 13& ', 93&
'          'RadioForm_TextProc = 1&
'          'exit function
'        End Select
    Case &H101&
      'up
      Select Case wParam
        Case 9&, 13&
          Exit Function
        End Select
    Case &H105&
      Call RadioForm_TryAccel(wParam)
    End Select
  RadioForm_TextProc = HookedWindowProc(hWnd, Message, wParam, lParam)
  End Function

Private Sub RadioForm_StartListURL(ByVal LoadHPOnly As Boolean)
  Dim xLong1 As Long
  xLong1 = SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETCARETINDEX, 0&, 0&)
  If Not xLong1 = -1& Then
    If SendMessage(Interface_Control(Interface_Control_Radio_List), LB_GETSEL, xLong1, 0&) > 0& Then
      If LoadHPOnly Then
        SaveOpenURL xRadioHP(xLong1)
        ElseIf StartWeb(xRadioURL(xLong1) + vbCr + xRadioCap(xLong1) + vbCr + xRadioHP(xLong1), True) And xRadioAutoClose Then
        ToggleInterface Interface_Mode_None
        End If
      End If
    End If
  End Sub

Private Sub RadioForm_StartManualURL()
  Dim xChar1 As String
  
  If CanIRadio Then
    xChar1 = GetWindowText(Interface_Control(Interface_Control_Radio_Txt))
    If Not Len(xChar1) = 0& Then
      If StartWeb(xChar1, True) Then
        xRadioLastEnteredURL = xChar1
        If xRadioAutoClose Then
          ToggleInterface Interface_Mode_None
          End If
        End If
      End If
    End If
  End Sub

Private Sub RadioForm_ShowRadioEditor(ByVal DialogID As Long)
  'xRadioEditing = True
  DialogTimer True
  Call DialogBoxIndirectParam(DialogID, MainHWnd, AddressOf RadioForm_RadioEditorProc, 0&)
  DialogTimer False
  xRadioEditingHWnd = 0&
  End Sub

Private Function RadioForm_RadioEditorProc(ByVal hWnd As Long, ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case Message
    Case &H111&
      Select Case wParam
        Case 2808&: RadioForm_ApplyDialog hWnd
        Case 2809&: EndDialog hWnd, 1&
        End Select
    Case &H10&
      EndDialog hWnd, 1&
    Case &H110&
      xRadioEditingHWnd = hWnd
      If Not xRadioEditingEdit = -1& Then
        Call SetDlgItemText(hWnd, 2800&, xRadioURL(xRadioEditingEdit))
        Call SetDlgItemText(hWnd, 2801&, xRadioCap(xRadioEditingEdit))
        Call SetDlgItemText(hWnd, 2802&, xRadioHP(xRadioEditingEdit))
        End If
      RadioForm_RadioEditorProc = 1&
    End Select
  End Function

Private Sub RadioForm_ApplyDialog(ByVal hWnd As Long)
  Dim xLong1 As Long
  Dim xChar1 As String
  
  xLong1 = GetDlgItem(hWnd, 2800&)
  If Not xLong1 = 0& Then
    xChar1 = Trim(GetDlgItemText(hWnd, 2800&))
    If Len(xChar1) = 0& Then
      SetFocusAPI (xLong1)
      Else
      If xRadioEditingEdit = -1& Then
        Call RadioForm_AddRadioEx(GetDlgItemText(hWnd, 2801&), xChar1, GetDlgItemText(hWnd, 2802&), True)
        Else
        Call RadioForm_EditUpdateRadioWithList(GetDlgItemText(hWnd, 2801&), xChar1, GetDlgItemText(hWnd, 2802&))
        End If
      EndDialog hWnd, 1&
      End If
    End If
  End Sub

'ThisTimeShift

#If 0 Then
Public Function GetAPIPopupMenu_ListBoxMouseAddRemove(ByVal hWndCaller As Long, ByVal hWndListBox As Long, ByVal hMenuSel As Long, ByVal hMenuNoSel As Long, ByVal y As Long, ByVal Count As Long, ByVal CountMax As Long) As Long
  Dim xLong1 As Long
  
  xLong1 = SendMessage(hWndListBox, LB_ITEMFROMPOINT, 0&, (&H10000 * y))  ' LB_ITEMFROMPOINT=&H1A9&
  xLong1 = SendMessage(hWndListBox, &H1A9&, 0&, (&H10000 * y))
  If (xLong1 And &HFFFF0000) = 0& And Not SendMessage(hWndListBox, LB_GETCOUNT, 0&, 0&) = 0& Then
    xLong1 = xLong1 And &HFFFF&
    If SendMessage(hWndListBox, LB_GETSEL, xLong1, 0&) > 0& Then
      Call SendMessage(hWndListBox, LB_SETCARETINDEX, xLong1, 1&)
      Else
      Call SendMessage(hWndListBox, LB_SETSEL, 0&, -1&)
      Call SendMessage(hWndListBox, LB_SETSEL, 1&, xLong1)
      Call SendMessage(hWndListBox, LB_SETCARETINDEX, xLong1, 1&)
      End If
    GetAPIPopupMenu_ListBoxMouseAddRemove = GetAPIPopupMenu(hWndCaller, hMenuSel, 0&)
    Else
UnSelected:
    GetAPIPopupMenu_ListBoxMouseAddRemove = GetAPIPopupMenu(hWndCaller, hMenuNoSel, 0&)
    End If
  End Function
#End If

Private Sub CheckboxAccelerate(ByVal hWnd As Long)
  If Not IsWindowEnabled(hWnd) = 0& Then
    If (GetWindowLong(hWnd, -16&) And &HF&) = 3& Then
      Call SendMessage(hWnd, &HF1&, -CLng(SendMessage(hWnd, &HF0&, 0&, 0&) = 0&), 0&)
      End If
    End If
  End Sub

#If MainMenuCloseButton Then
  Private Sub MainMenuCloseButton_Add(ByVal hMenu As Long, ByVal IDClose As Long)
  'Private Const MF_HELP As Long = &H4000&
  'Private Const MF_OWNERDRAW As Long = &H100&
  '  Exit Sub
    If IsUnicode Then
      Call AppendMenuW(hMenu, &H4100&, IDClose, IDClose)
      Else
      Call AppendMenuA(hMenu, &H4100&, IDClose, ByVal IDClose)
      End If
    End Sub
  
  Private Function MainMenuCloseButton_MeasureItem(ByVal lParam As Long) As Long
    Dim xType1 As MEASUREITEMSTRUCT
    
    If Not lParam = 0& Then
      Call RtlMoveMemory(xType1, ByVal lParam, 24&)
      If Not xType1.itemData = 0& Then
        xType1.itemWidth = 16&
        xType1.itemHeight = 16&
        Call RtlMoveMemory(ByVal lParam, xType1, 20&)
        End If
      End If
    MainMenuCloseButton_MeasureItem = 1&
    End Function
  
  Private Function MainMenuCloseButton_DrawItem(ByVal lParam As Long) As Long
    Dim xType1 As DRAWITEMSTRUCT
    'Dim xLong1 As Long
    'Dim xLong2 As Long
    
    If Not lParam = 0& Then
      Call RtlMoveMemory(xType1, ByVal lParam, 48&)
      If Not xType1.hdc = 0& Then
        
        'xLong1 = GetBkColor(xType1.hdc)
        'xLong2 = GetTextColor(xType1.hdc)
        
        With xType1.rcItem
          .Top = .Top + (((.Bottom - .Top) - 12) / 2&)
          .Left = .Right - 12&
          End With
        'Call SetBkColor(xType1.hdc, GetSysColor(5&)) '5&))
        'Call FillRect(xType1.hdc, xType1.rcItem, 6&) '6&)
        
        Call DrawFrameControl(xType1.hdc, xType1.rcItem, 1&, 0&)
        
        
        'Call SetBkColor(xType1.hdc, xLong1)
        'Call SetTextColor(xType1.hdc, xLong2)
        End If
      End If
    MainMenuCloseButton_DrawItem = 1&
    End Function
  #End If

Public Function AppendToTextFileEx(ByVal Filename As String, ByVal xLine As String) As Boolean
  Dim hFile As Long
  
  hFile = TxtFile_WriteAppend(Filename)
  If Not hFile = 0& Then
    AppendToTextFileEx = TxtFile_WriteLine(hFile, xLine)
    Call TxtFile_WriteClose(hFile)
    End If
  End Function

Public Function CalaymSaveSingleFile(ByVal Filter As String, ByVal Caption As String, ByVal InitDir As String, ByVal hWnd As Long, ByVal SetDialogTimer As Boolean) As String
  Dim xType1 As OPENFILENAME
  
  xType1.hWndOwner = hWnd
  xType1.Filter = Filter
  xType1.Title = Caption
  xType1.InitialDirFile = InitDir
  xType1.ErrExistsTitle = LoadResEx_String(1270&)
  xType1.ErrExistsText = LoadResEx_String(1271&)
  xType1.ErrROTitle = LoadResEx_String(1272&)
  xType1.ErrROText = LoadResEx_String(1273&)
  If SetDialogTimer Then DialogTimer True
  CalaymSaveSingleFile = GetSaveFileName(xType1)
  If SetDialogTimer Then DialogTimer False
  End Function

Public Function CalaymLoadSingleFile(ByVal Filter As String, ByVal Caption As String, ByVal InitDir As String, ByVal hWnd As Long, ByVal SetDialogTimer As Boolean) As String
  Dim xType1 As OPENFILENAME
  
  xType1.hWndOwner = hWnd
  xType1.Filter = Filter
  xType1.Title = Caption
  xType1.InitialDirFile = InitDir
  If SetDialogTimer Then DialogTimer True
  CalaymLoadSingleFile = GetOpenFileName(xType1)
  If SetDialogTimer Then DialogTimer False
  End Function

Public Function GetPLTypes(ByVal ForSaving As Boolean) As String
  If ForSaving Then
    If IsUnicode Then
      GetPLTypes = _
                        Replace(Replace(LoadResEx_String(1230&), "$X", "m3u8"), "$O", LoadResEx_String(1241&)) + vbNullChar + "*.m3u8" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1231&), "$X", "m3u"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.m3u" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1231&), "$X", "pls"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.pls" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1231&), "$X", "xspf"), "$O", LoadResEx_String(1241&)) + vbNullChar + "*.xspf" + vbNullChar + _
                        Replace(Replace(Replace(LoadResEx_String(1234&), "$X", "aap"), "$P", "Apollo"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.aap" + vbNullChar + _
                        Replace(Replace(Replace(LoadResEx_String(1234&), "$X", "wpl"), "$P", "Windows Media Player"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.wpl"
      Else
      GetPLTypes = _
                        Replace(Replace(LoadResEx_String(1230&), "$X", "m3u"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.m3u" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1232&), "$X", "m3u8"), "$O", LoadResEx_String(1241&)) + vbNullChar + "*.m3u8" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1231&), "$X", "pls"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.pls" + vbNullChar + _
                        Replace(Replace(LoadResEx_String(1232&), "$X", "xspf"), "$O", LoadResEx_String(1241&)) + vbNullChar + "*.xspf" + vbNullChar + _
                        Replace(Replace(Replace(LoadResEx_String(1234&), "$X", "aap"), "$P", "Apollo"), "$O", LoadResEx_String(1240&)) + vbNullChar + "*.aap" + vbNullChar + _
                        Replace(Replace(Replace(LoadResEx_String(1235&), "$X", "wpl"), "$P", "Windows Media Player"), "$O", LoadResEx_String(1241&)) + vbNullChar + "*.wpl"
'                        Replace(Replace(Replace(LoadResEx_String(1233&), "$X", "fpl"), "$P", "Foobar"), "$O", LoadResEx_String(1281&)) + vbNullChar + "*.fpl"
      End If
    ElseIf IsUnicode Then
    GetPLTypes = Replace(LoadResEx_String(1225&), "$X", "*.m3u; *.m3u8 *.pls; *.xspf; *.aap; *.wpl") + vbNullChar + "*.m3u;*.m3u8;*.pls;*.xspf;*.aap;*.wpl"
    Else
    #If WPL_AnsiTest Then
      GetPLTypes = Replace(LoadResEx_String(1225&), "$X", "*.m3u; *.m3u8 *.pls; *.xspf; *.aap; *.wpl") + vbNullChar + "*.m3u;*.m3u8;*.pls;*.xspf;*.aap;*.wpl"
      #Else
      GetPLTypes = Replace(LoadResEx_String(1225&), "$X", "*.m3u; *.pls; *.aap") + vbNullChar + "*.m3u;*.pls;*.aap;"
      #End If
    End If
  End Function

'Private Function LoadPList(ByVal FileName As String, ByVal InitRun As Boolean, ByVal AddOnly As Boolean, ByVal NoApplySettings As Boolean) As Boolean
Private Function LoadPList(ByVal Filename As String, ByVal AddOnly As Boolean, ByVal NoApplySettings As Boolean) As Boolean ' , ByVal IntroResume As Boolean) As Boolean
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xChar3 As String
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim CurrentDirDepth As Long
  Dim xLong6 As Long
  Dim llResumeMode As Long
  Dim llResumePos As Long
  Dim llResumeTime As Long
  Dim llResumeURL As String
  Dim llResumeURLCP As String
  Dim llResumeURLHP As String
  Dim llResumeNextIndex As Long
  Dim llResumeNextFile As String
  Dim llResumeCurrentIndex As Long
  Dim llResumeCurrentFile As String
  Dim OrgPLCount As Long
  Dim HasMissingSongs As Boolean
  Dim HasMissingDepth As Long
  Const NoMissingDepth As Long = &HFFFFFF
  Dim hFile As Long
  Dim xIsUnicodeExt As Boolean
  Dim FormatHandler As Long
  
  If (PLCount < PLCountMaxX Or Not AddOnly) And Not (Filename = "" Or xAutoDoing) Then ' Or AutoRefreshVisible) Then
    SetAutoDoing True
    xTimerIsEnabled = False
    AbortReading = False
    xPStateIconRefreshing = True
    MainForm_UpdateMainIcon
    MainForm_LockMe True
    OrgPLCount = PLCount
    HasMissingDepth = NoMissingDepth
    
    If Left(Filename, 1&) = Chr(34&) And Right(Filename, 1&) = Chr(34&) And Not Len(Filename) = 1& Then
      Filename = Mid(Filename, 2&, Len(Filename) - 2&)
      End If
    
    RadioForm_PrePreset
' playlistformatsimport
    Select Case GetExtension(Filename)
      Case "m3u8"
        #If WPL_AnsiTest Then
          hFile = TxtFile_ReadOpen(Filename, CP_ACP)
          #Else
          hFile = TxtFile_ReadOpen(Filename, CP_UTF8)
          #End If
        xIsUnicodeExt = True
      Case "m3u"
        hFile = TxtFile_ReadOpen(Filename)
        'xIsUnicodeExt = False
      Case "pls"
        hFile = TxtFile_ReadOpen(Filename, CP_ACP)
        'xIsUnicodeExt = False
        FormatHandler = 1&
      Case "aap"
        hFile = TxtFile_ReadOpen(Filename, CP_ACP)
        'xIsUnicodeExt = False
        FormatHandler = 2&
      Case "wpl"
        #If WPL_AnsiTest Then
          hFile = TxtFile_ReadOpen(Filename, CP_ACP)
          #Else
          hFile = TxtFile_ReadOpen(Filename, CP_UTF8)
          #End If
        'xIsUnicodeExt = False
        FormatHandler = 3&
'        Else
'        hFile = TxtFile_ReadOpen(Filename)
      Case "xspf"
        hFile = TxtFile_ReadOpen(Filename, CP_ACP)
        'xIsUnicodeExt = False
        FormatHandler = 4&
      End Select
    
    If Not hFile = 0& Then
      #If UseFileLogging Then
        MainForm_ctConsoleA_AddLog "Loading playlist (" & Filename & ")"
        #End If
    
      If TxtFile_ReadLine(hFile, xChar1) Then
        If Not AddOnly Then
          xAutoDirCount = 0&
          If Not PlNextSong = "" Then SetNextSong False
          PLCurItem = 0&
          PLCount = 0&
          OrgPLCount = 0&
          xRadioCount = 0&
          RadioForm_PrePreset
          ClearPLEx
          End If
        Select Case FormatHandler
          Case 1&
            ' pls is utf8 without bom, so extra critical
            Call LoadPList_PlsAap(hFile, Filename, xChar1, "file")
            GoTo ForeignPlaylistLoaded
          Case 2&
            Call LoadPList_PlsAap(hFile, Filename, xChar1, "entry")
            GoTo ForeignPlaylistLoaded
          Case 3&
            Call LoadPList_Wpl(hFile, Filename, xChar1)
            GoTo ForeignPlaylistLoaded
          Case 4&
            Call LoadPList_Xspf(hFile, Filename, xChar1)
            GoTo ForeignPlaylistLoaded
          End Select
        If xChar1 = m3uIString Then
          #If UseFileLogging Then
            MainForm_ctConsoleA_AddLog "  (Calaym playlist)"
            #End If
          
          
          Do While TxtFile_ReadLine(hFile, xChar1)
            If Left(xChar1, 1&) = "#" Then
              xLong1 = InStr(xChar1, "=")
              If Not xLong1 = 0& Then
                xChar2 = Mid(xChar1, xLong1 + 1&)
                Select Case UCase(Mid(xChar1, 2&, xLong1 - 2&))
                  Case "DIR"
                    If PLCount = PLCountMaxX Then
                      Exit Do
                      Else
                      #If BoldDirs = 98 Then
                        PLI(PLCount).Filename = ""
                        #Else
                        PLIFile(PLCount) = ""
                        #End If
                      xLong2 = InStr(xChar2, ",")
'Debug.Print "Komma", xLong2, xChar2
                      If xLong2 = 0& Then
                        CurrentDirDepth = 0& ' Val(xChar2)
                        xChar2 = LoadResEx_String(27&)
                        MsgBox "Error 200601a - please report", vbCritical Or vbOKOnly, "Warning"
                        Else
                        xLong3 = Val(Left(xChar2, xLong2 - 1&))
'Debug.Print "Depth", xLong3, CurrentDirDepth
                        If xLong3 > CurrentDirDepth Then
                          #If UseFileLogging Then
                            MainForm_ctConsoleA_AddLog "    DropInvalidDepthIncrement " + xChar2
                            #End If
                          GoTo IgnoreThisDir
                          Else
                          CurrentDirDepth = xLong3
                          xChar2 = Mid(xChar2, xLong2 + 1&)
                          If RemoveMissingOnPLLoad Then
                            If CurrentDirDepth > HasMissingDepth Then
                              #If UseFileLogging Then
                                MainForm_ctConsoleA_AddLog "    DropMissing SubDir " + xChar2
                                #End If
                              GoTo IgnoreThisDir
                              End If
  'Debug.Print xChar2
                            xLong2 = GetFileAttributes(xChar2)
                            If xLong2 = -1& Or (xLong2 And 16&) = 0& Then
                              HasMissingDepth = CurrentDirDepth
                              HasMissingSongs = True
                              #If UseFileLogging Then
                                MainForm_ctConsoleA_AddLog "    DropMissing FirstDir" + xChar2
                                #End If
                              GoTo IgnoreThisDir
                              Else
                              HasMissingDepth = NoMissingDepth
                              End If
                            End If
                          #If BoldDirs = 98 Then
                            PLI(PLCount).Filename = xChar2
                            #Else
                            PLIFile(PLCount) = xChar2
                            #End If
                          If Not CurrentDirDepth = 0& Then
                            xChar1 = SimpleFile(xChar2)
                            If Not xChar1 = "" Then
                              xChar2 = xChar1
                              End If
                            End If
                          End If
                        End If
                      If xChar2 = "" Then
                        xChar2 = LoadResEx_String(27&)
                        MsgBox "Error 200601a - please report", vbCritical Or vbOKOnly, "Warning"
                        End If
                      #If BoldDirs = 98 Then
                        PLI(PLCount).Caption = xChar2
                        PLI(PLCount).DirDepthFlags = CurrentDirDepth Or PLI_IsDir
                        #Else
                        PLICap(PLCount) = xChar2
                        PLIDF(PLCount) = CurrentDirDepth Or PLI_IsDir
                        #End If
                      CurrentDirDepth = CurrentDirDepth + 1& 'CurrentDirDepth * 2& + 4&
                      PLCount = PLCount + 1&
                      If PLCount = PLCountMax Then Exit Do
IgnoreThisDir:
                      End If
                  Case "AUTODIR"
                    If Not (xAutoDirCount = xAutoDirCountMax Or xNoAutoRefresh) Then
                      xChar1 = LCase(Trim(xChar2))
                      xChar2 = xChar1 & "\"
                      xLong3 = Len(xChar2)
                      If (Mid(xChar2, 2&, 1&) = ":" Or Left(xChar2, 2&) = "\\") Then
                        For xLong1 = xAutoDirCount - 1& To 0& Step -1&
                          If Left(xChar1, Len(xAutoDir(xLong1)) + 1&) = xAutoDir(xLong1) & "\" Then
                            xChar1 = ""
                            Exit For
                            ElseIf xAutoDir(xLong1) = xChar1 Then
                            xChar1 = ""
                            Exit For
                            ElseIf Left(xAutoDir(xLong1), xLong3) = xChar2 Then
                            For xLong2 = xLong1 To xAutoDirCount - 2&
                              xAutoDir(xLong2) = xAutoDir(xLong2 + 1&)
                              Next
                            xAutoDirCount = xAutoDirCount - 1&
                            End If
                          Next
                        If Not xChar1 = "" Then
                          xLong3 = xAutoDirCount
                          For xLong1 = 0& To xAutoDirCount - 1&
                            If xChar1 < xAutoDir(xLong1) Then
                              xLong3 = xLong1
                              Exit For
                              End If
                            Next
                          For xLong1 = xAutoDirCount To xLong3 + 1& Step -1&
                            xAutoDir(xLong1) = xAutoDir(xLong1 - 1&)
                            Next
                          xAutoDirCount = xAutoDirCount + 1&
                          xAutoDir(xLong3) = xChar1
                          End If
                        End If
                      End If
                  Case "AUTOREFRESHTIMER"
                    xLong1 = Val(xChar2)
                    If Not (AddOnly Or Abs(xLong1) < xAutoRefreshTimeMinAll Or Abs(xLong1) > xAutoRefreshTimeMaxAll) Then
                      xAutoRefreshTimer(0) = CLng(Val(xChar2))
                      End If
                  Case "AUTOREFRESHTIMERMONITOR"
                    xLong1 = Val(xChar2)
                    If Not (AddOnly Or Abs(xLong1) < xAutoRefreshTimeMin Or Abs(xLong1) > xAutoRefreshTimeMax) Then
                      xAutoRefreshTimer(1) = CLng(Val(xChar2))
                      End If
                  Case "CURRENTSONG"
                    If Not AddOnly Then
                      xLong1 = InStr(xChar2 & ",", ",")
                      llResumeCurrentFile = Mid(xChar2, xLong1 + 1&)
                      llResumeCurrentIndex = CLng(Val(Left(xChar2, xLong1 - 1&)))
                      If Not xLong1 = 0& Then PLCurItem = xLong1 - 1&
                      End If
                  Case "CURRENTSONGPOS"
                    If Not AddOnly Then
                      xLong1 = InStr(xChar2 & ",", ",")
                      If Not xLong1 = 0& Then
                        llResumeMode = CLng(Val(Left(xChar2, xLong1 - 1&))) + 1&
                        xChar2 = Mid(xChar2, xLong1 + 1&)
                        xLong1 = InStr(xChar2 & ",", ",")
                        If xLong1 = 0& Then
                          llResumePos = CLng(Val(xChar2))
                          Else
                          llResumePos = CLng(Val(Left(xChar2, xLong1 - 1&)))
                          llResumeTime = CLng(Val(Mid(xChar2, xLong1 + 1&)))
                          End If
                        If (llResumePos < 0& Or llResumeTime < 0&) Or Not (llResumeMode = 1& Or llResumeMode = 2&) Then
                          llResumeMode = 0&
                          End If
                        End If
                      End If
                  Case "CURRENTURL"
                    If CanIRadio And Not AddOnly Then
                      xLong1 = InStr(xChar2 & ",", ",")
                      If Not xLong1 = 0& Then
                        llResumeMode = 3& - CLng(Val(Left(xChar2, xLong1 - 1&)) > 0&)
                        llResumeURL = RealURL(Mid(xChar2, xLong1 + 1&))
                        If Len(llResumeURL) = 0& Then llResumeMode = 0&
                        End If
                      End If
                  Case "CURRENTURLCAPTION"
                    llResumeURLCP = RealURL(xChar2)
                  Case "CURRENTURLHOMEPAGE"
                    llResumeURLHP = RealURL(xChar2)
                  Case "NEXTSONG"
                    'If IntroResume Then
                    If NoApplySettings Then
                      xLong1 = InStr(xChar2 & ",", ",")
                      llResumeNextFile = Mid(xChar2, xLong1 + 1&)
                      llResumeNextIndex = CLng(Val(Left(xChar2, xLong1 - 1&)))
                      If Not xLong1 = 0& Then PLNext = xLong1 - 1&
                      End If
                  Case "REMOVEMISSINGSONGS"
                    If UsePLSettings And Not (AddOnly Or NoApplySettings) Then
                      xRemoveMissingSongs = (CLng(Val(xChar2)) > 0&)
                      CheckApiMenu 320&, xRemoveMissingSongs
                      End If
                  Case "REMOVEPLAYEDSONGS"
                    If UsePLSettings And Not (AddOnly Or NoApplySettings) Then
                      xRemovePlayedSongs = (CLng(Val(xChar2)) > 0&)
                      CheckApiMenu 322&, xRemovePlayedSongs
                      End If
                  Case "REMOVESKIPPEDSONGS"
                    If UsePLSettings And Not (AddOnly Or NoApplySettings) Then
                      xRemoveSkippedSongs = (CLng(Val(xChar2)) > 0&)
                      CheckApiMenu 321&, xRemoveSkippedSongs
                      End If
                  Case "REPEATMODE"
                    xLong1 = CLng(Val(xChar2))
                    If UsePLSettings And Not (AddOnly Or NoApplySettings Or xLong1 < 0& Or xLong1 > 2& Or xLong1 = xRep) Then
                      xRep = xLong1
                      MainForm_ApplyRepMode
                      End If
                  Case "RADIO"
                    If TxtFile_ReadLine(hFile, xChar1) Then
                      If Left(xChar1, 1&) = "#" Then
                        If LCase(Left(xChar1, 15&)) = "#radiohomepage=" Then
                          If TxtFile_ReadLine(hFile, xChar3) Then
                            xChar1 = Trim(Mid(xChar1, 16&))
                            GoTo RadioIsOK
                            End If
                          End If
                        Else
                        xChar3 = xChar1
                        xChar1 = ""
RadioIsOK:
                        RadioForm_AddRadioEx xChar2, xChar3, xChar1, False
                        End If
                      End If
                  End Select
                End If
              ElseIf Not Len(xChar1) = 0& Then
              If RemoveMissingOnPLLoad Then
                If HasMissingDepth = NoMissingDepth Then
                  If Not (AllowedItem(xChar1, True) = 1&) Then
                    #If UseFileLogging Then
                      MainForm_ctConsoleA_AddLog "    DropMissing File    " + xChar1
                      #End If
                    HasMissingSongs = True
                    GoTo IgnoreThisFile
                    End If
                  Else
                  #If UseFileLogging Then
                    MainForm_ctConsoleA_AddLog "    DropMissing SubFile " + xChar1
                    #End If
                  GoTo IgnoreThisFile
                  End If
                Else
                If Not AllowedItemSongFast(xChar1, True) Then
                  HasMissingSongs = True
                  GoTo IgnoreThisFile
                  End If
                End If
              #If BoldDirs = 98 Then
                PLI(PLCount).Filename = xChar1
                PLI(PLCount).Caption = SimpleFile(xChar1)
                PLI(PLCount).DirDepthFlags = CurrentDirDepth
                #Else
                PLIFile(PLCount) = xChar1
                PLICap(PLCount) = SimpleFile(xChar1)
                PLIDF(PLCount) = CurrentDirDepth
                #End If
              PLCount = PLCount + 1&
              If PLCount = PLCountMax Then Exit Do
IgnoreThisFile:
              xLong6 = (xLong6 + 1&) Mod 64&
              If xLong6 = 0& Then
                Call DoEventsEx_SleepPeriodicalLW(0&)
                If AbortReading Then Exit Do
                End If
              End If
            Loop
          If HasMissingSongs Then CheckEmptyNodes True, -1&
          Else
          LoadPList_M3uGeneric hFile, Filename, xChar1
          End If
        If IsUnicode = xIsUnicodeExt And Not AddOnly Then
          xCurPlistFilename = Filename
          xCurPlistFilenameSuggestNew = False
          Else
ForeignPlaylistLoaded:
          xCurPlistFilenameSuggestNew = True
          End If
        LoadPList = True
        End If
      If Not hFile = 0& Then Call TxtFile_ReadClose(hFile)
#If UseFileLogging Then
      Else
    MainForm_ctConsoleA_AddLog "Error loading playlist (" + Filename + ")"
#End If
      End If
     
    
    
    
    
    
    'CheckEmptyNodes True, -1&
    If Not xNoAutoRefreshByUser Then
      MainForm_EnableApiMenu 411&, (Not xAutoDirCount = 0&)
      MainForm_EnableApiMenu 412&, (Not xAutoDirCount = 0&)
      MainForm_EnableApiMenu 413&, (Not xAutoDirCount = 0&)
      MainForm_EnableApiMenu 414&, (Not xAutoDirCount = 0&)
      If Not AbortReading Then
        MainForm_xAutoRefreshNow True, True, True, True, True
        End If
      MainForm_LockMe True
      End If
    
    If Not PLCount = OrgPLCount Then
      InitResearchClear
      RealPLAdd OrgPLCount
      If xAutoDoing Then SetAutoDoing False
'Debug.Print xAutoDoing, PlayerIsLocked
      InitResearch True
      End If
    
    'If IntroResume And Not llResumeNextFile = "" Then
    If NoApplySettings And Not llResumeNextFile = "" Then
      If Not GetRealIndex(llResumeNextIndex, llResumeNextFile) Then
        llResumeNextIndex = 0&
        End If
      End If
    If Len(llResumeCurrentFile) = 0& Then
      GoTo NoRes
      ElseIf GetRealIndex(llResumeCurrentIndex, llResumeCurrentFile) Then
FoundNoRes:
      PLCurItem = llResumeCurrentIndex
      xMarkSong PLCurItem, False, False
      xFollowCurSongLast = DoEventsEx_GetTickCount
      xFollowCurSongLastDiff = xFollowCurSongLastDiffDef
      Else
NoRes:
      llResumePos = 0&
      llResumeTime = 0&
      If Not Len(llResumeNextFile) = 0& Then
        If GetRealIndex(llResumeNextIndex, llResumeNextFile) Then
          llResumeCurrentIndex = llResumeNextIndex
          llResumeCurrentFile = llResumeNextFile
          llResumeNextIndex = 0&
          llResumeNextFile = ""
          GoTo FoundNoRes
          End If
        End If
      If Len(llResumeURL) = 0& Then llResumeMode = 0&
      End If
      
    'If IntroResume And Not llResumeMode = 0& Then
    If NoApplySettings And Not llResumeMode = 0& Then
      xPStateIconPlaying = True
      If xAutoPlayback Or Not InStr(CommandX, "/PLAY") = 0& Then
        If llResumeMode = 1& Then
          llResumeMode = 2&
          ElseIf llResumeMode = 3& Then
          llResumeMode = 4&
          End If
        End If
      'Debug.Print llResumeMode, llResumeURL
      If llResumeMode > 2& Then
        If Not Len(llResumeURLCP) = 0& Then
          llResumeURL = llResumeURL + vbCr + llResumeURLCP
          If Not Len(llResumeURLHP) = 0& Then
            llResumeURL = llResumeURL + vbCr + llResumeURLHP
            End If
          End If
'        Debug.Print "ResumeURL:", llResumeURL
        If xPlayItem(False, llResumeMode = 4&, False, True, llResumeMode = 3&, llResumeURL) Then
          If Not CurMusic = ctSSLayer_MusicTypeNone Then
            llResumeMode = llResumeMode - 2&
            xPStateIconPaused = (llResumeMode = 1&)
            MainForm_UpdateMainIcon
            GoTo HackIt
            End If
          End If
        End If
      If Len(llResumeCurrentFile) = 0& Then
        xPStateIconPlaying = False
        If Not llResumeNextIndex = 0& Then
ReSetNext:
          PLNext = llResumeNextIndex
          PlNextSong = llResumeNextFile
          PlNextSongCap = SimpleFile(PlNextSong)
          SetNextSong True
          End If
        ElseIf xPlayItem(False, llResumeMode = 2&, False, True, llResumeMode = 1&, "") Then
        If Not CurMusic = ctSSLayer_MusicTypeNone Then
          xPStateIconPaused = (llResumeMode = 1&)
          MainForm_UpdateMainIcon
          If llResumePos < PLCurSongLength Then
            Call ctSSLayer_SeekSong(SongPointer(CurrentSongID), llResumePos)
            End If
          If xEndSongTimerEventTime > 0& Then
            xEndSongTimerEventTime = (xEndSongTimerEventTime - (llResumeTime And &H3FFFFFFF) + &H40000000) And &H3FFFFFFF
            End If
HackIt:
          
          If llResumeMode = 2& Then
            If (xGenFading And Tear_StepT(CurrentSongID) = 0&) And Not xPStateIconPaused Then
              Tear_Volume(CurrentSongID) = 0&
              Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 0&, 0&, LongFadingTotalTimeStrt, 1&)
              'SimpleFadingTotalTime
              ElseIf Tear_StepT(CurrentSongID) = 0& And Not xPStateIconPaused Then
              Tear_Volume(CurrentSongID) = 0&
              Call Tear_StartDropping(CurrentSongID, xSongIsMidi, 0&, 0&, LongFadingTotalTimeStrtF, 1&)
              Else
              Tear_Volume(CurrentSongID) = 1&
              Call ctSSLayer_SetVolume(SongPointer(CurrentSongID), SimpleFadingTotalTime) 'xSetVolume)
              End If
            End If
          If Not llResumeNextIndex = 0& Then GoTo ReSetNext
          ElseIf Not llResumeNextIndex = 0& Then
          GoTo xx7
          Else
          xPStateIconPlaying = False
          End If
        ElseIf Not llResumeNextIndex = 0& Then
xx7:
        PLCurItem = llResumeNextIndex
        If Not xPlayItem(xGenFading, False, False, True, False, "") Then
          PLCurItem = llResumeCurrentIndex
          xPStateIconPlaying = False
          End If
        Else
        xPStateIconPlaying = False
        End If
      End If
    
    If InterfaceMode = Interface_Mode_Radio Then
      RadioForm_ReListRadios False
      End If
    
    xTimerIsEnabled = True
    MainForm_LockMe False
    
    If xAutoDoing Then SetAutoDoing False
    CheckReplay
    xPStateIconRefreshing = False
    MainForm_UpdateMainIcon
    ElseIf xAutoDoing Then
    SetAutoDoing False
    End If
  End Function

Private Sub SavePList_M3uCalaym(ByRef hTxtFileOut As Long)
  Dim xLong1 As Long
  Dim xChar1 As String

  Call TxtFile_WriteLine(hTxtFileOut, m3uIString)
  Call TxtFile_WriteLine(hTxtFileOut, "#AutoRefreshTimer=" & CStr(xAutoRefreshTimer(0)))
  Call TxtFile_WriteLine(hTxtFileOut, "#AutoRefreshTimerMonitor=" & CStr(xAutoRefreshTimer(1)))
  If PLCurSong = "" Then
    Call TxtFile_WriteLine(hTxtFileOut, "#CurrentSong=" & CStr(PLCurItem + 1&))
    Else
    Call TxtFile_WriteLine(hTxtFileOut, "#CurrentSong=" & CStr(PLCurItem + 1&) & "," & PLCurSong)
    End If
  If xPStateIconPlaying And Not (CurMusic = ctSSLayer_MusicTypeNone Or xSongIsMidi = 2&) Then
    'else ... TxtFile_WriteLine (hTxtFileOut, "#CurrentSongPos=0,0"
    If (CurMusic = ctSSLayer_MusicTypeStream And xSongIsMidi = 3&) Then
      Call TxtFile_WriteLine(hTxtFileOut, "#CurrentURL=" + Chr(49& + CLng(xPStateIconPaused)) + "," + _
        Replace(Replace(Replace(Replace(Replace(PLCurFullURL, vbLf, ""), vbCr, vbLf + "#CurrentURLCaption=", , 1&), vbCr, vbLf + "#CurrentURLHomepage=", , 1&), vbCr, ""), vbLf, vbCrLf))
      ElseIf Not PLCount = 0& Then
      Call TxtFile_WriteLine(hTxtFileOut, "#CurrentSongPos=" + Chr(49& + CLng(xPStateIconPaused)) + "," + CStr(PLCurSongPos) & "," & CStr(DoEventsEx_GetElapsedTime(xEndSongTimerEventTime)))
      End If
    End If
  If Not (PLCount = 0& Or PlNextSong = "") Then
  '  TxtFile_WriteLine (hTxtFileOut, "#NextSong=0"
  '  Else
    Call TxtFile_WriteLine(hTxtFileOut, "#NextSong=" & CStr(PLNext + 1&) & "," & PlNextSong)
    End If
  Call TxtFile_WriteLine(hTxtFileOut, "#RemoveMissingSongs=" & BoolChar(xRemoveMissingSongs))
  Call TxtFile_WriteLine(hTxtFileOut, "#RemovePlayedSongs=" & BoolChar(xRemovePlayedSongs))
  Call TxtFile_WriteLine(hTxtFileOut, "#RemoveSkippedSongs=" & BoolChar(xRemoveSkippedSongs))
  Call TxtFile_WriteLine(hTxtFileOut, "#RepeatMode=" & CStr(xRep))
  'If Not xAutoDirCount = 0& Then
    For xLong1 = 0& To xAutoDirCount - 1&
      Call TxtFile_WriteLine(hTxtFileOut, "#AutoDir=" + xAutoDir(xLong1))
      Next
  '  End If
  For xLong1 = 0& To xRadioCount - 1&
    Call TxtFile_WriteLine(hTxtFileOut, "#Radio=" + xRadioCap(xLong1))
    If Not Len(xRadioHP(xLong1)) = 0& Then
      Call TxtFile_WriteLine(hTxtFileOut, "#RadioHomepage=" + xRadioHP(xLong1))
      End If
    Call TxtFile_WriteLine(hTxtFileOut, xRadioURL(xLong1))
    Next
  For xLong1 = 0& To PLCount - 1&
    If (PLI(xLong1).DirDepthFlags And PLI_IsAuto) = 0& Then
      If (PLI(xLong1).DirDepthFlags And PLI_IsDir) = PLI_IsDir Then
        xChar1 = PLI(xLong1).Filename
        If Not xChar1 = "" Then xChar1 = "," & xChar1
        Call TxtFile_WriteLine(hTxtFileOut, "#Dir=" & CStr(PLI(xLong1).DirDepthFlags And PLI_Depth) & xChar1)
        Else
        Call TxtFile_WriteLine(hTxtFileOut, PLI(xLong1).Filename)
        End If
      End If
    Next
  End Sub

Private Function SavePList_M3u(ByVal Filename As String, ByVal OverWriteDef As Boolean) As Boolean
  Dim hTxtFileOut As Long

  #If UseFileLogging Then
    Dim xSuccess As Boolean
    If IsUnicode Then
      MainForm_ctConsoleA_AddLog "Saving playlist (m3u ansi generic): " & Filename
      Else
      MainForm_ctConsoleA_AddLog "Saving playlist (m3u ansi calaym): " & Filename
      End If
    #End If
    
  hTxtFileOut = TxtFile_WriteCreateEx(Filename, CP_ACP, False)
  If Not hTxtFileOut = 0& Then
    If IsUnicode Then
      Call SavePList_M3uGeneric(hTxtFileOut)
      Else
      Call SavePList_M3uCalaym(hTxtFileOut)
      If OverWriteDef And Not hTxtFileOut = 0& Then
        xCurPlistFilename = Filename
        xCurPlistFilenameSuggestNew = False
        End If
      End If
    #If UseFileLogging Then
      xSuccess = True
      #End If
    Call TxtFile_WriteClose(hTxtFileOut)
    SavePList_M3u = True
    End If
  #If UseFileLogging Then
    If Not xSuccess Then
      MainForm_ctConsoleA_AddLog "  failed"
      End If
    #End If
  End Function

Private Function SavePList_M3u8(ByVal Filename As String, ByVal OverWriteDef As Boolean) As Boolean
  Dim hTxtFileOut As Long

  #If UseFileLogging Then
    Dim xSuccess As Boolean
    If IsUnicode Then
      MainForm_ctConsoleA_AddLog "Saving playlist (m3u8 utf8 calaym): " & Filename
      Else
      MainForm_ctConsoleA_AddLog "Saving playlist (m3u8 utf8 generic): " & Filename
      End If
    #End If
    
  hTxtFileOut = TxtFile_WriteCreateEx(Filename, CP_UTF8, True)
  If Not hTxtFileOut = 0& Then
    If IsUnicode Then
      Call SavePList_M3uCalaym(hTxtFileOut)
      If OverWriteDef And Not hTxtFileOut = 0& Then
        xCurPlistFilename = Filename
        xCurPlistFilenameSuggestNew = False
        End If
      Else
      Call SavePList_M3uGeneric(hTxtFileOut)
      End If
    #If UseFileLogging Then
      xSuccess = True
      #End If
    Call TxtFile_WriteClose(hTxtFileOut)
    SavePList_M3u8 = True
    End If
  #If UseFileLogging Then
    If Not xSuccess Then
      MainForm_ctConsoleA_AddLog "  failed"
      End If
    #End If
  End Function

Private Function SavePList(ByVal Filename As String, ByVal OverWriteDef As Boolean) As Boolean
  'If Len(Filename) = 0& Then Filename = xCurPlistFilename
  'Debug.Print GetExtension(Filename)
  Select Case GetExtension(Filename)
    Case "m3u8": SavePList = SavePList_M3u8(Filename, OverWriteDef)
    Case "m3u":  SavePList = SavePList_M3u(Filename, OverWriteDef)
    Case "pls":  SavePList = SavePList_PlsAap(Filename, "File", False)
    Case "xspf": SavePList = SavePList_Xspf(Filename)
    Case "aap":  SavePList = SavePList_PlsAap(Filename, "Entry", True)
    Case "wpl":  SavePList = SavePList_Wpl(Filename)
    End Select
  End Function

Public Function AllowedItem(ByVal Filename As String, ByVal MustExist As Boolean) As Long
#If CanEnumComputerShares Then
  If Not (Left(Filename, 2&) = "\\" And InStr(3&, Filename, "\") = 0&) Then
    'AllowedItem = 0&
    ' computer sharing parend, e.g. \\LAPTOP
    Else
#End If
    If MustExist Then
      AllowedItem = GetFileAttributes(Filename)
      If (AllowedItem And 16&) = 16& Then
        AllowedItem = 0&
        Exit Function
        ElseIf AllowedItem = -1& Then
        Exit Function
        End If
      'Else
      End If
    Filename = GetExtension(Filename)
    If InStr(xSupportedFiles, vbNullChar & Filename & vbNullChar) = 0& Then
' playlistformatsimport
      Select Case Filename
        Case "m3u", "pls", "aap"
          AllowedItem = 255&
        Case "m3u8", "xspf", "wpl"
          If IsUnicode Or Not MustExist Then
            AllowedItem = 255&
            End If
          #If WPL_AnsiTest Then
            AllowedItem = 255&
            #End If
        Case "cpt"
          AllowedItem = 254&
        Case "ini", "cps"
          AllowedItem = 253&
        Case Else
          AllowedItem = -1&
        End Select
      Else
      AllowedItem = 1&
      End If
#If CanEnumComputerShares Then
    End If
#End If
  End Function

Private Function AllowedItemPLFast(ByVal ShortFilename As String) As Boolean
  Select Case GetExtension(ShortFilename)
    Case "m3u", "pls", "aap"
      AllowedItemPLFast = True
    Case "m3u8", "xspf", "wpl"
      #If WPL_AnsiTest Then
        AllowedItemPLFast = True
        #Else
        AllowedItemPLFast = IsUnicode
        #End If
    End Select
  End Function

Public Function AllowedItemSongFast(ByVal ShortFilename As String, ByVal IgnoreNotEnabled As Boolean) As Boolean
  ShortFilename = GetExtension(ShortFilename)
  If Not InStr(xSupportedFiles, vbNullChar & ShortFilename & vbNullChar) = 0& Then
    AllowedItemSongFast = (Not InStr(xUsedTypes, vbNullChar & ShortFilename & vbNullChar) = 0& Or IgnoreNotEnabled)
    End If
  End Function

