 /***************************************************************************************
 *This program is free software; you can redistribute it and/or modify					*
 * it under the terms of the GNU General Public License as published by					*
 * the Free Software Foundation; either version 2 of the License, or					*
 * (at your option) any later version.													*
 *																						*
 * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html						*
 *																						*
 *																						*	
 ****************************************************************************************
 * Authors:																			  *
 *          Marc Dukette                                                              *
 **************************************************************************************/

#include "stdafx.h"
#include "GetFileName.h"
#include "resource.h"
#include <commctrl.h>
#include <wininet.h>
HANDLE hmod;
unsigned int vkCtrl;
typedef struct _entry{
	TCHAR Name[200];
	TCHAR Type[20];
	int	  Size;
	TCHAR URL[200];
}entry;

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	hmod=hModule;
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }
    return TRUE;
}
TCHAR CurrentPath[200]=_T("\\");
//TCHAR PreviousPath[200]=_T("\\");
TCHAR FileName[200]=_T("");
TCHAR g_Filter[200];

HWND g_Parent;
 char data[100000];
 TCHAR up[100000];
 TCHAR dataw[100000];
 TCHAR History[100][200];
 int HistoryPos=0;
int SortCol=1;
int Dir=1;
int g_webView=0;
TCHAR MultiBuff[100000];
// Global Variables:
bool AllowMulti=false;
bool bEnd;
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle
HWND hwndList;
// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);
int SelectedRow=-1;
HWND hwndMain;
HWND hwndURL;
HWND hwndBrowse;
HWND hwndOK;
HWND hwndFolder;

int NumFiles=0;
long oldproc;
bool KeyDown=false;
LRESULT CALLBACK ListProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	entry *pHouse1 = (entry *)lParam1;
	entry *pHouse2 = (entry *)lParam2;
	LPTSTR lpStr1, lpStr2;
	int iResult;
	
	if (pHouse1 && pHouse2)
	{
		switch( lParamSort)
		{
		case 0:     // sort by Address
			lpStr1 = pHouse1->Name;
			lpStr2 = pHouse2->Name;
			if (!wcscmp(pHouse1->Type,_T(""))) return -1*Dir;
			if (!wcscmp(pHouse2->Type,_T(""))) return 1*Dir;
			if (!wcscmp(pHouse1->Type,_T("Dir"))&&(wcscmp(pHouse2->Type,_T("Dir")))) return -1*Dir;
			if (!wcscmp(pHouse2->Type,_T("Dir"))&&(wcscmp(pHouse1->Type,_T("Dir")))) return 1*Dir;
			iResult = wcsicmp(lpStr1, lpStr2);
			break;
			
		case 1:     // sort by city

			lpStr1 = pHouse1->Type;
			lpStr2 = pHouse2->Type;
			if (!wcscmp(pHouse1->Type,_T(""))) return -1*Dir;
			if (!wcscmp(pHouse2->Type,_T(""))) return 1*Dir;
			if (!wcscmp(pHouse1->Type,_T("Dir"))&&(wcscmp(pHouse2->Type,_T("Dir")))) return -1*Dir;
			if (!wcscmp(pHouse2->Type,_T("Dir"))&&(wcscmp(pHouse1->Type,_T("Dir")))) return 1*Dir;
			iResult = wcsicmp(lpStr1, lpStr2);
			break;
			
		case 2:     // sort by price
			if (!g_webView)
				iResult = pHouse1->Size - pHouse2->Size;
			else
			{
				lpStr1 = pHouse1->URL;
				lpStr2 = pHouse2->URL;
				if (!wcscmp(pHouse1->Type,_T(""))) return -1*Dir;
				if (!wcscmp(pHouse2->Type,_T(""))) return 1*Dir;
				if (!wcscmp(pHouse1->Type,_T("Dir"))&&(wcscmp(pHouse2->Type,_T("Dir")))) return -1*Dir;
				if (!wcscmp(pHouse2->Type,_T("Dir"))&&(wcscmp(pHouse1->Type,_T("Dir")))) return 1*Dir;
				iResult = wcsicmp(lpStr1, lpStr2);
			}
			break;
			
		default:
			iResult = 0;
			break;
			
		}
		
	}
	return(iResult*Dir);
}


ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= NULL; //LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OPENFILE));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(GRAY_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

void GetFileExt(LPTSTR File,LPTSTR EXT)
{
     LPTSTR temp=File;
     int dcnt=0;
	int done=0;
	int found=0;
	int n=wcslen(File);
	while (!done&&(n>=0))
	{
		if (File[n]==L'.')
		{
			found=1;
			done=1;
			continue;
		}
		if (File[n]==L'/')
		{
			done=1;
			continue;
		}
		n--;
	}
	if (found&&(n>0))
	{
		wcscpy(EXT,File+n+1);
	}
	else
	{
		wcscpy(EXT,_T(""));
	}

}
void GetBaseURL(LPTSTR URL,LPTSTR base)
{
	int n=wcslen(URL);
	int done=0;
	int found=0;

	while (!done&&(n>=0))
	{
		if (URL[n]==L'.')
			found=1;
		if (URL[n]==L'/')
		{
			done=1;
			continue;
		}
		n--;
	}
	if (found&&(n>6))
	{
		wcsncpy(base,URL,n);
		base[n]=0;
	}
	else
	{
		wcscpy(base,URL);
	}
}
void ParseURL(HWND hWnd, LPTSTR URL)
{
     TCHAR temp[500];
	TCHAR baseURL[200];
     TCHAR * next=up;
     unsigned long n;
	int tot=0;
     HINTERNET hSess=InternetOpen(_T("HTTPList"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
	 if (!hSess)
	 {
			MessageBox(hwndList,_T("Could not open an Internet connection"),_T(""),MB_OK);
			return;
	 }
     HINTERNET hFile=InternetOpenUrl(hSess,URL,NULL,NULL,INTERNET_FLAG_DONT_CACHE,NULL);
	 if (!hFile)
	 {
			MessageBox(hwndList,_T("Could not open URL"),_T(""),MB_OK);
			return;
	 }
	 GetBaseURL(URL,baseURL);
	wcscpy(History[HistoryPos],URL);
	if (HistoryPos<99) HistoryPos++;

	LVITEM lv;
	for (n=0;n<ListView_GetItemCount(hwndList);n++)
	{
		lv.iItem=n;
		lv.mask=LVIF_PARAM;
		ListView_GetItem(hwndList,&lv);
		if (lv.lParam) delete (entry*)lv.lParam;
	}	

	
	int lastDir=0; 
	lv.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM ;
	lv.pszText=_T("");
	lv.cchTextMax=1;
	lv.iSubItem=0;
	ListView_DeleteAllItems(hWnd);
 			lv.iImage=1;

			
			entry* e=new entry;
			lv.lParam=(LPARAM)e;
			wcscpy(e->Name,_T(".."));
			wcscpy(e->Type,_T(""));
		lv.iItem=lastDir;
		ListView_InsertItem(hWnd,&lv);
		ListView_SetItemText(hWnd,lastDir,0,_T(".."));
			lastDir++;
			
		do{
			n=0;
			InternetReadFile(hFile,(void*)(data+tot),100000-tot,&n);
			tot+=n;
		}while(n&&(tot<100000));
		InternetCloseHandle(hSess);
		InternetCloseHandle(hFile);
  	 MultiByteToWideChar(CP_ACP, 0, data, tot, dataw, tot);
     wcscpy(up,dataw);
     wcsupr(up);
     while((next=wcsstr(next,_T("<A ")))!=0)
     {
          TCHAR* end;
          TCHAR one[500];
		  TCHAR name[100];
		  TCHAR fname[100];
		  next=wcsstr(next,_T("HREF="));
		  if (!next) break;
          next+=5;

          if (wcsstr(next,_T("\""))==next)
          {
               next++;
               end=wcsstr(next,_T("\""));
          }
          else if (wcsstr(next,_T("\'"))==next)
          {
               next++;
               end=wcsstr(next,_T("\'"));
          }
          else
          {
               end=wcsstr(next,_T(">"));
          }
		  if (!end) continue;
		  while(*(end-1)==_T(' ')||(*(end-1)==_T('\r'))||(*(end-1)==_T('\n'))||(*(end-1)==_T('\t')))
		  {
			   end--;
		   }
          //*end=0;
          *(dataw+(end-up))=0;
          wcscpy(one,_T(""));
          if (!wcsstr(dataw+(next-up),_T("://")))
          {
			  if (*(dataw+(next-up))!=L'/')
			   {
	               wcscpy(one,baseURL);
				   if (baseURL[wcslen(baseURL)-1]!=L'/') wcscat(one,_T("/"));
			   }
			   else
			   {
				   TCHAR* root=baseURL+8;
				   root=wcsstr(root,_T("/"));
				   if (!root)
				   {
					   wcscpy(one,baseURL);
				   }
				   else
				   {
						wcsncpy(one,baseURL,root-baseURL);
						one[root-baseURL]=0;
				   }
			   }
          }
          wcscat(one,dataw+(next-up));
		
		  
		TCHAR* nstart=one;
		TCHAR* nend=one;

		while ((nstart=wcsstr(nstart,_T("/")))!=NULL)
		{
			nstart++;
			nend=nstart;
		}
		wcscpy(fname,nend);
		  
		wcscpy(temp,one);
		nstart=wcsstr(next,_T(">"));
		nstart++;
		nend=wcsstr(nstart,_T("<"));
		wcsncpy(name,nstart,(nend-nstart));
		name[(nend-nstart)]=0;
		
		
		
		TCHAR ext[100]; 
		if (!wcscmp(name,_T(""))) wcscpy(name,dataw+(next-up));
         GetFileExt(one,ext);
		  wcsupr(ext);
          if (!wcscmp(ext,_T(""))||(wcsstr(_T("ASP,HTM,HTML,PHP"),ext)))
			{

			lv.iImage=1;
			lv.iItem=lastDir;
			entry* e=new entry;
			lv.lParam=(LPARAM)e;
			wcscpy(e->Name,name);
			wcscpy(e->URL,one);
			wcscpy(e->Type,_T("Dir"));
			ListView_InsertItem(hWnd,&lv);
			ListView_SetItemText(hWnd,lastDir,0,name);
			ListView_SetItemText(hWnd,lastDir,1,_T("Dir"));
			ListView_SetItemText(hWnd,lastDir,2,one);
					
		
			lastDir++;
		  }
          if (wcscmp(ext,_T(""))&&(wcsstr(g_Filter,ext)))
		  {
				lv.iItem=lastDir;
				entry* e=new entry;
				lv.lParam=(LPARAM)e;
				wcscpy(e->Name,fname);
				wcscpy(e->URL,one);
				wcscpy(e->Type,ext);
				if (!wcscmp(ext,_T("PDL")))
					lv.iImage=2;
				else if (!wcscmp(ext,_T("MP3")))
					lv.iImage=3;				
				else if (!wcscmp(ext,_T("OGG")))
					lv.iImage=3;				
				else
					lv.iImage=0;
				ListView_InsertItem(hWnd,&lv);
				ListView_SetItemText(hWnd,lastDir,0,fname);
	
				ListView_SetItemText(hWnd,lastDir,1,ext);
				ListView_SetItemText(hWnd,lastDir,2,one);
		  }
     }
  ListView_SortItems( hWnd,
		ListViewCompareProc,
		(LPARAM) SortCol);

}


void FillWindow(HWND hWnd, LPTSTR Path)
{
  WIN32_FIND_DATA FileData;   // Data structure describes the file found
  HANDLE hSearch;             // Search handle returned by FindFirstFile
  BOOL bFinished = FALSE;
 	int lastDir=0; 
	LVITEM lv;

	for (int n=0;n<ListView_GetItemCount(hwndList);n++)
	{
		lv.iItem=n;
		lv.mask=LVIF_PARAM;
		ListView_GetItem(hwndList,&lv);
		if (lv.lParam) delete (entry*)lv.lParam;
	}	
	
	lv.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM ;
	lv.pszText=_T("");
	lv.cchTextMax=1;
	lv.iSubItem=0;
	ListView_DeleteAllItems(hWnd);
 			lv.iImage=1;
			entry* e=new entry;
			lv.lParam=(LPARAM)e;
			wcscpy(e->Name,_T(".."));
			wcscpy(e->Type,_T(""));
		lv.iItem=lastDir;
		ListView_InsertItem(hWnd,&lv);
		ListView_SetItemText(hWnd,lastDir,0,_T(".."));
		lastDir++;
 hSearch = FindFirstFile (Path, &FileData);
  if (hSearch == INVALID_HANDLE_VALUE)
  {
    return;
  }

 while (!bFinished)
  {

	TCHAR temp[100];
		if (FileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
	  {

			lv.iImage=1;
			lv.iItem=lastDir;
			entry* e=new entry;
			lv.lParam=(LPARAM)e;
			wcscpy(e->Name,FileData.cFileName);
			e->Size=0;
			wcscpy(e->Type,_T("Dir"));
			ListView_InsertItem(hWnd,&lv);
			ListView_SetItemText(hWnd,lastDir,0,FileData.cFileName);
			ListView_SetItemText(hWnd,lastDir,1,_T("Dir"));
		
		
		lastDir++;
	  }
		wcscpy(temp,FileData.cFileName);
//		LPTSTR ext=wcstok(temp,_T("."));
//	  ext=wcstok(NULL,_T("."));
		TCHAR ext[100]; 
         GetFileExt(temp,ext);
		  wcsupr(ext);
		if (wcscmp(ext,_T("")))
		{
		  wcsupr(ext);
		  if (wcsstr(g_Filter,ext))
		  {
				lv.iItem=lastDir;
				entry* e=new entry;
				lv.lParam=(LPARAM)e;
				wcscpy(e->Name,FileData.cFileName);
				e->Size=FileData.nFileSizeLow;
				wcscpy(e->Type,ext);
				if (!wcscmp(ext,_T("PDL")))
				{
					lv.iImage=2;
				}
				else if (!wcscmp(ext,_T("MP3")))
				{
					lv.iImage=3;
				}
				else if (!wcscmp(ext,_T("OGG")))
				{
					lv.iImage=3;
				}
				else
				{
					lv.iImage=0;
				}
				ListView_InsertItem(hWnd,&lv);
				ListView_SetItemText(hWnd,lastDir,0,FileData.cFileName);
	
				ListView_SetItemText(hWnd,lastDir,1,ext);
				_itow(FileData.nFileSizeLow/1000,temp,10);
				wcscat(temp,_T("kb"));
				ListView_SetItemText(hWnd,lastDir,2,temp);
		  }
		}

    if (!FindNextFile (hSearch, &FileData))
    {
      bFinished = TRUE;
    }
  }

  // Close the search handle.

  FindClose (hSearch);
  ListView_SortItems( hWnd,
		ListViewCompareProc,
		(LPARAM) SortCol);

}


#define REG_BASE			L"software\\PocketMVP"
#define OPT_URL			L"URLs"



void FillRecent()
{
	HKEY hKey = 0;
	DWORD dwSize = 0;
	DWORD dwType = 0;
	DWORD dwTmp;
	TCHAR temp[10];
	TCHAR value[200];
	int n=0;
	if ( ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, REG_BASE, 0, 0, &hKey ) )
	{
		dwTmp = 0;
		wcscpy(temp,OPT_URL);
		for (n=0;n<5;n++)
		{
			dwSize =sizeof(value);
			_itow(n,temp+4,10);
			temp[5]=0;
			if ( ERROR_SUCCESS == RegQueryValueEx( hKey, temp, 0, &dwType, ( LPBYTE )value, &dwSize ) )
			{
				if (wcscmp(value,_T(""))) SendMessage(hwndURL,CB_ADDSTRING,0,(LPARAM)value);
			}
		}
		RegCloseKey( hKey );
	}
}

void LoadSort()
{
	HKEY hKey = 0;
	DWORD dwSize = 0;
	DWORD dwType = 0;
	DWORD dwTmp;
	if ( ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, REG_BASE, 0, 0, &hKey ) )
	{
		dwTmp = 0;
		dwSize =sizeof(int);
		RegQueryValueEx( hKey, _T("SortCol"), 0, &dwType, ( LPBYTE )&SortCol, &dwSize );
		RegQueryValueEx( hKey, _T("SortDir"), 0, &dwType, ( LPBYTE )&Dir, &dwSize );
		RegCloseKey( hKey );
	}
}

void SaveSort()
{
	HKEY hKey = 0;
	DWORD dwDisp = 0;
	int n=0;
	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_LOCAL_MACHINE, REG_BASE, NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		RegSetValueEx( hKey, _T("SortCol"), 0, REG_DWORD, ( LPBYTE )&SortCol, sizeof(DWORD));
		RegSetValueEx( hKey, _T("SortDir"), 0, REG_DWORD, ( LPBYTE )&Dir, sizeof(DWORD));
		RegCloseKey( hKey );
	}

}
void SaveRecent()
{
	HKEY hKey = 0;
	DWORD dwDisp = 0;
	TCHAR temp[10];
	TCHAR value[200];
	int n=0;
	if ( ERROR_SUCCESS == RegCreateKeyEx( HKEY_LOCAL_MACHINE, REG_BASE, NULL, 0, 0,
		KEY_READ | KEY_WRITE | KEY_SET_VALUE,
		NULL, &hKey, &dwDisp ) )
	{
		wcscpy(temp,OPT_URL);
		for (n=0;n<5;n++)
		{
			_itow(n,temp+4,10);
			temp[5]=0;
			if (SendMessage(hwndURL,CB_GETLBTEXT,n,(WPARAM)value)!=CB_ERR)
				RegSetValueEx( hKey, temp, 0, REG_SZ, ( LPBYTE )value, sizeof(value));
		}
		RegCloseKey( hKey );
	}

}


//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd;
	TCHAR Temp[200];
	hInst = hInstance;		// Store instance handle in our global variable
	MyRegisterClass(hInstance, _T("OpenDialog"));

	hWnd=hwndMain = CreateWindow(_T("OpenDialog"), _T("Open File"), WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{	
		return FALSE;
	}
	RECT rc;
	GetClientRect(hWnd,&rc);
//	rc.bottom-=48;
	rc.bottom-=24;
	hwndOK=CreateWindow(_T("BUTTON"), _T("OK"), WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
		rc.right-80, rc.top+1, 64, 21, hWnd, NULL, hInstance, NULL);
	hwndFolder=CreateWindow(_T("BUTTON"), _T("All"), WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
		(rc.right-rc.left)/2-32, rc.top+1, 64, 21, hWnd, NULL, hInstance, NULL);
	HWND hwndCancel=CreateWindow(_T("BUTTON"), _T("Cancel"), WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
		rc.left+16, rc.top+1, 64, 21, hWnd, NULL, hInstance, NULL);
	rc.top+=23;
	if (g_webView)
	{
		hwndURL=CreateWindow(_T("COMBOBOX"), _T(""), WS_VISIBLE|WS_CHILD|WS_BORDER|CBS_AUTOHSCROLL|CBS_DROPDOWN,
			rc.left, rc.top+2, rc.right-64, 100, hWnd, NULL, hInstance, NULL);
		hwndBrowse=CreateWindow(_T("BUTTON"), _T("Browse"), WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
			rc.right-64, rc.top+1, 64, 21, hWnd, NULL, hInstance, NULL);
		rc.top+=23;
		rc.bottom-=23;
		SetWindowText(hwndURL,FileName);
		HistoryPos=0;
		FillRecent();
	}
		InitCommonControls();
	if (AllowMulti)
		hwndList=CreateWindow(_T("SysListView32"), _T(""), WS_VISIBLE|WS_CHILD|WS_VSCROLL|LVS_REPORT,
			rc.left, rc.top, rc.right, rc.bottom, hWnd, NULL, hInstance, NULL);
	else
		hwndList=CreateWindow(_T("SysListView32"), _T(""), WS_VISIBLE|WS_CHILD|WS_VSCROLL|LVS_REPORT|LVS_SINGLESEL,
			rc.left, rc.top, rc.right, rc.bottom, hWnd, NULL, hInstance, NULL);
	ListView_SetExtendedListViewStyle(hwndList,LVS_EX_FULLROWSELECT);
	HIMAGELIST hwndImage=ImageList_LoadBitmap((HINSTANCE)hmod,MAKEINTRESOURCE(IDB_BITMAP1),16,0,CLR_NONE);
	
	ListView_SetImageList(hwndList,hwndImage,LVSIL_SMALL);
	LVCOLUMN lv;
	lv.mask=LVCF_TEXT | LVCF_WIDTH ;
	lv.fmt=LVCFMT_LEFT ;
	lv.cx=120;
	lv.pszText=_T("File");
	lv.cchTextMax=5;
	ListView_InsertColumn(hwndList,0,&lv);
	lv.cx=40;
	lv.pszText=_T("Type");
	ListView_InsertColumn(hwndList,1,&lv);
	if (!g_webView)
	{
		lv.cx=80;
		lv.mask=LVCF_TEXT | LVCF_WIDTH | LVCF_FMT ;
		lv.fmt=LVCFMT_RIGHT ;
		lv.pszText=_T("Size");
	}
	else
	{
		lv.cx=500;
		lv.mask=LVCF_TEXT | LVCF_WIDTH | LVCF_FMT ;
		lv.fmt=LVCFMT_LEFT ;
		lv.pszText=_T("URL");
	}
	ListView_InsertColumn(hwndList,2,&lv);
	wcscpy(Temp,CurrentPath);
	wcscat(Temp,_T("*.*"));

	if (!g_webView) FillWindow(hwndList,Temp);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	oldproc=SetWindowLong(hwndList,GWL_WNDPROC,(long)ListProc);
	SetFocus(hwndList);
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)&&(IsWindow(hwndMain))) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	UnregisterClass(_T("OpenDialog"),hInstance);
	return NumFiles;
}

void PickItem()
{
	TCHAR Temp[200];
	LPTSTR current=MultiBuff;
	int n=0;
	wcscpy(FileName,_T(""));
	for (n=0;n<ListView_GetItemCount(hwndList);n++)
	{
		if (ListView_GetItemState(hwndList,n,LVIS_SELECTED))
		{
	
			ListView_GetItemText(hwndList,n,1,Temp,200);
			if (!wcscmp(Temp,_T("")))
			{
				if (!g_webView) 
				{
					wcscpy(Temp,_T(""));
					int n=wcslen(CurrentPath)-2;
					while(n>0&&(CurrentPath[n]!=_T('\\')))
					{
						n--;
					}
					if (n<0) n=0;
					CurrentPath[n+1]=0;
					wcscpy(Temp,CurrentPath);
					wcscat(Temp,_T("*.*"));
					FillWindow(hwndList,Temp);
				}
				else
				{
					if (HistoryPos>1)
					{
						HistoryPos-=2;
						SetWindowText(hwndURL,History[HistoryPos]);
						ParseURL(hwndList,History[HistoryPos]);
					}
				}
				return;
			}

			else if (wcsstr(Temp,_T("Dir"))!=NULL)
			{
				if (!g_webView) 
				{
					ListView_GetItemText(hwndList,n,0,Temp,200);
					wcscat(CurrentPath,Temp);
					wcscat(CurrentPath,_T("\\"));
					wcscpy(Temp,CurrentPath);
					wcscat(Temp,_T("*.*"));
					FillWindow(hwndList,Temp);
				}
				else 
				{
					ListView_GetItemText(hwndList,n,2,Temp,200);
					SetWindowText(hwndURL,Temp);
					ParseURL(hwndList,Temp);
				}
				return;
			}
			else
			{
				if (!g_webView)
				{
					ListView_GetItemText(hwndList,n,0,Temp,200);
					wcscpy(FileName,CurrentPath);
				}
				else
				{
					wcscpy(FileName,_T(""));
					ListView_GetItemText(hwndList,n,2,Temp,200);
				}
				wcscat(FileName,Temp);
				wcscpy(current,FileName);
				wcscat(current,_T("\r\n"));
				current+=(wcslen(FileName)+2);
				NumFiles++;
			}
		}
	}
	if (!NumFiles&&(g_webView))
	{
		GetWindowText(hwndURL,Temp,200);
		wcscpy(current,Temp);
		wcscpy(FileName,Temp);
		NumFiles=1;
	}
	wcscat(current,_T("\0\0"));
	EnableWindow(g_Parent,true);
	DestroyWindow(hwndMain);
}

void PickFolder()
{
	TCHAR Temp[200];
	LPTSTR current=MultiBuff;
	int n=0;
	wcscpy(FileName,_T(""));
	for (n=0;n<ListView_GetItemCount(hwndList);n++)
	{
		ListView_GetItemText(hwndList,n,1,Temp,200);
		if (!wcscmp(Temp,_T("")))
		{
		}

		else if (wcsstr(Temp,_T("Dir"))!=NULL)
		{
		}
		else
		{
			if (!g_webView)
			{
				ListView_GetItemText(hwndList,n,0,Temp,200);
				wcscpy(FileName,CurrentPath);
			}
			else
			{
				wcscpy(FileName,_T(""));
				ListView_GetItemText(hwndList,n,2,Temp,200);
			}
			wcscat(FileName,Temp);
			wcscpy(current,FileName);
			wcscat(current,_T("\r\n"));
			current+=(wcslen(FileName)+2);
			NumFiles++;
		}
	}
	if (NumFiles)
	{
		wcscat(current,_T("\0\0"));
		EnableWindow(g_Parent,true);
		DestroyWindow(hwndMain);
	}
}


LRESULT CALLBACK ListProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_KEYDOWN:
			if (wParam == vkCtrl)
			{
				keybd_event(VK_CONTROL,1,0,0);
				KeyDown=true;
			}			
			return 0;
			break;
		case WM_KEYUP:
			if (wParam == vkCtrl)
			{
				keybd_event(VK_CONTROL,1,KEYEVENTF_KEYUP,0);
				KeyDown=false;
			}
			return 0;
			break;
		default:
//#if _WIN32_WCE >= 300
#ifdef STRICT
			return CallWindowProc((WNDPROC)oldproc,hWnd,message,wParam,lParam);
#else
			return CallWindowProc((int (__cdecl *)(void))oldproc,hWnd,message,wParam,lParam);
#endif
			break;
	}
}
//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	int wmEvent;
	PAINTSTRUCT ps;

	switch (message) 
	{

		case WM_NOTIFY:
		{
			LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)lParam;
			if (lpnmlv->hdr.code==LVN_ITEMCHANGED &&(lpnmlv->iItem!=-1))
			{
				if (lpnmlv->uNewState==3)
					SelectedRow=lpnmlv->iItem;
			}
				 
			if (lpnmlv->hdr.code==NM_CLICK&&(lpnmlv->iItem!=-1)&&(ListView_GetSelectedCount(hwndList)<2)&&(!KeyDown))
			{
				SelectedRow=lpnmlv->iItem;
				PickItem();
			}

			if (lpnmlv->hdr.code==LVN_COLUMNCLICK)
			{
				if (SortCol!=lpnmlv->iSubItem)
				{
					Dir=1;
				}
				else
				{
					Dir*=-1;
				}
				SortCol=lpnmlv->iSubItem;
				ListView_SortItems( lpnmlv->hdr.hwndFrom,
					ListViewCompareProc,
					(LPARAM)(lpnmlv->iSubItem));
			}
			break;

		}
		break;
		case WM_COMMAND:
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmEvent)
			{
				case BN_CLICKED:
					if ((HWND)lParam==hwndOK)
					{
						PickItem();
					}
					else if ((HWND)lParam==hwndFolder)
					{
						PickFolder();
					}
					else if ((HWND)lParam==hwndBrowse)
					{
						TCHAR URL[200];
						GetWindowText(hwndURL,URL,200);
						if (wcscmp(URL,_T("")))
						{
							TCHAR value[200];
							if (wcsnicmp(URL,_T("http://"),7))
							{
								wcscpy(value,_T("http://"));
								wcscat(value,URL);
								SetWindowText(hwndURL,value);
								wcscpy(URL,value);
							}
							int cur=SendMessage(hwndURL,CB_GETCURSEL,0,0);
							if (cur>=0)
							{
								SendMessage(hwndURL,CB_GETLBTEXT,cur,(WPARAM)value);
								if (!wcscmp(value,URL)) SendMessage(hwndURL,CB_DELETESTRING,cur,0);
							}
							SendMessage(hwndURL,CB_INSERTSTRING,0,(WPARAM)URL);
							SendMessage(hwndURL,CB_SETCURSEL,0,0);
							SaveRecent();
							HistoryPos=0;
							ParseURL(hwndList,URL);

						}
					}
					else
					{
						LVITEM lv;
						for (int n=0;n<ListView_GetItemCount(hwndList);n++)
						{
							lv.iItem=n;
							lv.mask=LVIF_PARAM;
							ListView_GetItem(hwndList,&lv);
							if (lv.lParam) delete (entry*)lv.lParam;
						}	
						EnableWindow(g_Parent,true);
						DestroyWindow(hWnd);
					}
					break;
			}
			break;
		case WM_DESTROY:
			SetWindowLong(hwndList,GWL_WNDPROC,(long)oldproc);
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			EndPaint(hWnd, &ps);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// This is an example of an exported function.
GETFILENAME_API int fnGetFileName(HWND Parent,LPTSTR Filter, LPTSTR initialDir, LPTSTR OutFileName, int MaxFileName,LPTSTR* szMultiBuff,unsigned int vkctrl,int webview)
{
	int ret;
	bEnd=false;
	g_Parent=Parent;
	g_webView=webview;
	vkCtrl=vkctrl;
	EnableWindow(Parent,false);
	wcscpy(g_Filter,Filter);
	wcsupr(g_Filter);
	wcscpy(FileName,OutFileName);
	if (initialDir) wcscpy(CurrentPath,initialDir);
	NumFiles=0;
	AllowMulti=(szMultiBuff!=NULL);
	LoadSort();
	ret=InitInstance ((HINSTANCE)hmod, SW_SHOW); 
	SaveSort();
	wcscpy(OutFileName,FileName);
	if (szMultiBuff) *szMultiBuff=MultiBuff;
	return ret;

}

// This is the constructor of a class that has been exported.
// see GetFileName.h for the class definition
CGetFileName::CGetFileName()
{ 
	return; 
}

