⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explorer.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2003, 2004, 2005, 2006 Martin Fuchs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


 //
 // Explorer clone
 //
 // explorer.cpp
 //
 // Martin Fuchs, 23.07.2003
 //
 // Credits: Thanks to Leon Finker for his explorer cabinet window example
 //


#include <precomp.h>	// <precomp.h> instead of "precomp.h" because the ROS build system needs this to find the precompiled header file (*.gch) in the output directory tree

#include "resource.h"

#include <locale.h>	// for setlocale()

#ifndef __WINE__
#include <io.h>		// for dup2()
#include <fcntl.h>	// for _O_RDONLY
#endif

#include "dialogs/settings.h"	// for MdiSdiDlg

#include "services/shellservices.h"


extern "C" int initialize_gdb_stub();	// start up GDB stub


DynamicLoadLibFct<void(__stdcall*)(BOOL)> g_SHDOCVW_ShellDDEInit(TEXT("SHDOCVW"), 118);


ExplorerGlobals g_Globals;


ExplorerGlobals::ExplorerGlobals()
{
	_hInstance = 0;
	_cfStrFName = 0;

#ifndef ROSSHELL
	_hframeClass = 0;
	_hMainWnd = 0;
	_desktop_mode = false;
	_prescan_nodes = false;
#endif

	_log = NULL;
#ifndef __MINGW32__	// SHRestricted() missing in MinGW (as of 29.10.2003)
	_SHRestricted = 0;
#endif
	_hwndDesktopBar = 0;
	_hwndShellView = 0;
	_hwndDesktop = 0;
}


void ExplorerGlobals::init(HINSTANCE hInstance)
{
	_hInstance = hInstance;

#ifndef __MINGW32__	// SHRestricted() missing in MinGW (as of 29.10.2003)
	_SHRestricted = (DWORD(STDAPICALLTYPE*)(RESTRICTIONS)) GetProcAddress(GetModuleHandle(TEXT("SHELL32")), "SHRestricted");
#endif

	_icon_cache.init();
}


void ExplorerGlobals::read_persistent()
{
	 // read configuration file
	_cfg_dir.printf(TEXT("%s\\ReactOS"), (LPCTSTR)SpecialFolderFSPath(CSIDL_APPDATA,0));
	_cfg_path.printf(TEXT("%s\\ros-explorer-cfg.xml"), _cfg_dir.c_str());

	if (!_cfg.read(_cfg_path)) {
		//if (_cfg._last_error != XML_ERROR_NO_ELEMENTS)
		MessageBox(_hwndDesktop, _cfg._errors.str(),
					TEXT("ROS Explorer - reading user settings"), MB_OK);

		_cfg.read(TEXT("explorer-cfg-template.xml"));
	}

	 // read bookmarks
	_favorites_path.printf(TEXT("%s\\ros-explorer-bookmarks.xml"), _cfg_dir.c_str());

	if (!_favorites.read(_favorites_path)) {
		_favorites.import_IE_favorites(0);
		_favorites.write(_favorites_path);
	}
}

void ExplorerGlobals::write_persistent()
{
	 // write configuration file
	RecursiveCreateDirectory(_cfg_dir);

	_cfg.write(_cfg_path);
	_favorites.write(_favorites_path);
}


XMLPos ExplorerGlobals::get_cfg()
{
	XMLPos cfg_pos(&_cfg);

	cfg_pos.smart_create("explorer-cfg");

	return cfg_pos;
}

XMLPos ExplorerGlobals::get_cfg(const char* path)
{
	XMLPos cfg_pos(&_cfg);

	cfg_pos.smart_create("explorer-cfg");
	cfg_pos.create_relative(path);

	return cfg_pos;
}


void _log_(LPCTSTR txt)
{
	FmtString msg(TEXT("%s\n"), txt);

	if (g_Globals._log)
		_fputts(msg, g_Globals._log);

	OutputDebugString(msg);
}


bool FileTypeManager::is_exe_file(LPCTSTR ext)
{
	static const LPCTSTR s_executable_extensions[] = {
		TEXT("COM"),
		TEXT("EXE"),
		TEXT("BAT"),
		TEXT("CMD"),
		TEXT("CMM"),
		TEXT("BTM"),
		TEXT("AWK"),
		0
	};

	TCHAR ext_buffer[_MAX_EXT];
	const LPCTSTR* p;
	LPCTSTR s;
	LPTSTR d;

	for(s=ext+1,d=ext_buffer; (*d=toupper(*s)); s++)
		++d;

	for(p=s_executable_extensions; *p; p++)
		if (!lstrcmp(ext_buffer, *p))
			return true;

	return false;
}


const FileTypeInfo& FileTypeManager::operator[](String ext)
{
	ext.toLower();

	iterator found = find(ext);
	if (found != end())
		return found->second;

	FileTypeInfo& ftype = super::operator[](ext);

	ftype._neverShowExt = false;

	HKEY hkey;
	TCHAR value[MAX_PATH], display_name[MAX_PATH];
	LONG valuelen = sizeof(value);

	if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
		ftype._classname = value;

		valuelen = sizeof(display_name);
		if (!RegQueryValue(HKEY_CLASSES_ROOT, ftype._classname, display_name, &valuelen))
			ftype._displayname = display_name;

		if (!RegOpenKey(HKEY_CLASSES_ROOT, ftype._classname, &hkey)) {
			if (!RegQueryValueEx(hkey, TEXT("NeverShowExt"), 0, NULL, NULL, NULL))
				ftype._neverShowExt = true;

			RegCloseKey(hkey);
		}
	}

	return ftype;
}

LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
{
	LPCTSTR ext = _tcsrchr(entry->_data.cFileName, TEXT('.'));

	if (ext) {
		const FileTypeInfo& type = (*this)[ext];

		if (!type._displayname.empty())
			entry->_type_name = _tcsdup(type._displayname);

		 // hide some file extensions
		if (type._neverShowExt && !dont_hide_ext) {
			int len = ext - entry->_data.cFileName;
			entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
			lstrcpyn(entry->_display_name, entry->_data.cFileName, len + 1);
		}

		if (is_exe_file(ext))
			entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
	}

	return ext;
}


Icon::Icon()
 :	_id(ICID_UNKNOWN),
	_itype(IT_STATIC),
	_hicon(0)
{
}

Icon::Icon(ICON_ID id, UINT nid)	//, int cx, int cy
 :	_id(id),
	_itype(IT_STATIC),
	_hicon(ResIcon(nid))	// ResIconEx(nid, cx, cy)
{
}

Icon::Icon(ICON_ID id, UINT nid, int icon_size)
 :	_id(id),
	_itype(IT_STATIC),
	_hicon(ResIconEx(nid, icon_size, icon_size))
{
}

Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
 :	_id((ICON_ID)id),
	_itype(itype),
	_hicon(hIcon)
{
}

Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
 :	_id((ICON_ID)id),
	_itype(itype),
	_sys_idx(sys_idx)
{
}

void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
{
	if (_itype == IT_SYSCACHE)
		ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
	else
		DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
}

HBITMAP	Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
{
	if (_itype == IT_SYSCACHE) {
		HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();

		int cx, cy;
		ImageList_GetIconSize(himl, &cx, &cy);

		HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
		HDC hdc = CreateCompatibleDC(hdc_wnd);
		HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
		ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
		SelectBitmap(hdc, hbmp_old);
		DeleteDC(hdc);

		return hbmp;
	} else
		return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
}


int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
{
	int ret;

	if (_itype == IT_SYSCACHE) {
		HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();

		int cx, cy;
		ImageList_GetIconSize(himl, &cx, &cy);

		HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
		HDC hdc = CreateCompatibleDC(hdc_wnd);
		HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
		ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
		SelectBitmap(hdc, hbmp_old);
		DeleteDC(hdc);

		ret = ImageList_Add(himl, hbmp, 0);

		DeleteObject(hbmp);
	} else
		ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);

	return ret;
}

HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd/*, int icon_size*/)
{
	int cx = ICON_SIZE_SMALL;
	int cy = ICON_SIZE_SMALL;
	HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);

	MemCanvas canvas;
	BitmapSelection sel(canvas, hbmp);

	RECT rect = {0, 0, cx, cy};
	FillRect(canvas, &rect, hbrush_bkgnd);

	DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);

	return hbmp;
}

HBITMAP create_small_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
{
	int cx = GetSystemMetrics(SM_CXSMICON);
	int cy = GetSystemMetrics(SM_CYSMICON);
	HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);

	MemCanvas canvas;
	BitmapSelection sel(canvas, hbmp);

	RECT rect = {0, 0, cx, cy};
	FillRect(canvas, &rect, hbrush_bkgnd);

	DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);

	return hbmp;
}

int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
{
	HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);

	int ret = ImageList_Add(himl, hbmp, 0);

	DeleteObject(hbmp);

	return ret;
}


int IconCache::s_next_id = ICID_DYNAMIC;


void IconCache::init()
{
	int icon_size = STARTMENUROOT_ICON_SIZE;

	_icons[ICID_NONE]		= Icon(IT_STATIC, ICID_NONE, (HICON)0);

	_icons[ICID_FOLDER]		= Icon(ICID_FOLDER,		IDI_FOLDER);
	//_icons[ICID_DOCUMENT]	= Icon(ICID_DOCUMENT,	IDI_DOCUMENT);
	_icons[ICID_EXPLORER]	= Icon(ICID_EXPLORER,	IDI_EXPLORER);
//	_icons[ICID_APP]		= Icon(ICID_APP,		IDI_APPICON);

	_icons[ICID_CONFIG]		= Icon(ICID_CONFIG,		IDI_CONFIG,		icon_size);
	_icons[ICID_DOCUMENTS]	= Icon(ICID_DOCUMENTS,	IDI_DOCUMENTS,	icon_size);
	_icons[ICID_FAVORITES]	= Icon(ICID_FAVORITES,	IDI_FAVORITES,	icon_size);
	_icons[ICID_INFO]		= Icon(ICID_INFO,		IDI_INFO,		icon_size);
	_icons[ICID_APPS]		= Icon(ICID_APPS,		IDI_APPS,		icon_size);
	_icons[ICID_SEARCH] 	= Icon(ICID_SEARCH, 	IDI_SEARCH,		icon_size);
	_icons[ICID_ACTION] 	= Icon(ICID_ACTION, 	IDI_ACTION,		icon_size);
	_icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC,	icon_size);
	_icons[ICID_PRINTER]	= Icon(ICID_PRINTER,	IDI_PRINTER,	icon_size);
	_icons[ICID_NETWORK]	= Icon(ICID_NETWORK,	IDI_NETWORK,	icon_size);
	_icons[ICID_COMPUTER]	= Icon(ICID_COMPUTER,	IDI_COMPUTER,	icon_size);
	_icons[ICID_LOGOFF] 	= Icon(ICID_LOGOFF, 	IDI_LOGOFF,		icon_size);
	_icons[ICID_SHUTDOWN]	= Icon(ICID_SHUTDOWN,	IDI_SHUTDOWN,	icon_size);
	_icons[ICID_RESTART]	= Icon(ICID_RESTART,	IDI_RESTART,	icon_size);
	_icons[ICID_BOOKMARK]	= Icon(ICID_BOOKMARK,	IDI_DOT_TRANS,	icon_size);
	_icons[ICID_MINIMIZE]	= Icon(ICID_MINIMIZE,	IDI_MINIMIZE,	icon_size);
	_icons[ICID_CONTROLPAN] = Icon(ICID_CONTROLPAN, IDI_CONTROLPAN,	icon_size);
	_icons[ICID_DESKSETTING]= Icon(ICID_DESKSETTING,IDI_DESKSETTING,icon_size);
	_icons[ICID_NETCONNS]	= Icon(ICID_NETCONNS,	IDI_NETCONNS,	icon_size);
	_icons[ICID_ADMIN]		= Icon(ICID_ADMIN,		IDI_ADMIN,		icon_size);
	_icons[ICID_RECENT] 	= Icon(ICID_RECENT, 	IDI_RECENT,		icon_size);
}


const Icon& IconCache::extract(LPCTSTR path, ICONCACHE_FLAGS flags)
{
	 // search for matching icon with unchanged flags in the cache
	CacheKey mapkey(path, flags);
	PathCacheMap::iterator found = _pathCache.find(mapkey);

	if (found != _pathCache.end())
		return _icons[found->second];

	 // search for matching icon with handle
	CacheKey mapkey_hicon(path, flags|ICF_HICON);
	if (flags != mapkey_hicon.second) {
		found = _pathCache.find(mapkey_hicon);

		if (found != _pathCache.end())
			return _icons[found->second];
	}

	 // search for matching icon in the system image list cache
	CacheKey mapkey_syscache(path, flags|ICF_SYSCACHE);
	if (flags != mapkey_syscache.second) {
		found = _pathCache.find(mapkey_syscache);

		if (found != _pathCache.end())
			return _icons[found->second];
	}

	SHFILEINFO sfi;

	int shgfi_flags = 0;

	if (flags & ICF_OPEN)
		shgfi_flags |= SHGFI_OPENICON;

	if ((flags&(ICF_LARGE|ICF_MIDDLE|ICF_OVERLAYS|ICF_HICON)) && !(flags&ICF_SYSCACHE)) {
		shgfi_flags |= SHGFI_ICON;

		if (!(flags & (ICF_LARGE|ICF_MIDDLE)))
			shgfi_flags |= SHGFI_SMALLICON;

		if (flags & ICF_OVERLAYS)
			shgfi_flags |= SHGFI_ADDOVERLAYS;

		 // get small/big icons with/without overlays
		if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags)) {
			const Icon& icon = add(sfi.hIcon, IT_CACHED);

			///@todo limit cache size
			_pathCache[mapkey_hicon] = icon;

			return icon;
		}
	} else {
		assert(!(flags&ICF_OVERLAYS));

		shgfi_flags |= SHGFI_SYSICONINDEX|SHGFI_SMALLICON;

		 // use system image list - the "search program dialog" needs it
		HIMAGELIST himlSys_small = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags);

		if (himlSys_small) {
			_himlSys_small = himlSys_small;

			const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);

			///@todo limit cache size
			_pathCache[mapkey_syscache] = icon;

			return icon;
		}
	}

	return _icons[ICID_NONE];
}

const Icon& IconCache::extract(LPCTSTR path, int icon_idx, ICONCACHE_FLAGS flags)
{
	IdxCacheKey key(path, make_pair(icon_idx, (flags|ICF_HICON)&~ICF_SYSCACHE));

	key.first.toLower();

	IdxCacheMap::iterator found = _idxCache.find(key);

	if (found != _idxCache.end())
		return _icons[found->second];

	HICON hIcon;

	if ((int)ExtractIconEx(path, icon_idx, NULL, &hIcon, 1) > 0) {
		const Icon& icon = add(hIcon, IT_CACHED);

		_idxCache[key] = icon;

		return icon;
	} else {

		///@todo retreive "http://.../favicon.ico" format icons

		return _icons[ICID_NONE];
	}
}

const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int icon_idx, ICONCACHE_FLAGS flags)
{
	HICON hIconLarge = 0;
	HICON hIcon;

	int icon_size = ICON_SIZE_FROM_ICF(flags);
	HRESULT hr = pExtract->Extract(path, icon_idx, &hIconLarge, &hIcon, MAKELONG(GetSystemMetrics(SM_CXICON), icon_size));

	if (hr == NOERROR) {	//@@ oder SUCCEEDED(hr) ?
		if (icon_size > ICON_SIZE_SMALL) {	//@@ OK?
			if (hIcon)
				DestroyIcon(hIcon);

			hIcon = hIconLarge;
		} else {
			if (hIconLarge)
				DestroyIcon(hIconLarge);
		}

		if (hIcon)
			return add(hIcon);	//@@ When do we want not to free this icons?
	}

	return _icons[ICID_NONE];
}

const Icon&	IconCache::extract(LPCITEMIDLIST pidl, ICONCACHE_FLAGS flags)
{
	 // search for matching icon with unchanged flags in the cache
	PidlCacheKey mapkey(pidl, flags);
	PidlCacheMap::iterator found = _pidlcache.find(mapkey);

	if (found != _pidlcache.end())
		return _icons[found->second];

	 // search for matching icon with handle
	PidlCacheKey mapkey_hicon(pidl, flags|ICF_HICON);
	if (flags != mapkey_hicon.second) {
		found = _pidlcache.find(mapkey_hicon);

		if (found != _pidlcache.end())
			return _icons[found->second];
	}

	 // search for matching icon in the system image list cache
	PidlCacheKey mapkey_syscache(pidl, flags|ICF_SYSCACHE);
	if (flags != mapkey_syscache.second) {
		found = _pidlcache.find(mapkey_syscache);

		if (found != _pidlcache.end())
			return _icons[found->second];
	}

	SHFILEINFO sfi;

	int shgfi_flags = SHGFI_PIDL;

	if (!(flags & (ICF_LARGE|ICF_MIDDLE)))
		shgfi_flags |= SHGFI_SMALLICON;

	if (flags & ICF_OPEN)
		shgfi_flags |= SHGFI_OPENICON;

	if (flags & ICF_SYSCACHE) {
		assert(!(flags&ICF_OVERLAYS));

		HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|shgfi_flags);
		if (himlSys) {
			const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);

			///@todo limit cache size
			_pidlcache[mapkey_syscache] = icon;

			return icon;
		}
	} else {
		if (flags & ICF_OVERLAYS)
			shgfi_flags |= SHGFI_ADDOVERLAYS;

		if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_ICON|shgfi_flags)) {
			const Icon& icon = add(sfi.hIcon, IT_CACHED);

			///@todo limit cache size
			_pidlcache[mapkey_hicon] = icon;

			return icon;
		}
	}

	return _icons[ICID_NONE];
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -