Const LVM_SORTITEMSEX = LVM_FIRST + 81

Type LV_DISPINFO
	hdr  As NMHDR
	item As LV_ITEM
End Type

'Editing inside listview
Type EditLVItem
	As HWND TheLView 'since using 2/8 listviews -- remember which one
	As HWND EditMe 'handle for edit box created
	As String OldValue 'original value in Listview
	As BOOL IsDir 'flag if is a folder
	As String ThePath 'path for renaming
	As Integer ItemNum 'which item editing
End Type

'functions in strwrap.bi
Function str_numparse(ByRef source as string,ByRef delimiter as string) As long
	Dim As Long s=1,c,l
	l=Len(delimiter)
	Do
		s=instr(s,source,delimiter)
		If s then
			c+=1
			s+=l
		end if
	Loop While s
	Function=c+1
End Function

Function str_parse(ByRef source As String,Byref delimiter As String,ByVal idx As Long)As String
	Dim As Long s=1,c,l
	l=Len(delimiter)
	Do
		If c=idx-1 then Return mid(source,s,instr(s,source,delimiter)-s)
		s=instr(s,source,delimiter)
		If s then
			c+=1
			s+=l
		end if
	Loop While s
End Function

Function Str_replaceany(ByRef match As String,Byref sreplace As String,Byref source As String)As String
	Dim As Long s=1,c=len(sreplace)
	Dim As String text
	text=source
	Do
		s=InStr(s,text,Any match)
		If s then
			text=Mid$(text,1,s-1)+sreplace+Mid$(text,s+1,-1)
			s+=c
		end if
	Loop While s
	Function=text
End Function

Function str_ReplaceAnyAll(ByRef match As String,ByRef sreplace As String,ByRef source As String)As String
	Dim text As String
	text=source
	While InStr(text,Any match)
		text=str_replaceany(match,sreplace,text)
	Wend
	Function=text
End Function

Sub LV_SetSelState(H As HWND,R As Integer,MakeSel As BOOL)
	'macro ListView_SetItemState not defined
	Dim As LV_ITEM lvi
	Dim As Integer x, NumCol = 2
	'NumCol will be set at start up....shared var
	lvi.mask = LVIF_STATE
	lvi.iItem = R
	For x = 0 To NumCol
		'do all col in this row
		lvi.iSubItem = x
		ListView_GetItem(H,@lvi)
		lvi.stateMask = LVIS_SELECTED
		If MakeSel Then
			lvi.state = LVIS_SELECTED 'LVIS_FOCUSED
		Else
			lvi.state = 0
		EndIf
		SendMessage(H,LVM_SETITEMSTATE,R,@lvi)
	Next
End Sub

'report style
Sub LVAddItem(hList As HWND, Rec As String,InZip As Integer=0)
	Dim z As Integer
	Dim iStatus As Integer
	Dim szStr As ZString * 300
	Dim lvi As LV_ITEM
	'
	'this will be the next record
	lvi.iItem = SendMessage(hlist,LVM_GETITEMCOUNT,0,0)'ListView_GetItemCount(hList) '+ 1
	lvi.stateMask = LVIS_FOCUSED '%LVIS_SELECTED
	lvi.pszText = @szStr
	'
	For z = 0 To str_numparse(Rec,"|")-1
		szStr = str_parse(Rec,"|",z+1)
		lvi.iSubItem = z
		lvi.lParam = lvi.iItem
		If z = 0 Then
			lvi.mask = LVIF_TEXT Or LVIF_PARAM Or LVIF_STATE
			If szStr <> "" Then
				'doubt will occur on ftp
				Dim As String Exe = wtTop
				If hList = FBottom(BotLVType) Then Exe = wtBottom
				lvi.mask Or= LVIF_IMAGE
				lvi.iImage = SetIcon(Exe,szStr,InZip)
				'Print lvi.iImage
			EndIf
			iStatus = ListView_InsertItem (hList, @lvi)
		Else
			lvi.mask = LVIF_TEXT
			iStatus = ListView_SetItem (hList, @lvi)
		End If
	Next
End Sub

'small and large icon
Sub IconicLVAddItem(LV As HWND, Stats As FileStats)
	Dim szStr As ZString * 300
	Dim lvi As LV_ITEM
	Dim x As Integer
	'
	lvi.iItem = SendMessage(LV,LVM_GETITEMCOUNT,0,0)
	lvi.stateMask = LVIS_FOCUSED '%LVIS_SELECTED
	lvi.lParam = lvi.iItem
	lvi.mask = LVIF_TEXT Or LVIF_PARAM Or LVIF_STATE
	If Stats.FName <> "" Then
		Dim As String Exe = wtTop
		'doubt will occur on ftp
		If LV = FBottom(BotLVType) Then Exe = wtBottom
		lvi.mask Or= LVIF_IMAGE
		lvi.iImage = SetIcon(Exe,Stats.FName,FALSE)
		szStr = Stats.FName
	Else
		szStr = "Folder is empty."
	EndIf
	lvi.pszText = @szStr
	x = ListView_InsertItem(LV, @lvi)
End Sub

Function LVGetValue(ByVal rr As HWND, ByVal item As Long, ByVal Column As Long=0) As String
	Dim ms_lvi As LV_ITEM
	Dim psztext As ZString * MAX_PATH
	ms_lvi.iSubItem = Column
	ms_lvi.cchTextMax = MAX_PATH
	ms_lvi.pszText = @pszText
	SendMessage rr, LVM_GETITEMTEXT, item, @ms_lvi
	Function = psztext
End Function

Function lvFindItem(hList As HWND, Value As String) As Integer
	Dim lvf As LVFINDINFO
	Dim i As Integer
	Dim szStr As ZString * 300
	Dim iStatus As Integer
	'
	lvf.flags = LVFI_STRING Or LVFI_Partial
	szStr = UCase(Value)
	lvf.psz = @szStr
	i = SendMessage(hList,LVM_FINDITEM, -1, @lvf)
	If i >= 0 Then
		istatus = ListView_EnsureVisible(hList, i, TRUE)
	End If
	Function=i
End Function

Sub LVSetHeader(lvHdl As HWND, ColNum As Long, txtItem As String, ColW As Long)
	Dim tlvc          As LVCOLUMN             ' specifies or receives the attributes of a listview column
	'
	ColNum -= 1 ' Column is zero based
	tlvc.mask     = LVCF_FMT Or LVCF_WIDTH Or LVCF_TEXT Or LVCF_SUBITEM
	tlvc.fmt      = LVCFMT_LEFT
	tlvc.cx       = ColW
	tlvc.pszText  = StrPtr(txtItem)
	tlvc.iSubItem = ColNum
	SendMessage lvHdl, LVM_SETCOLUMN, ColNum, @tlvc
End Sub

Sub LVSetMaxCols(ByVal rr As HWND,ByVal cols As Long)
	Dim tlvc  As LVCOLUMN
	Dim col   As Long
	'
	For col=0 To cols-1
		SendMessage rr, LVM_INSERTCOLUMN, 0, @tlvc
	Next
End Sub

'actual sort routine
Function SortFuncBoth(E1 As FileStats, E2 As FileStats, ByVal Col As Integer, Direction As Integer) As Integer
	'Return a negative value if the first item should precede the second,
	'a positive value if the first item should follow the second,
	'Or zero if the two items are equivalent.
	'
	Dim As Integer Ret,IsF1,IsF2
	Dim As String N1,N2
	'
	'directory location
	IsF1 = E1.Att And 16
	IsF2 = E2.Att And 16
	If KeepDirTop Then
		'force to top
		If IsF1 Then
			If IsF2 Then
				Col = 0 'sort by name both are dir
			Else
				Return -1 'second is not folder
			EndIf
		Else
			If IsF2 Then
				Return 1 'second is a folder
			Else
				'neither one folder do normal
			EndIf
		EndIf
	Else
		If GroupDir Then
			'at least keep together
			If IsF1 Then
				If IsF2 Then
					Col = 0 'sort by name both are dir
				Else
					Return -1 * Direction 'second is not folder
				EndIf
			Else
				If IsF2 Then
					Return Direction 'second is a folder
				Else
					'neither one folder do normal
				EndIf
			EndIf
		EndIf
	EndIf
	'do checks
	'no worry about placement of folders
	Select Case Col
		Case 0
			'sort by name - names are never equal
			If LCase(E1.FName) < LCase(E2.FName) Then
				Return -1 * Direction
			Else
				Return Direction
			EndIf
		Case 1
			'sort by type
			If E1.Ext = E2.Ext Then
				'sort by name - names are never equal
				If LCase(E1.FName) < (E2.FName) Then
					Return -1 * Direction
				Else
					Return Direction
				EndIf
			EndIf
			If E1.Ext < E2.Ext Then
				Return -1 * Direction
			Else
				Return Direction
			EndIf
			'
		Case 2
			'sort by date
			'folders have no date so...??
			If E1.DT = E2.DT Then
				'sort by name - names are never equal
				If LCase(E1.FName) < (E2.FName) Then
					Return -1 * Direction
				Else
					Return Direction
				EndIf
			EndIf
			If E1.DT < E2.DT Then
				Return -1 * Direction
			Else
				Return Direction
			EndIf
		Case 3
			'sort by size
			'folders have no size so...??
			If E1.FSize = E2.FSize Then
				'sort by name - names are never equal
				If LCase(E1.FName) < (E2.FName) Then
					Return -1 * Direction
				Else
					Return Direction
				EndIf
			EndIf
			If E1.FSize < E2.FSize Then
				Return -1 * Direction
			Else
				Return Direction
			EndIf
		Case Else
			'sort by attrib
			'???????
	End Select
	Return 0
End Function

'this is the callback function for the ListView_SortItems
Function SortFuncTop(I1 As LPARAM, I2 As LPARAM, ByVal C As LPARAM) As Integer
	Dim As Integer Ret, Direction
	'
	'sort direction
	Direction = -1
	If LastSortAccendTop Then Direction = 1
	'
	Ret = SortFuncBoth(FTPL(I1), FTPL(I2), C, Direction)
	Return Ret
End Function

'this is the callback function for the ListView_SortItems
Function SortFuncBottom(I1 As LPARAM, I2 As LPARAM, ByVal C As LPARAM) As Integer
	Dim As Integer Ret, Direction
	'
	'sort direction
	Direction = -1
	If LastSortAccendBottom Then Direction = 1
	'
	Ret = SortFuncBoth(FTPR(I1), FTPR(I2), C, Direction)
	Return Ret
End Function

Sub SelAll(LV As HWND)
	Dim As Integer TotF,x
	Dim As UINT ItemFlags
	'
	TotF = SendMessage(LV,LVM_GETITEMCOUNT,0,0) 'ListView_GetItemCount(From)
	'
	For x = 0 To TotF-1
		ItemFlags = ListView_GetItemState(LV, x, LVNI_SELECTED)
		If ItemFlags = 0 Then
			LV_SetSelState(LV,x,TRUE)
		EndIf
	Next
	ListView_RedrawItems(LV,0,TotF-1) 'need?
End Sub

Sub InvSel(L As HWND)
	Dim As Integer TotF,x
	Dim As UINT ItemFlags
	'
	TotF = SendMessage(L,LVM_GETITEMCOUNT,0,0) 'ListView_GetItemCount(From)
	'
	For x = 0 To TotF-1
		ItemFlags = ListView_GetItemState(L, x, LVNI_SELECTED)
		If ItemFlags = 0 Then
			LV_SetSelState(L,x,TRUE)
		Else
			LV_SetSelState(L,x,FALSE)
		EndIf
	Next
	ListView_RedrawItems(L,0,TotF-1)
End Sub

Sub ViewOpenFile(LV As HWND)
	Dim As Integer Sel
	'
	Sel = SendMessage(LV,LVM_GETSELECTEDCOUNT,0,0)
	If Sel = 0 Then Exit Sub
	'
	'at least something was selected
	Dim As String FName,AsscPrg,LookP',ECheck
	Dim As Integer L,x
	Dim As HINSTANCE Ret
	Dim As FileStats FStats
	'set current path
	If LV = FTop(TopLVType) Then
		If TopZip.ZipPtr <> NULL Then
			'FindPathInTree DTop
			ViewOpenFile_Zip TopZip
			Exit Sub
		EndIf
		LookP = wtTop
	Else
		If BottomZip.ZipPtr <> NULL Then
			'FindPathInTree DBottom
			ViewOpenFile_Zip BottomZip
			Exit Sub
		EndIf
		LookP = wtBottom
		If Connected Then LookP = FTPTemp+Path_Delim
	EndIf
	'
	L = ListView_GetNextItem(LV, -1, LVNI_SELECTED)
	While L > -1
		FStats = SelFileInfo(LV,L)
		FName = FStats.FName
		If FName = "" Then Exit While 'no files
		'check single selected on a folder or zip file
		If Sel = 1 And ((FStats.Att And 16) = 16 Or LCase(Right(FName,4)) = ".zip") Then
			'user select...menu open/view
			'user doubleclick/enter on this one
			If LV = FTop(TopLVType) Then
				If LCase(Right(FName,4)) = ".zip" Then
					OpenZipFile LookP+FName, TRUE
				Else
					'sorted by filename accending
					LastSortColTop = 0
					LastSortAccendTop = TRUE
					wtTop += FName+Path_Delim
				EndIf
			Else
				If LCase(Right(FName,4)) = ".zip" Then
					OpenZipFile LookP+FName, FALSE
				Else
					'sorted by filename accending
					LastSortColBottom = 0
					LastSortAccendBottom = TRUE
					If Connected = FALSE Then
						wtBottom += FName+Path_Delim
					Else
						ChangeFTPDir FName 'calls refreshFTP
					EndIf
				EndIf
			EndIf
			Exit Sub
		EndIf
		'multiple selects -- ignore dir and zip files
		If (FStats.Att And 16) = 0 Or LCase(Right(FName,4)) <> ".zip" Then
			If LCase(Right(FName,4)) = ".exe" Or LCase(Right(FName,4)) = ".com" Then
				If Not(Connected = TRUE And LV = FBottom(BotLVType)) Then Chain LookP+FName
			Else
				If Connected = TRUE And LV = FBottom(BotLVType) Then
					ftp_getfile(@ftp, FName, LookP+FName )
					ftp.LastFTP = Timer
					'DebugFile __FUNCTION__,"Downloaded to:"+LookP+FName
				EndIf
				AsscPrg = Space(MAX_PATH + 1)
				Ret = FindExecutable(StrPtr(FName),StrPtr(LookP),StrPtr(AsscPrg))
				Select Case CInt(Ret)
					Case 0
						MessageBox(0,"The system is out of memory or resources.","Error",MB_OK Or MB_ICONERROR)
						'
					Case ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND
						If Connected = TRUE And LV = FBottom(BotLVType) Then
							MessageBox(0,"File not downloaded.","Error",MB_OK Or MB_ICONERROR)
						Else
							MessageBox(0,"Programming error","Error",MB_OK Or MB_ICONERROR)
						EndIf
						'
					Case 31
						MessageBox(0,"No associated program for file '"+FName+"'.","Error",MB_OK)
						'
					Case Is > 32
						'has extra spaces at end
						AsscPrg = Trim(AsscPrg)
						'command options for this program?
						'ECheck = "Start "+Chr(34)+AsscPrg+Chr(34)+ " /?"
						'Shell(ECheck)
						'Sleep
						FName = "Start "+Chr(34)+AsscPrg+ Chr(34,32,34)+LookP+FName+ Chr(34)
						x = Shell(FName)
					Case Else
						MessageBox(0,"Unknown Error","Error",MB_OK Or MB_ICONERROR)
						'
				End Select
			EndIf
		EndIf
		L = ListView_GetNextItem(LV, L, LVNI_SELECTED)
	Wend
End Sub

Sub RClick(TheWin As HWND, TheLV As HWND, PopX As Integer, PopY As Integer)
	Dim As HMENU hPop,hCreateNew
	Dim As Integer NumItemSel, MenuItemSel, TotNewItems, x
	Dim As String HoldPath, CheckIt
	'
	NumItemSel = SendMessage(TheLV,LVM_GETSELECTEDCOUNT,0,0)
	'a right click with nothing selected will select the "nearest" one
	'
	hCreateNew = CreatePopupMenu()
	If Connected = TRUE And TheLV = FBottom(BotLVType) Then
		'if connected this is all we can do
		AppendMenu( hCreateNew, MF_STRING, 12101, "Folder" )
	Else
		For MenuItemSel = 1 To UBound(NewFMenu)
			AppendMenu( hCreateNew, MF_STRING, 12100 + MenuItemSel, NewFMenu(MenuItemSel).MenuDisp)
		Next
	EndIf
	'
	hPop = CreatePopupMenu()
	'always can create new folder
	AppendMenu( hPop, MF_STRING Or MF_POPUP, hCreateNew, "New" )
	'any files displayed?
	If LVGetValue(TheLV,0) <> "" Then
		AppendMenu( hPop, MF_SEPARATOR, 0, 0 )
		AppendMenu( hPop, MF_STRING, 12001, "Open" )
		AppendMenu( hPop, MF_SEPARATOR, 0, 0 )
		AppendMenu( hPop, MF_STRING, 12002, "Copy" )
		'no moving to/from zip
		If TopZip.ZipPtr = NULL And BottomZip.ZipPtr = NULL Then AppendMenu( hPop, MF_STRING, 12004, "Move" )
		AppendMenu( hPop, MF_STRING, 12003, "Delete" )
		If Not(Connected = TRUE And TheLV = FBottom(BotLVType)) Then
			'no recycle bin on FTP server
			'no recycle for zip files
			If TheLV <> FTop(3) And TheLV <> FBottom(3) Then AppendMenu( hPop, MF_STRING, 12008, "Send to Trash" )
			AppendMenu( hPop, MF_SEPARATOR, 0, 0 )
			'can not create zip file on FTP server
			AppendMenu( hPop, MF_STRING, 12010, "Copy to new zip file" )
			'AppendMenu( hPop, MF_STRING, 12011, "Move to new zip file" )
		EndIf
		AppendMenu( hPop, MF_SEPARATOR, 0, 0 )
		AppendMenu( hPop, MF_STRING, 12005, "Select All" )
		AppendMenu( hPop, MF_STRING, 12006, "Invert Selection" )
		AppendMenu( hPop, MF_SEPARATOR, 0, 0 )
		AppendMenu( hPop, MF_STRING, 12009, "Properties" )
		If NumItemSel = 1 Then AppendMenu( hPop, MF_STRING, 12007, "Rename" )
	EndIf
	'
	' Or TPM_HORPOSANIMATION, _
	MenuItemSel = TrackPopupMenu( hPop, _
	TPM_LEFTALIGN Or _
	TPM_RETURNCMD Or _
	TPM_TOPALIGN,_
	PopX, _
	PopY, _
	0, _
	TheWin, _
	0 )
	'
	If MenuItemSel > 12100 Then
		MenuItemSel -= 12100
		HandlePopUpMenu TheLV, MenuItemSel
		If strShare = "CF" Then MenuItemSel = 12007 'Rename!
	EndIf
	Select Case MenuItemSel
		Case 12001
			'open
			If TheLV = FTop(TopLVType) Then
				HoldPath = wtTop 'check for change dir
				ViewOpenFile FTop(TopLVType)
				'path change
				If HoldPath <> wtTop Then FindPathInTree DTop
			Else
				If Connected = FALSE Then
					HoldPath = wtBottom 'check for change dir
					ViewOpenFile FBottom(BotLVType)
					'path change
					If HoldPath <> wtBottom Then FindPathInTree DBottom
				Else
					ViewOpenFile FBottom(BotLVType)
				EndIf
			EndIf
			'
		Case 12002
			'copy
			KillTimer(TheWin,0)
			If TheLV = FTop(TopLVType) Then
				DoFileComm FTop(TopLVType),FBottom(BotLVType),"copied"
			Else
				DoFileComm FBottom(BotLVType),FTop(TopLVType),"copied"
			EndIf
			SetTimer(TheWin,0,TimerDelay,0)
			'
		Case 12003
			'del
			KillTimer(TheWin,0)
			DoFileComm TheLV,TheLV,"deleted"
			SetTimer(TheWin,0,TimerDelay,0)
			'
		Case 12004
			'move
			KillTimer(TheWin,0)
			'no moving from zip (allow moving from zip to zip)
			If TheLV = FTop(TopLVType) Then
				DoFileComm FTop(TopLVType),FBottom(BotLVType),"moved"
			Else
				DoFileComm FBottom(BotLVType),FTop(TopLVType),"moved"
			EndIf
			SetTimer(TheWin,0,TimerDelay,0)
			'
		Case 12005
			'select all
			SelAll TheLV
			'
		Case 12006
			'inv sel
			InvSel TheLV
			'
		Case 12007
			'rename -- only available if ONE item selected
			NumItemSel = ListView_GetNextItem(TheLV, -1, LVNI_SELECTED)
			SendMessage(TheLV,LVM_EDITLABEL ,NumItemSel,0)
			'
		Case 12008
			'will not appear if click on bottom LV and connected
			'my recycle bin
			KillTimer(TheWin,0)
			Recycle TheLV
			SetTimer(TheWin,0,TimerDelay,0)
			'
		Case 12009
			'properties
			Dim As FileStats FStats
			strShare = "Bottom"
			If TheLV = FTop(TopLVType) Then strShare = "Top"
			'Print "looking at: ";strShare
			'Print "Items selected count:";NumItemSel
			If NumItemSel = 1 Then
				MenuItemSel = ListView_GetNextItem(TheLV, -1, LVNI_SELECTED)
				FStats = SelFileInfo(TheLV,MenuItemSel)
				'Print FStats.Att
				If (FStats.Att And 16) = 16 Then
					'open prop dlg for one folder selected
					DialogBoxParam(hInstance, Cast(ZString Ptr,IDD_DLG1DirProp), NULL, @Prop1DirDlgProc, NULL)
				Else
					'open file prop dlg
					DialogBoxParam(hInstance, Cast(ZString Ptr,IDD_DLGFileProp), NULL, @PropDlgProc, NULL)
				EndIf
			Else
				x = 0 'count of folders
				MenuItemSel = ListView_GetNextItem(TheLV, -1, LVNI_SELECTED)
				While MenuItemSel > -1
					FStats = SelFileInfo(TheLV,MenuItemSel)
					If (FStats.Att And 16) = 16 Then x += 1
					MenuItemSel = ListView_GetNextItem(TheLV, MenuItemSel, LVNI_SELECTED)
				Wend
				If x Then
					'open prop dlg for multiple selection with one (or more) being a folder
					DialogBoxParam(hInstance, Cast(ZString Ptr,IDD_DLGMixProp), NULL, @PropMixDlgProc, NULL)
				Else
					'open file prop dlg
					DialogBoxParam(hInstance, Cast(ZString Ptr,IDD_DLGFileProp), NULL, @PropDlgProc, NULL)
				EndIf
			EndIf
			'
		Case 12010
			'copy to new zip file
			CreateNewZip TheLV, FALSE
			'
		Case 12011
			'move to new zip file
			CreateNewZip TheLV, TRUE
			'
	End Select
	DestroyMenu(hCreateNew)
	DestroyMenu(hPop)
End Sub

Function AddFileStats(Info As FileStats) As String
	Dim As Double Calc
	Dim As String TStr
	Dim As Integer BFactor
	'
	'"Test.txt|Txt|12/19/2011 12:15PM|1.5 KB|DAHSR"
	If Info.FName = "" Then Return "||Folder is empty.||"
	With Info
		TStr = .FName+"|"+.Ext+"|"
		TStr += Format(.DT, "mm/dd/yyyy hh:mm:ss AM/PM")+"|"
		If (.Att And fbDirectory) = 0 Then
			'is file
			TStr += ToKB(.FSize) + "|"
		Else
			'is folder
			TStr += "|" 'no size
		EndIf
		If (.Att And FILE_ATTRIBUTE_HIDDEN) Then
			TStr += "H"
		EndIf
		If (.Att And FILE_ATTRIBUTE_SYSTEM) Then
			TStr += "S"
		EndIf
		If (.Att And FILE_ATTRIBUTE_READONLY) Then
			TStr += "R"
		EndIf
	End With
	Return TStr
End Function

Function MyDir(pathspec As String="") As FileStats
	Dim Collect As FileStats
	Static hFind As handle
	'Static OrigSpec As String
	Dim As WIN32_FIND_DATA ffd
	'
	If Len(pathspec) Then
		FindClose( hFind )
		hFind = NULL
	EndIf
	'
	If hFind Then
		'Get next file
		If FindNextFile( hFind, @ffd ) = 0 Then
			Collect.FName = ""
			FindClose( hFind )
			hFind = NULL
		EndIf
	Else
		'new query
		hFind = FindFirstFile( Strptr(pathspec), @ffd )
		'
		If hFind = INVALID_HANDLE_VALUE Then
			Collect.FName = ""
			hFind = NULL
			Print "Invalid handle"
		EndIf
	EndIf
	If hFind Then
		'transfer to my types
		Collect.FName = ffd.cFileName
		Collect.Att = ffd.dwFileAttributes
		Collect.FName83 = ffd.cAlternateFileName
		Collect.DT = FT2Dbl(ffd.ftLastWriteTime)
		Collect.LastAccess = FT2Dbl(ffd.ftLastAccessTime)
		Collect.Create = FT2Dbl(ffd.ftCreationTime)
		Collect.FSize = (ffd.nFileSizeHigh * MAXDWORD) + ffd.nFileSizeLow
	EndIf
	Return Collect
End Function

Sub GetFileListTop
	If TopZip.ZipPtr = NULL Then
		Dim As Integer DCount,x
		ReDim FTPL(0) As FileStats
		Dim RetStat As FileStats
		'
		RetStat = MyDir(wtTop+"*")
		While RetStat.FName <> ""
			'OS file hack
			If LCase(RetStat.FName) = "pagefile.sys" Then RetStat.FName = "."
			If LCase(RetStat.FName) = "hiberfil.sys" Then RetStat.FName = "."
			If (RetStat.FName <> ".") And (RetStat.FName <> "..") And (Left(RetStat.FName,4) <> "$REC") And (RetStat.FName <> "System Volume Information") And (wtTop+RetStat.FName <> MyRecycle) Then
				ReDim Preserve FTPL(DCount) As FileStats
				FTPL(DCount).FName = RetStat.FName
				FTPL(DCount).Att = RetStat.Att
				FTPL(DCount).DT = RetStat.DT
				FTPL(DCount).LastAccess = RetStat.LastAccess
				FTPL(DCount).Create = RetStat.Create
				FTPL(DCount).FName83 = RetStat.FName83
				If (RetStat.Att And 16) = 16 Then
					FTPL(DCount).Ext = "File Folder"
				Else
					FTPL(DCount).FSize = RetStat.FSize
					x = InStrRev(RetStat.FName,".")
					If x Then
						FTPL(DCount).Ext = Mid(RetStat.FName,x+1) 'has extention
					Else
						FTPL(DCount).Ext = "FILE"
					EndIf
				EndIf
				DCount += 1
			EndIf
			RetStat = MyDir()
		Wend
	EndIf
	'
	RefreshTopLV
End Sub

Sub GetFileListBottom
	If BottomZip.ZipPtr = NULL Then
		Dim As Integer DCount,x
		ReDim FTPR(0) As FileStats
		Dim RetStat As FileStats
		'
		RetStat = MyDir(wtBottom + "*")
		While RetStat.FName <> ""
			'OS file hack
			If LCase(RetStat.FName) = "pagefile.sys" Then RetStat.FName = "."
			If LCase(RetStat.FName) = "hiberfil.sys" Then RetStat.FName = "."
			If (RetStat.FName <> ".") And (RetStat.FName <> "..") And (Left(RetStat.FName,4) <> "$REC") And (RetStat.FName <> "System Volume Information") And (wtTop+RetStat.FName <> MyRecycle) Then
				ReDim Preserve FTPR(DCount) As FileStats
				FTPR(DCount).FName = RetStat.FName
				FTPR(DCount).Att = RetStat.Att
				FTPR(DCount).DT = RetStat.DT
				FTPR(DCount).LastAccess = RetStat.LastAccess
				FTPR(DCount).Create = RetStat.Create
				FTPR(DCount).FName83 = RetStat.FName83
				If (RetStat.Att And 16) = 16 Then
					FTPR(DCount).Ext = "File Folder"
				Else
					FTPR(DCount).FSize = RetStat.FSize
					x = InStrRev(RetStat.FName,".")
					If x Then
						FTPR(DCount).Ext = Mid(RetStat.FName,x+1) 'has extention
					Else
						FTPR(DCount).Ext = "FILE"
					EndIf
				EndIf
				DCount += 1 'dir
			EndIf
			RetStat = MyDir()
		Wend
	EndIf
	'
	RefreshBottomLV
End Sub

Sub HandlePopUpMenu(WhichLV As HWND,Selection As Integer)
	Dim As String HoldPath,CheckIt
	Dim As Integer x
	Dim dirInfo As WIN32_FIND_DATA
	Dim hFind As HINTERNET
	'
	strShare = "" 'set to "CF" on creation of new file
	'
	HoldPath = wtBottom
	If WhichLV = FTop(TopLVType) Then HoldPath = wtTop
	'
	'create new folder ....
	If Selection = 1 Then
		CheckIt = "New Folder"
		x = 1
		Do
			If Connected = TRUE And WhichLV = FBottom(BotLVType) Then
				'ftp create dir???
				dirInfo.cFileName = ""
				hFind = FtpFindFirstFile( ftp.hconn, CheckIt, @dirInfo, NULL, 0 )
				InternetCloseHandle(hFind)
				If dirInfo.cFileName = "" Then
					FtpCreateDirectory(ftp.hconn,CheckIt)
					Exit Do
				EndIf
			Else
				'returns 0 on success
				If MkDir(HoldPath+CheckIt) = 0 Then Exit Do
			EndIf
			x += 1
			CheckIt = "New Folder ("+Str(x)+")"
		Loop
	Else
		'create new file
		CheckIt = "New "+NewFMenu(Selection).MenuDisp+NewFMenu(Selection).Ext
		x = 1
		Do
			'returns 0 on file not there
			If FileExists(HoldPath+CheckIt) = 0 Then Exit Do
			x += 1
			CheckIt = "New "+NewFMenu(Selection).MenuDisp+" ("+Str(x)+")"+NewFMenu(Selection).Ext
		Loop
		If NewFMenu(Selection).BlankFile = "" Then
			x = FreeFile
			Open HoldPath+CheckIt For Output As #x
			'write ZERO byte file!!!
			Close #x
		Else
			'need to set file datetime to now!
			FileCopy NewFMenu(Selection).BlankFile, HoldPath+CheckIt
			Dim As UByte Ptr FD
			Dim As Integer LF
			x = FreeFile
			Open NewFMenu(Selection).BlankFile For Binary Access Read As #x
			LF = Lof(x)
			FD = Callocate(LF-1)
			Get #x,,FD[0],LF
			Close #x
			x = FreeFile
			Open "test -- "+HoldPath+CheckIt For Binary Access Write As #x
			Put #x,,FD[0],LF
			Close #x
			DeAllocate(FD)
		EndIf
	EndIf
	If WhichLV = FTop(TopLVType) Then
		GetFileListTop
	Else
		If Connected = TRUE Then
			RefreshFTPFiles @ftp
		Else
			GetFileListBottom
		EndIf
	EndIf
	x=lvFindItem(WhichLV,CheckIt) 'find
	LV_SetSelState(WhichLV,x,TRUE) 'select
	strShare = "CF" 'flag to catch esc on rename
End Sub

Sub RefreshTopLV
	'
	If TopZip.ZipPtr <> NULL Then
		RefreshTopZip
	Else
		SendMessage(FTop(TopLVType),LVM_DELETEALLITEMS,0,0)
		For x As Integer = 0 To UBound(FTPL)
			If TopLVType Then
				'1 or 2
				IconicLVAddItem FTop(TopLVType), FTPL(x)
			Else
				LVAddItem FTop(0), AddFileStats(FTPL(x))
			EndIf
		Next
		If TopLVType = 0 Then
			ListView_SortItems(FTop(0),@SortFuncTop,0)
			LastSortColTop = 0
		EndIf
		SetWindowText(PDispTop,wtTop)
	EndIf
End Sub

Sub RefreshBottomLV
	'
	If BottomZip.ZipPtr <> NULL Then
		RefreshBottomZip
	Else
		SendMessage(FBottom(BotLVType),LVM_DELETEALLITEMS,0,0)
		For x As Integer = 0 To UBound(FTPR)
			If BotLVType Then
				'1 or 2
				IconicLVAddItem FBottom(BotLVType), FTPR(x)
			Else
				LVAddItem FBottom(0), AddFileStats(FTPR(x))
			EndIf
		Next
		If BotLVType = 0 Then
			ListView_SortItems(FBottom(0),@SortFuncBottom,0)
			LastSortColBottom = 0
		EndIf
		SetWindowText(PDispBottom,wtBottom)
	EndIf
End Sub

'Function LV2ArrayInx(LV As HWND,Sel As Integer) As Integer
'	Dim lvi As LV_ITEM
'	lvi.mask = LVIF_PARAM
'	lvi.iSubItem = 0
'	lvi.iItem = Sel
'	SendMessage(LV,LVM_GETITEM,0,@lvi)
'	Return lvi.lParam' = x 'array index
'End Function

/'Sub ResetLParam(ByRef hList As Long)
	Dim i As Long
	Dim recs As Long
	Dim lvi As LV_ITEM
	Dim x As Long

	lvi.mask = LVIF_PARAM
	lvi.iSubItem = 0
	recs = sendmessage(hlist,lvm_GetItemCount,0,0)
	For i = 0 To recs - 1
		lvi.iItem = i
		x = SendMessage(hList,LVM_GetItem,0,@lvi)
		lvi.lParam = lvi.iItem
		x = SendMessage(hList,LVM_SetItem,0,@lvi)
	Next
End Sub'/