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

📄 favoriteshelper.cpp

📁 wince IE浏览器的源码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "Precomp.h"

#ifndef NO_FAVORITES
#include "defines.h"
#include "resource.h"
#include "FavStorageIfc.h"
#include "FavoritesHelper.h"

BOOL CFavoritesHelper::InitHelper (HINSTANCE hInstance,
								   CWindow *pTreeCtrl,
								   IFavStorage *pFavStorage,
								   int nMode)
{
	if (pTreeCtrl != NULL && pFavStorage != NULL && hInstance != NULL &&
		(nMode == MODE_ORGANIZEFAV || nMode == MODE_FAVFOLDERS))
	{
		m_pTreeCtrl   = pTreeCtrl;
		m_pFavStorage = pFavStorage;
		m_nMode       = nMode;
		g_hInstance   = hInstance;
		m_hIml        = NULL;
		m_hRootItem   = NULL;
		m_hNewItem    = NULL;
		return TRUE;
	}
	return FALSE;
}

CFavoritesHelper::~CFavoritesHelper ()
{
	m_pTreeCtrl = NULL;
	m_pFavStorage = NULL;

	if (m_hIml)
	{
		ImageList_Destroy(m_hIml);
		m_hIml = NULL;
	}
}

BOOL CFavoritesHelper::AddIconToImgList (UINT uiIcoID, int& nIcoIndex)
{
	HICON hIcon = NULL;

	// Add the icon
    if (!(hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(uiIcoID))))
        return FALSE;

    if (0 > (nIcoIndex = ImageList_AddIcon(m_hIml, hIcon)))
	{
		DeleteObject (hIcon);
        return FALSE;
	}

    DeleteObject(hIcon);
	hIcon = NULL;

	return TRUE;
}

BOOL CFavoritesHelper::InitTreeViewImageLists() 
{ 
	BOOL  bRetVal   = TRUE;
	int   nNumIcons = 0;

	if (m_nMode == MODE_ORGANIZEFAV)
		nNumIcons = 3;
	else
		nNumIcons = 2;

    // Create the image list. 
	m_hIml = NULL;
    if (!(m_hIml = ImageList_Create(CX_ICON,CY_ICON,FALSE,nNumIcons,0))) 
		goto ERR_RET;

    // Add the open folder icon. 
	if (!AddIconToImgList (IDI_FOLDER_OPEN, m_nOpenFolder))
		goto ERR_RET;

    // Add the close folder icon. 
	if (!AddIconToImgList (IDI_FOLDER_CLOSE, m_nCloseFolder))
		goto ERR_RET;

    // Add the URL icon. 
	if (m_nMode == MODE_ORGANIZEFAV)
	{
		if (!AddIconToImgList (IDI_URL, m_nURL))
			goto ERR_RET;
	}

    // Fail if not all of the images were added. 
    if (nNumIcons > ImageList_GetImageCount(m_hIml))
        goto ERR_RET;

    // Associate the image list with the tree-view control. 
    TreeView_SetImageList(m_pTreeCtrl->m_hWnd, m_hIml, TVSIL_NORMAL); 

	goto RETURN;

ERR_RET:
	if (m_hIml)
	{
		ImageList_Destroy(m_hIml);
		m_hIml = NULL;
	}

	bRetVal = FALSE;

RETURN:

    return bRetVal; 
}

DWORD CFavoritesHelper::PopulateTreeControl (LPTSTR szPath,HTREEITEM hTreeItem, DWORD level)
{
	HTREEITEM hParent = NULL;
	DWORD     dwFavorites = 0;
	DWORD     dwRegKeyLen = 0;
	DWORD     idx = 0;
	DWORD     dwValues = 0;
	TCHAR     szRegKey1[MAX_REGKEY_LEN-1];
	TCHAR     szRegKey [FAV_NAME_LEN-1];

	INIT_STR (szRegKey);
	INIT_STR (szRegKey1);

	// Add the first item to tree, which is Favorites itself
	if ((m_nMode == MODE_FAVFOLDERS) && (hTreeItem == NULL))
	{
		level += 1;
		hTreeItem = AddItemToTree(_T("Favorites"),level,hTreeItem,hParent);
		m_hRootItem = hTreeItem;
		SetItemInfo (hTreeItem, szPath);
	}

	if (ERROR_SUCCESS != m_pFavStorage->GetFolderInfo (szPath,
													   dwFavorites,
												       dwValues))
		goto ERR_RET;

	if (dwFavorites <= 0)
	{
		level = (level < 0)?0:level;

		// If this item has no values also, it is assumed to be a folder.
		if (dwValues <= 0)
		{
			TVITEM    tvi;
			tvi.mask = TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
			tvi.hItem = hTreeItem;
			tvi.iImage = m_nCloseFolder; 
			tvi.iSelectedImage = m_nOpenFolder; 
			if (0 > TreeView_SetItem(m_pTreeCtrl->m_hWnd, &tvi))
				goto ERR_RET;
			if (!SetItemInfo (hTreeItem, NULL))
				goto ERR_RET;
		}
		else 
		{
			if (m_nMode == MODE_FAVFOLDERS) // We don't want any leaf items. This tree lists only folders (if any)
				TreeView_DeleteItem (m_pTreeCtrl->m_hWnd, hTreeItem);
		}
		goto RETURN;
	}
	else
	{
		if (!SetItemInfo (hTreeItem, szPath))
			goto ERR_RET;
		level += 1;
		hParent = hTreeItem;
	}

	// For each sub key under favorites, add the favorite title to tree
	for (idx=0; idx < dwFavorites; idx++)
	{
		dwRegKeyLen = FAV_NAME_LEN-1;
		if (ERROR_SUCCESS != m_pFavStorage->EnumFolders (szPath,
													     idx,
												   	     szRegKey,
														 dwRegKeyLen))
			continue;

		hTreeItem = AddItemToTree(szRegKey,level,hTreeItem,hParent);
		swprintf(szRegKey1,_T("%s\\%s"),szPath,szRegKey);
		level = PopulateTreeControl(szRegKey1,hTreeItem,level);
		level = (level <= 0)?1:level;
	}
	level = (level > 0)?level-1:level;
	goto RETURN;

ERR_RET:

	level = 0;

RETURN:

	return level;
}

BOOL CFavoritesHelper::SetItemInfo (HTREEITEM hTreeItem, LPCTSTR szRegKey)
{
    TVITEM tvi; 
	DWORD dwLen = 0;
	LPTSTR szTmpStr = NULL;

	if (!hTreeItem)
		return TRUE;

	// If szRegKey is NULL then hTreeItem is a folder item. For this,
	// get the parent item and construct the lParam from it
	if (szRegKey == NULL)
	{	
		TVITEM tviParent;
		TVITEM tviItem;
		BOOL bParent = FALSE;

		bParent = GetParentItem (hTreeItem, tviParent);
		if (bParent)
			dwLen = _tcslen (reinterpret_cast<LPTSTR>(tviParent.lParam));
		else
			dwLen = _tcslen (m_pFavStorage->GetRootFolder());

		// Get some information about this item too
		IsLeafItem (hTreeItem, tviItem);
		dwLen += _tcslen (tviItem.pszText);

		if (NULL == (szTmpStr = new TCHAR[dwLen + 1]))
			return FALSE;

		memset ((void*)szTmpStr, 0, dwLen*sizeof (TCHAR));
		if (bParent)
			wsprintf (szTmpStr, L"%s\\%s",reinterpret_cast<LPTSTR>(tviParent.lParam),tviItem.pszText);
		else
			wsprintf (szTmpStr, L"%s\\%s",m_pFavStorage->GetRootFolder(),tviItem.pszText);
	}
	else
	{
		if (NULL == (szTmpStr = new TCHAR[_tcslen(szRegKey) + 1]))
			return FALSE;
		wcscpy(szTmpStr,szRegKey);
	}

	tvi.mask = TVIF_HANDLE|TVIF_PARAM;
	tvi.hItem = hTreeItem;
	tvi.lParam = reinterpret_cast<LPARAM>(szTmpStr);

	// Set the item information
	if (!TreeView_SetItem(m_pTreeCtrl->m_hWnd, &tvi))
	{
		delete[] szTmpStr;
		tvi.lParam = NULL;
		return FALSE;
	}
	
	return TRUE;
}

HTREEITEM CFavoritesHelper::AddItemToTree(LPTSTR lpszItem, int nLevel, HTREEITEM hPrevItem, HTREEITEM hParent, BOOL bAppendLast, BOOL bFolder)
{ 
    TVITEM tvi; 
    TVINSERTSTRUCT tvins; 
    HTREEITEM hPrev = (!hPrevItem)?(HTREEITEM)TVI_FIRST:hPrevItem; 
    HTREEITEM hti; 

    tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; 
 
    // Set the text of the item. 
    tvi.pszText = lpszItem; 
    tvi.cchTextMax = lstrlen(lpszItem); 

	// If this is a Folder item, set folder icons
	if ((bFolder) || (m_nMode == MODE_FAVFOLDERS))
	{
        tvi.iImage = m_nCloseFolder; 
        tvi.iSelectedImage = m_nOpenFolder; 
	}
	else
	{
		if (m_nMode == MODE_ORGANIZEFAV)
		{
			tvi.iImage = m_nURL; 
			tvi.iSelectedImage = m_nURL; 
		}
	}
 
    // Save the heading level in the item's application-defined 
    // data area. 
	tvi.lParam = (LPARAM) NULL;
 
    tvins.item = tvi; 
    tvins.hInsertAfter = (bAppendLast)?TVI_LAST:hPrev; 
 
    // Set the parent item based on the specified level. 
    if ((nLevel == 1) || (hParent == NULL))
	{
		if (m_nMode == MODE_FAVFOLDERS)
			tvins.hParent = m_hRootItem;
		else
			tvins.hParent = TVI_ROOT; 
	}
    else
		tvins.hParent = hParent;
 
    // Add the item to the tree-view control. 
    hPrev = (HTREEITEM) SendMessage(m_pTreeCtrl->m_hWnd, TVM_INSERTITEM, 0, 
									(LPARAM) (LPTVINSERTSTRUCT) &tvins); 
 
    if (nLevel > 1) 
	{ 
        hti = TreeView_GetParent(m_pTreeCtrl->m_hWnd, hPrev); 
        tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
        tvi.hItem = hti; 
        tvi.iImage = m_nCloseFolder; 
        tvi.iSelectedImage = m_nOpenFolder; 
        TreeView_SetItem(m_pTreeCtrl->m_hWnd, &tvi); 
    } 
 
    return hPrev; 
}

BOOL CFavoritesHelper::IsLeafItem (HTREEITEM hTreeItem, TVITEM &tv)
{
	TCHAR *szText = NULL;

	if (NULL == (szText = new TCHAR[FAV_NAME_LEN]))
		return FALSE;

	memset((void*)szText,0,sizeof (TCHAR) * FAV_NAME_LEN);
	
	// Get the information about this item
	tv.mask = TVIF_HANDLE|TVIF_TEXT;
	tv.hItem = hTreeItem;
	tv.pszText = szText;
	tv.cchTextMax = FAV_NAME_LEN;
	if (! TreeView_GetItem(m_pTreeCtrl->m_hWnd, &tv))
		return FALSE;

	// Check if this is a leaf item or a folder
	if (tv.lParam != NULL)		
		return FALSE;

	return TRUE;
}

BOOL CFavoritesHelper::GetParentItem (HTREEITEM hTreeItem, TVITEM &tv)
{
	HTREEITEM hParent = NULL;

	// Get the parent for this item. 
	hParent = TreeView_GetParent(m_pTreeCtrl->m_hWnd, hTreeItem);

	if (hParent != NULL)
	{
		tv.mask = TVIF_HANDLE|TVIF_PARAM;
		tv.hItem = hParent;
		if (! TreeView_GetItem(m_pTreeCtrl->m_hWnd, &tv))
			return FALSE;
		return TRUE;
	}

	return FALSE;
}

void CFavoritesHelper::ShowErrDialog (HWND hWnd, int nID)
{
	TCHAR szBfr[512];

	// Load the string resource
	INIT_STR (szBfr);

	if (0 != LoadString(g_hInstance, nID, szBfr, 512))
		::MessageBox(hWnd, szBfr, NULL, MB_OK|MB_ICONERROR);
}

LRESULT CFavoritesHelper::CreateFolder (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	HTREEITEM hCurItem    = NULL;
	HTREEITEM hParentItem = NULL;
	UINT      nLevel      = 2;
	TVITEM tvi;

	// Get the selected item
	if (NULL == (hCurItem = TreeView_GetSelection(m_pTreeCtrl->m_hWnd)))
	{
		// if there are no items in the tree, we can go ahead by adding the
		// folder to root
		if (0 >= TreeView_GetCount (m_pTreeCtrl->m_hWnd))
		{
			nLevel = 1;
			hCurItem = NULL;
		}
		else
			return -1;
	}
	hParentItem = hCurItem;
	
	// Get this item information to check if this is a folder or not
	if (hCurItem)
	{
		tvi.mask  = TVIF_HANDLE;
		if (m_nMode == MODE_ORGANIZEFAV)
			tvi.mask |= TVIF_STATE;
		tvi.hItem = hCurItem;	
		if (m_nMode == MODE_ORGANIZEFAV)
			tvi.stateMask = TVIS_STATEIMAGEMASK;
		if (TreeView_GetItem(m_pTreeCtrl->m_hWnd, &tvi))
		{
			// If this is a leaf node or the folder is not expanded,
			// then create the new folder under its parent.
			int tmp = tvi.state & TVIS_EXPANDED;
			if (m_nMode == MODE_ORGANIZEFAV)
			{
				if ((tvi.lParam == NULL) ||
					(tmp == 0))
					hParentItem = TreeView_GetParent(m_pTreeCtrl->m_hWnd, hCurItem);
			}
			else if (tvi.lParam == NULL)
					hParentItem = TreeView_GetParent(m_pTreeCtrl->m_hWnd, hCurItem);

		}
		else
			return -1;
	}

	// Add the new item as a child to hCurItem
	hCurItem = AddItemToTree(NEW_FOLDER, nLevel, hCurItem, hParentItem, TRUE, TRUE);

	if (m_nMode == MODE_FAVFOLDERS)
	{
		if (hParentItem == NULL)
			TreeView_Expand(m_pTreeCtrl->m_hWnd, m_hRootItem, TVE_EXPAND);
		else
			TreeView_Expand(m_pTreeCtrl->m_hWnd, hParentItem, TVE_EXPAND);
	}

	// Make the user edit this item
	if (hCurItem)
	{
		m_hNewItem = hCurItem;
		m_pTreeCtrl->SetFocus();
		TreeView_EditLabel(m_pTreeCtrl->m_hWnd, hCurItem);
	}
	else
		return -1;

	return ERROR_SUCCESS;
}

LRESULT CFavoritesHelper::BeginEditLabel(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	LPNMTVDISPINFO lpTVInfo = (LPNMTVDISPINFO)pnmh;

	if (lpTVInfo != NULL)
	{
		if (!m_hNewItem ||
			(lpTVInfo->item.hItem != m_hNewItem))
			return 1;
	}

	return 0;
}

LRESULT CFavoritesHelper::EndEditLabel(int idCtrl, LPNMHDR pnmh, BOOL& bHandled, 
									   BOOL& bRename, LPTSTR szSrcPath)
{
	LPNMTVDISPINFO lpTVInfo = (LPNMTVDISPINFO)pnmh;
	HTREEITEM hParentItem   = NULL;
	DWORD dwDescription     = 0;
	TVITEM tvi;
	TCHAR  szRegKey[MAX_REGKEY_LEN-1];

	INIT_STR(szRegKey);

	if (lpTVInfo != NULL)
	{
		if ((lpTVInfo->item.pszText == NULL) && (!bRename))
			lpTVInfo->item.pszText = NEW_FOLDER;

		if (lpTVInfo->item.pszText != NULL) 
		{
			if (_tcslen (lpTVInfo->item.pszText) <= 0)
			{
				if (bRename)
				{
					ShowErrDialog (::GetParent(m_pTreeCtrl->m_hWnd), IDS_ERR_EMPTY_TITLE);
					m_pTreeCtrl->SetFocus();
					TreeView_EditLabel(m_pTreeCtrl->m_hWnd, lpTVInfo->item.hItem);
					return FALSE;
				}
				else
				{
					TreeView_DeleteItem (m_pTreeCtrl->m_hWnd, lpTVInfo->item.hItem);
					return TRUE;
				}
			}
				
			// Set the lparam for this item
			hParentItem = TreeView_GetParent(m_pTreeCtrl->m_hWnd, lpTVInfo->item.hItem);
			if (!hParentItem)
				hParentItem = (m_nMode == MODE_FAVFOLDERS)?m_hRootItem:TVI_ROOT;

			if (hParentItem == ((m_nMode == MODE_FAVFOLDERS)?m_hRootItem:TVI_ROOT))
				wsprintf(szRegKey,L"%s\\%s",m_pFavStorage->GetRootFolder(),lpTVInfo->item.pszText);
			else
			{
				tvi.mask  = TVIF_PARAM|TVIF_HANDLE;
				tvi.hItem = hParentItem;
				TreeView_GetItem(m_pTreeCtrl->m_hWnd, &tvi);
				wsprintf(szRegKey,L"%s\\%s",reinterpret_cast<LPTSTR>(tvi.lParam),lpTVInfo->item.pszText);
			}

			if (bRename)
			{
				RenameItem (szSrcPath, szRegKey);
				SetItemInfo (lpTVInfo->item.hItem, szRegKey);
				bRename = FALSE;
				return TRUE;
			}

			LRESULT lRetVal;
			if (hParentItem == ((m_nMode == MODE_FAVFOLDERS)?m_hRootItem:TVI_ROOT))
				lRetVal = m_pFavStorage->CreateFavorite (NULL,
														 lpTVInfo->item.pszText,
														 NULL);
			else
				lRetVal = m_pFavStorage->CreateFavorite (reinterpret_cast<LPTSTR>(tvi.lParam),
														 lpTVInfo->item.pszText,
														 NULL);

			if (lRetVal == ERR_DUPLICATE_ENTRY)
			{
				ShowErrDialog (::GetParent(m_pTreeCtrl->m_hWnd), IDS_DUPLICATE_ITEM);
				
				// Make the user edits this item
				m_pTreeCtrl->SetFocus();
				TreeView_EditLabel(m_pTreeCtrl->m_hWnd, lpTVInfo->item.hItem);
				return FALSE;
			}
			else if (m_nMode == MODE_FAVFOLDERS)
					SetItemInfo (lpTVInfo->item.hItem,szRegKey);

			if (m_nMode == MODE_FAVFOLDERS)
				TreeView_SelectItem (m_pTreeCtrl->m_hWnd,lpTVInfo->item.hItem);

			SetNewItem(NULL);
			return TRUE;
		}
	}

	return FALSE;
}

BOOL CFavoritesHelper::RenameItem (LPTSTR szSrcPath, LPTSTR szNewItem)
{
	// Don't do any thing if user hasn't renamed the item
	if (!_tcsicmp (szNewItem,szSrcPath))
		return TRUE;

	if (ERROR_SUCCESS == m_pFavStorage->Rename (szSrcPath,szNewItem))
		return TRUE;

	return FALSE;
}

#endif // NO_FAVORITES

⌨️ 快捷键说明

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