📄 explorefolders.cpp
字号:
/*
* Copyright (C) 2003-2007 Funambol, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
// exploreDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ui.h"
#include "ExploreFolders.h"
#include "FolderCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CExploreFolders dialog
#if defined(WIN32_PLATFORM_PSPC)
HWND g_hwndTreeView = 0;
HWND hwndCB;
HTREEITEM hti, htiNext;
TCHAR szDir[MAX_PATH];
#endif
IMPLEMENT_DYNAMIC(CExploreFolders, CDialog)
CExploreFolders::CExploreFolders(CWnd* pParent /*=NULL*/)
: CDialog(CExploreFolders::IDD, pParent)
{
}
CExploreFolders::~CExploreFolders()
{
}
void CExploreFolders::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
#if defined(WIN32_PLATFORM_WFSP)
DDX_Control(pDX, IDC_LIST2, lstFiles);
#endif
}
BEGIN_MESSAGE_MAP(CExploreFolders, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
#if defined(WIN32_PLATFORM_WFSP)
ON_COMMAND(IDCHOOSE, &CExploreFolders::OnChoose)
#endif
#if defined(WIN32_PLATFORM_PSPC)
ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREEVIEW, &CExploreFolders::OnTvnItemExpanded)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREEVIEW, &CExploreFolders::OnTvnItemExpanding)
ON_NOTIFY(NM_DBLCLK, IDC_TREEVIEW, &CExploreFolders::OnTvnDblClk)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREEVIEW, &CExploreFolders::OnTvnSelChanged)
ON_WM_CTLCOLOR()
#endif
END_MESSAGE_MAP()
// CExploreFolders message handlers
BOOL CExploreFolders::OnInitDialog()
{
CString s1;
// on sph only briefcase is supported
#if defined(WIN32_PLATFORM_WFSP)
s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_EXPLORE_TITLE_BRIEFCASE);
#else
if(isNotes){
s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_EXPLORE_TITLE_NOTES);
}
else{
s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_EXPLORE_TITLE_BRIEFCASE);
}
#endif
SetWindowText(s1);
CDialog::OnInitDialog();
ZeroMemory(&dlgCommandBar, sizeof(SHMENUBARINFO));
dlgCommandBar.cbSize = sizeof(SHMENUBARINFO);
dlgCommandBar.hwndParent = this->GetSafeHwnd();
dlgCommandBar.dwFlags = SHCMBF_HMENU;
dlgCommandBar.nToolBarId = IDM_OKCANCEL;
#if defined WIN32_PLATFORM_WFSP
dlgCommandBar.nToolBarId = IDM_CHOOSECANCEL;
#if (_WIN32_WCE < 0x500)
dlgCommandBar.nToolBarId = IDR_MENUBAR_CHOOSECANCEL;
#endif
#endif
dlgCommandBar.hInstRes = getLocalizationUtils()->getLocaleResource();
dlgCommandBar.nBmpId = 0;
dlgCommandBar.cBmpImages = 0;
if(!SHCreateMenuBar(&dlgCommandBar)){
TRACE(_T("Cannot create command bar!"));
return FALSE;
};
#if defined(WIN32_PLATFORM_WFSP)
// set the selection in tree
s1 = folder;
// add folders in path list
CString token(""); int iStart =0;
CString lastToken("");
path.push_back(_T("\\"));
while(iStart!=-1){
token = s1.Tokenize(_T("\\"), iStart);
if(iStart != -1){
path.push_back(token+_T("\\"));
lastToken = token;
};
}
path.pop_back();
BuildDirectory(path, s1);
SetDlgItemText(IDC_EXPLORE_FOLDER, s1);
s1+="*";
GetDirectoryContents(&lstFiles, s1.GetBuffer(s1.GetLength()));
lstFiles.SetFocus();
lstFiles.SelectString(0,lastToken);
#else
// for PPC
HWND hwndTV;
CRect rcClient;
CRect rcHeader;
g_hInstance = AfxGetInstanceHandle(); // !
// Start by initializing the common control libraries
InitCommonControls();
// Get the client area rect to put the treeview in
GetClientRect(&rcClient);
hwndCB = dlgCommandBar.hwndMB;
GetDlgItem(IDC_HEADER_STATIC)->GetClientRect(&rcHeader);
// We need to create the treeview control
hwndTV = CreateWindowEx(0, WC_TREEVIEW, TEXT("Tree View Control"),
WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS,
0, rcHeader.BottomRight().y+5, rcClient.Width(),
rcClient.Height()- rcHeader.BottomRight().y - 5,
GetSafeHwnd(),
(HMENU) IDC_TREEVIEW, g_hInstance, NULL);
// Make sure the treeview was actually created
if (!hwndTV){
return FALSE;
}
if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV)) {
::DestroyWindow(hwndTV); // check it
return FALSE;
}
// window creation was ok
g_hwndTreeView = hwndTV;
// set the selection in tree
char* source;
if(isNotes) // display folder for notes or for briefcase
source = "note";
else
source = "briefcase";
s1 = folder;
HTREEITEM hti, htiNext, htiRoot, htiFound;
TVITEM tvi;
WCHAR tviText[256];
hti = NULL; htiNext = NULL; htiRoot = NULL; htiFound = NULL;
hti = AddItemToTree(hwndTV, TEXT("\\"),NULL, true);
htiRoot = TreeView_GetRoot(hwndTV);
CString token("\\"); int iStart =0;
wstring completePath(TEXT(""));
wstring previousPath(TEXT(""));
CString lastToken("");
while(iStart!=-1){
if(iStart != -1){
//path.push_back(token+_T("\\"));
while (hti){
htiNext = TreeView_GetNextSibling(hwndTV, hti);
memset(&tvi, 0, sizeof(TV_ITEM));
tvi.hItem = hti;
tvi.mask = TVIF_CHILDREN|TVIF_HANDLE|TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvi.cchTextMax = 256;//*sizeof(wchar_t);
tvi.pszText = tviText;
TreeView_GetItem(hwndTV, &tvi);
if(wcscmp(tviText, token.GetBuffer(token.GetLength())) == 0){
htiFound = hti;
TreeView_Expand(hwndTV, htiFound,TVE_EXPAND);
TreeView_SelectDropTarget(hwndTV, htiFound);
hti = NULL;
break;
}
else{
hti = htiNext;
if(!htiNext){
hti = TreeView_GetChild(hwndTV, htiNext);
};
}
} // end while (hti)
lastToken = token;
hti = TreeView_GetChild(hwndTV, htiFound);
};
token = s1.Tokenize(_T("\\"), iStart);
if (token != "") {
completePath += TEXT("\\");
completePath += token;
}
if (existsDirectory((wchar_t*)completePath.c_str())) {
folder = completePath.c_str();
} else {
folder = previousPath.c_str();
break;
}
if (token != "") {
previousPath += TEXT("\\");
previousPath += token;
}
}
// set ppc header
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
HFONT hFont = (HFONT) GetStockObject(SYSTEM_FONT);
GetObject(hFont, sizeof lf, &lf);
lf.lfWeight = FW_BOLD;
lf.lfHeight +=FONT_HEIGHT_OFFSET;
VERIFY(fontBold.CreateFontIndirect(&lf));
if(isNotes){
s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_EXPLORE_TITLE_NOTES);
}
else{
s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_EXPLORE_TITLE_BRIEFCASE);
}
SetDlgItemText(IDC_HEADER_STATIC, s1);
GetDlgItem(IDC_HEADER_STATIC)->SetFont(&fontBold);
SendMessage(WM_SETFOCUS, (WPARAM)hwndTV, NULL);// not necessary
#endif
// fix for wrong title problem, we get & set the title for the main window
wchar_t title[256];
AfxGetMainWnd()->GetWindowText(title,256);
AfxGetMainWnd()->SetWindowText(title);
return FALSE;
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CExploreFolders::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
// TODO: not needed
/*DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_EXPLORE_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_EXPLORE_DIALOG));
*/
}
#endif
#if defined(WIN32_PLATFORM_PSPC)
void CExploreFolders::OnTvnItemExpanded(NMHDR * pNotifyStruct, LRESULT * result ){
LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) pNotifyStruct;
if (pnmtv->action == TVE_COLLAPSE)
{
// remove the child items from this directory
hti = TreeView_GetChild(pNotifyStruct->hwndFrom, pnmtv->itemNew.hItem);
while (hti){
htiNext = TreeView_GetNextSibling(pNotifyStruct->hwndFrom, hti);
TreeView_DeleteItem(pNotifyStruct->hwndFrom, hti);
hti = htiNext;
}
}
*result = 0;
}
void CExploreFolders::OnTvnItemExpanding(NMHDR * pNotifyStruct, LRESULT * result ){
LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) pNotifyStruct;
if (pnmtv->action == TVE_COLLAPSE){
// Retrieve the image from the current item
pnmtv->itemNew.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
TreeView_GetItem(pNotifyStruct->hwndFrom, &(pnmtv->itemNew));
// Set the item's image to the closed folder
pnmtv->itemNew.iImage = IMAGE_CLOSED;
pnmtv->itemNew.iSelectedImage = IMAGE_CLOSED;
TreeView_SetItem(pNotifyStruct->hwndFrom, &(pnmtv->itemNew));
}
else
{
// Retrieve the image from the current item
pnmtv->itemNew.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
TreeView_GetItem(pNotifyStruct->hwndFrom, &(pnmtv->itemNew));
// Set the item's image to the closed folder
pnmtv->itemNew.iImage = IMAGE_OPEN;
pnmtv->itemNew.iSelectedImage = IMAGE_OPEN;
TreeView_SetItem(pNotifyStruct->hwndFrom, &(pnmtv->itemNew));
// We need to fill in the subdirectory just expanded
BuildDirectory(pnmtv->hdr.hwndFrom, pnmtv->itemNew, szDir);
GetDirectoryContents(pnmtv->hdr.hwndFrom, szDir,(HTREEITEM)pnmtv->itemNew.hItem);
}
*result = 0;
}
void CExploreFolders::OnTvnDblClk(NMHDR * pNotifyStruct, LRESULT * result ){
DWORD dwPos;
TV_HITTESTINFO tvhti;
POINT point;
HWND hwndTV; GetDlgItem(IDC_TREEVIEW, &hwndTV);
TV_ITEM tvi;
TCHAR szbufW[MAX_PATH];
TCHAR sztempW[MAX_PATH];
szbufW[0] = '\0';
sztempW[0] = '\0';
// Find out where the cursor was
dwPos = GetMessagePos();
point.x = LOWORD(dwPos);
point.y = HIWORD(dwPos);
::ScreenToClient(hwndTV, &point);
tvhti.pt = point;
tvi.hItem = TreeView_HitTest(hwndTV, &tvhti);
tvi.pszText = szbufW;
tvi.cchTextMax = MAX_PATH;
tvi.mask = TVIF_IMAGE | TVIF_HANDLE | TVIF_TEXT;
TreeView_GetItem(hwndTV, &tvi);
HTREEITEM htv;
htv = tvi.hItem;
_tcsrev(szbufW);
_tcscat(szbufW, _T("\\"));
while (htv = TreeView_GetParent(hwndTV, htv)){
//Construct path
tvi.hItem = htv;
tvi.pszText = sztempW;
tvi.cchTextMax = MAX_PATH;
tvi.mask = TVIF_TEXT;
TreeView_GetItem(hwndTV, &tvi);
_tcsrev(sztempW);
if (_tcscmp(sztempW, _T("\\")) != 0) _tcscat(szbufW, sztempW);
if (szbufW[lstrlen(szbufW)-1] != '\\') _tcscat(szbufW, _T("\\"));
};
_tcsrev(szbufW);
*result =0;
folder = szbufW;
if(folder == _T("\\\\"))
folder = _T("\\");
EndDialog(IDCHOOSE); // the user has chosen
}
void CExploreFolders::OnTvnSelChanged(NMHDR * pNotifyStruct, LRESULT * result ){
LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) pNotifyStruct;
BuildDirectory(pnmtv->hdr.hwndFrom, pnmtv->itemNew, szDir);
folder = szDir;
folder.Replace(_T("\\*.*"),_T(""));
if(folder == _T("")){
folder = _T("\\");
}
TreeView_SelectDropTarget(pnmtv->hdr.hwndFrom, pnmtv->itemNew.hItem);
}
HBRUSH CExploreFolders::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// set ppc header text color
if(pWnd->GetDlgCtrlID() == IDC_HEADER_STATIC) {
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(PPC_HEADER_TEXT_COLOR);
}
return hbr;
}
#endif
/// end PPC
void CExploreFolders::OnOK()
{
#if defined(WIN32_PLATFORM_WFSP)
BrowseFolder();
#endif
#if defined(WIN32_PLATFORM_PSPC)
EndDialog(IDCHOOSE); // the user has chosen
#endif
}
#if defined(WIN32_PLATFORM_WFSP)
void CExploreFolders::OnChoose(){
CString s1,s2;
BuildDirectory(path, s1);
lstFiles.GetText(lstFiles.GetCurSel(), s2);
if(wcscmp(s2,_T("..")) != 0){
s1+=s2;
}
if(wcscmp(s1,_T("\\\\")) == 0){
s1="\\";
}
folder = s1;
EndDialog(IDCHOOSE); // the user has chosen
}
void CExploreFolders::BrowseFolder()
{
CString s1,s2;
lstFiles.GetText(lstFiles.GetCurSel(), s1);
lstFiles.ResetContent();
if(s1 == ".."){
if(!isRoot){
path.pop_back();
}
if(path.size()==0){
isRoot = true;
s2=_T("My device");
lstFiles.ResetContent();
lstFiles.AddString(_T("\\"));
SetDlgItemText(IDC_EXPLORE_FOLDER, s2);
lstFiles.SetCurSel(0);
goto finally;
}
else{
isRoot = false;
}
}
else{
if(s1 != "\\"){
s1 += "\\";
path.push_back(s1);
isRoot=false;
};
}
if(!isRoot){
BuildDirectory(path,s1);
SetDlgItemText(IDC_EXPLORE_FOLDER, s1);
s1+="*";
GetDirectoryContents(&lstFiles, s1.GetBuffer(s1.GetLength()));
}
else{
if(path.size()==0){
path.push_back(_T("\\"));
}
s2=_T("\\");
SetDlgItemText(IDC_EXPLORE_FOLDER, s2);
s1=_T("\\*");
GetDirectoryContents(&lstFiles, s1.GetBuffer(s1.GetLength()));
isRoot = false;
}
finally:
lstFiles.SetFocus(); lstFiles.SetCurSel(0);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -