/*
* IconedFileList.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL++2000
// 2000.07.01
#pragma once
#include <sol\ListView.h>
#include <sol\SystemImageList.h>
#include <sol\FileFinder.h>
#include <sol/FileInfo.h>
namespace SOL {
class IconedFileList :public ListView {
private:
SystemImageList imglist;
private:
TCHAR currentDir[1024];
private:
bool scanning;
private:
virtual void addColumn()
{
ListViewColumn items[] = {
{_T("FileName"), LVCFMT_LEFT, 260},
{_T("Type"), LVCFMT_LEFT, 180},
{_T("Size(KB)"), LVCFMT_RIGHT, 100},
{_T("DateTime"), LVCFMT_LEFT, 140},
};
setColumn(items, SizeOf(items));
}
public:
/**
* Constructor
*/
IconedFileList()
:ListView()
{
memset(currentDir, (TCHAR)0, SizeOf(currentDir));
}
public:
/**
* Constructor
*/
IconedFileList(View* parent, const TCHAR* name, Args& args)
:ListView(parent, name, args)
{
memset(currentDir, (TCHAR)0, SizeOf(currentDir));
setImageList(&imglist, LVSIL_SMALL);
setImageList(&imglist, LVSIL_NORMAL);
addColumn();
}
public:
~IconedFileList()
{
setImageList((HIMAGELIST)NULL, LVSIL_SMALL);
setImageList((HIMAGELIST)NULL, LVSIL_NORMAL);
}
public:
Boolean create(View* parent, const TCHAR* name, Args& args)
{
Boolean rc = ListView::create(parent, name, args);
setImageList(&imglist, LVSIL_SMALL);
setImageList(&imglist, LVSIL_NORMAL);
addColumn();
return rc;
}
public:
void findFiles(const TCHAR* dir)
{
if (dir == NULL) {
return;
}
strcpy_s(currentDir, SizeOf(currentDir), dir);
setRedraw(FALSE);
clear();
// Note: typedef WIN32_FIND_DATA FindData;
WIN32_FIND_DATA data;
TCHAR buffer[_MAX_PATH];
_stprintf_s(buffer, SizeOf(buffer), _T("%s\\*"), dir);
TCHAR fullPath[1024];
// Create an instance of FileFinder.
FileFinder fileFinder(buffer);
if (fileFinder.getFirst(&data)) {
do {
TCHAR size[80];
TCHAR date[128];
//2009/11/06
if (String::endsWith(dir, _T("\\"))) {
_stprintf_s(fullPath, SizeOf(fullPath), _T("%s%s"),
dir, data.cFileName);
} else {
_stprintf_s(fullPath, SizeOf(fullPath), _T("%s\\%s"),
dir, data.cFileName);
}
String type = "";
FileInfo fileInfo;
fileInfo.getFileType(fullPath, type);
size[0] = Zero;
date[0] = Zero;
// Get a file time from ftLastWriteTime field of FindData structure.
SYSTEMTIME time;
FILETIME ltime;
::FileTimeToLocalFileTime(&(data.ftLastWriteTime), <ime);
::FileTimeToSystemTime(<ime, &time);
_stprintf_s(date, SizeOf(date), _T("%.4u/%.2u/%.2u %.2u:%.2u:%.2u"),
time.wYear, time.wMonth, time.wDay,
time.wHour, time.wMinute, time.wSecond);
LV_ITEM item;
memset(&item, 0, sizeof(LV_ITEM));
DWORD attr = 0;
int iconId = 0;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
iconId = imglist.getDirectoryIndex(data.cFileName);
} else {
iconId = imglist.getNormalFileIndex(data.cFileName);
int kb = (data.nFileSizeLow/1024);
int remain = (data.nFileSizeLow%1024);
if (remain>0) {
kb++;
}
_stprintf_s(size, SizeOf(size), _T("%lu"), kb);
}
if (strcmp(_T("."), data.cFileName) != 0 &&
strcmp(_T(".."), data.cFileName) != 0) {
// Insert an item having an iconIndex and a filename into this listView.
item.mask = LVIF_TEXT|LVIF_IMAGE;
item.iImage = iconId;
item.pszText = data.cFileName;
item.cchTextMax = _MAX_PATH;
int m = insertItem(&item);
int n = 1;
// Set type, size, filedatetime to the line of index m.
setItemText(m, n++, (const TCHAR*)type);
setItemText(m, n++, size);
setItemText(m, n++, date);
}
} while(fileFinder.getNext(&data));
}
setRedraw(TRUE);
}
private:
void dispatchMessage()
{
MSG msg;
while(
PeekMessage (&msg,NULL,0,0,PM_REMOVE)) {
Sleep(10);
if (msg.message == WM_QUIT) {
scanning = false;
//Repost WM_QUIT message to your application message queue.
PostMessage(NULL, WM_QUIT, 0, 0);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
public:
//2009/11/06
void findAllFiles(const TCHAR* dir, const TCHAR* ext)
{
if (dir == NULL) {
return;
}
strcpy_s(currentDir, SizeOf(currentDir), dir);
setRedraw(FALSE);
clear();
scanning = true;
findAllFilesRecursively(dir, ext);
scanning = false;
setRedraw(TRUE);
}
public:
bool isScanning()
{
return scanning;
}
public:
void stopScanning()
{
scanning =false;
}
private:
//2009/11/06
void findAllFilesRecursively(const TCHAR* dir, const TCHAR* ext)
{
if (scanning == false) {
return;
}
// Note: typedef WIN32_FIND_DATA FindData;
WIN32_FIND_DATA data;
TCHAR buffer[_MAX_PATH];
_stprintf_s(buffer, SizeOf(buffer), _T("%s\\*"), dir);
TCHAR fullPath[1024];
// Create an instance of FileFinder.
FileFinder fileFinder(buffer);
View* parent = getParent();
if (parent) {
TCHAR message[MAX_PATH*2];
_stprintf(message, _T("Scanning the folder: %s"), dir);
parent->setText(message);
}
if (fileFinder.getFirst(&data)) {
do {
dispatchMessage();
if (scanning == false) {
break;
}
TCHAR size[80];
TCHAR date[128];
//2009/11/06
if (String::endsWith(dir, _T("\\"))) {
_stprintf_s(fullPath, SizeOf(fullPath), _T("%s%s"),
dir, data.cFileName);
} else {
_stprintf_s(fullPath, SizeOf(fullPath), _T("%s\\%s"),
dir, data.cFileName);
}
String type = "";
FileInfo fileInfo;
fileInfo.getFileType(fullPath, type);
size[0] = Zero;
date[0] = Zero;
// Get a file time from ftLastWriteTime field of FindData structure.
SYSTEMTIME time;
FILETIME ltime;
::FileTimeToLocalFileTime(&(data.ftLastWriteTime), <ime);
::FileTimeToSystemTime(<ime, &time);
_stprintf_s(date, SizeOf(date), _T("%.4u/%.2u/%.2u %.2u:%.2u:%.2u"),
time.wYear, time.wMonth, time.wDay,
time.wHour, time.wMinute, time.wSecond);
LV_ITEM item;
memset(&item, 0, sizeof(LV_ITEM));
DWORD attr = 0;
int iconId = 0;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
iconId = imglist.getDirectoryIndex(fullPath);
} else {
iconId = imglist.getNormalFileIndex(fullPath);
int kb = (data.nFileSizeLow/1024);
int remain = (data.nFileSizeLow%1024);
if (remain>0) {
kb++;
}
_stprintf_s(size, SizeOf(size), _T("%lu"), kb);
}
if (strcmp(_T("."), data.cFileName) != 0 &&
strcmp(_T(".."), data.cFileName) != 0) {
//If the file extension of data.cFileName were matched with ext,
if (String::endsWithIgnoreCase(data.cFileName, ext)) {
// Insert an item having an iconIndex and a filename into this listView.
item.mask = LVIF_TEXT|LVIF_IMAGE;
item.iImage = iconId;
item.pszText = fullPath;
item.cchTextMax = _MAX_PATH;
int m = insertItem(&item);
int n = 1;
// Set type, size, filedatetime to the line of index m.
setItemText(m, n++, (const TCHAR*)type);
setItemText(m, n++, size);
setItemText(m, n++, date);
}
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
findAllFilesRecursively(fullPath, ext);
}
}
} while(fileFinder.getNext(&data));
}
setRedraw(TRUE);
}
public:
HIMAGELIST detachImageList()
{
setImageList((HIMAGELIST)NULL, LVSIL_SMALL);
return setImageList((HIMAGELIST)NULL, LVSIL_NORMAL);
}
public:
bool getSelectedFilePath(__out String& fileName,__out String& filePath)
{
bool rc = false;
int count = getItemCount();
TCHAR name[1024];
memset(name, (TCHAR)0, SizeOf(name));
for (int i = 0; i<count; i++) {
UINT state = getItemState(i, LVIS_SELECTED);
if (state & LVIS_SELECTED) {
//Get a file name to buffer text
LV_ITEM item;
memset(&item, 0, sizeof(item));
item.iItem = i;
item.pszText = name;
item.mask = LVIF_TEXT;
item.cchTextMax = SizeOf(name);
getItem(&item);
rc = true;
break;
}
} //for
if (rc) {
TCHAR path[1024];
_stprintf_s(path, SizeOf(path), _T("%s\\%s"), currentDir, name);
filePath = path;
fileName = name;
}
return rc;
}
public:
bool getSelectedFileName(__out String& fileName)
{
bool rc = false;
int count = getItemCount();
TCHAR name[1024];
memset(name, (TCHAR)0, SizeOf(name));
for (int i = 0; i<count; i++) {
UINT state = getItemState(i, LVIS_SELECTED);
if (state & LVIS_SELECTED) {
//Get a file name to buffer text
LV_ITEM item;
memset(&item, 0, sizeof(item));
item.iItem = i;
item.pszText = name;
item.mask = LVIF_TEXT;
item.cchTextMax = SizeOf(name);
getItem(&item);
rc = true;
break;
}
} //for
if (rc) {
fileName = name;
}
return rc;
}
public:
const TCHAR* getCurrentDir()
{
return currentDir;
}
};
}
|