📄 filewindowproject.cpp
字号:
#include "stdafx.h"
#include "cedtHeader.h"
BOOL CFileWindow::InitProjectWorkspace()
{
if( ! RemoveAllProjectItems() ) return FALSE;
CString szItemText; szItemText.LoadString(IDS_MENU_NO_PROJECT_AVAILABLE);
if( ! InsertProjectItem(TVI_ROOT, szItemText, PROJECT_ITEM_PROJECT, 0, "", 0) ) return FALSE;
if( ! EnableAllProjectButtons(FALSE) ) return FALSE;
return TRUE;
}
BOOL CFileWindow::NewProjectWorkspace(LPCTSTR lpszPathName)
{
if( ! RemoveAllProjectItems() ) return FALSE;
if( ! InsertProjectItem(TVI_ROOT, GetFileName(lpszPathName), PROJECT_ITEM_PROJECT, 0, lpszPathName, 0) ) return FALSE;
if( ! EnableAllProjectButtons(TRUE) ) return FALSE;
return TRUE;
}
BOOL CFileWindow::SaveProjectWorkspace(LPCTSTR lpszPathName)
{
ofstream fout(lpszPathName, ios::out);
if( ! fout.is_open() ) return FALSE;
CString szContents;
// save project
szContents.Format("<project version=\"%s\">", STRING_PROJECTFILEVER);
fout << szContents << endl;
HTREEITEM hRoot = m_treProjectTree.GetRootItem();
HTREEITEM hChild = m_treProjectTree.GetChildItem( hRoot );
while( hChild ) { // recursive call to all child items
if( ! SaveProjectItem(fout, 1, hChild) ) return FALSE;
hChild = m_treProjectTree.GetNextSiblingItem( hChild );
}
fout << "</project>" << endl << endl;
// save workspace
szContents.Format("<workspace version=\"%s\">", STRING_PROJECTFILEVER);
fout << szContents << endl;
CCedtApp * pApp = (CCedtApp *)AfxGetApp();
POSITION posDoc = pApp->GetFirstDocPosition();
while( posDoc ) { // recursive call to all open documents
CCedtDoc * pDoc = (CCedtDoc *)pApp->GetNextDoc( posDoc );
if( ! SaveWorkspaceItem(fout, 1, pDoc) ) return FALSE;
}
fout << "</workspace>" << endl << endl;
fout.close();
return TRUE;
}
BOOL CFileWindow::OpenProjectWorkspace(LPCTSTR lpszPathName)
{
if( ! RemoveAllProjectItems() ) return FALSE;
ifstream fin(lpszPathName, ios::in | ios::nocreate);
CMapStringToString mapAttr; TCHAR szText[4096];
fin >> szText; // get first tocken
// load project
if( ! _stricmp(szText, "<project") ) {
fin.getline(szText, 4096, '>'); // get attributes
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szVersion; BOOL bLookup = mapAttr.Lookup("version", szVersion);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
HTREEITEM hItem = InsertProjectItem(TVI_ROOT, GetFileName(lpszPathName), PROJECT_ITEM_PROJECT, 0, lpszPathName, 0);
fin >> szText; // get next tocken
while( _stricmp(szText, "</project>") ) {
if( ! LoadProjectItem(fin, szText, hItem) ) return FALSE;
}
fin >> szText; // get next tocken
// expand and select root item
if( ! m_treProjectTree.Expand( hItem, TVE_EXPAND ) ) ; // there could be only one root item;
if( ! m_treProjectTree.SelectItem( hItem ) ) return FALSE;
} else { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
// load workspace
if( ! _stricmp(szText, "<workspace") ) {
fin.getline(szText, 4096, '>'); // get attributes
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szVersion; BOOL bLookup = mapAttr.Lookup("version", szVersion);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
CCedtApp * pApp = (CCedtApp *)AfxGetApp();
fin >> szText; // get next tocken
while( _stricmp(szText, "</workspace>") ) {
if( ! LoadWorkspaceItem(fin, szText, pApp) ) return FALSE;
}
fin >> szText; // get next tocken
} else { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
if( ! EnableAllProjectButtons(TRUE) ) return FALSE;
fin.close();
return TRUE;
}
BOOL CFileWindow::SaveRegularWorkspace(LPCTSTR lpszPathName)
{
ofstream fout(lpszPathName, ios::out);
if( ! fout.is_open() ) return FALSE;
CString szContents;
// save workspace
szContents.Format("<workspace version=\"%s\">", STRING_PROJECTFILEVER);
fout << szContents << endl;
CCedtApp * pApp = (CCedtApp *)AfxGetApp();
POSITION posDoc = pApp->GetFirstDocPosition();
while( posDoc ) { // recursive call to all open documents
CCedtDoc * pDoc = (CCedtDoc *)pApp->GetNextDoc( posDoc );
if( ! SaveWorkspaceItem(fout, 1, pDoc) ) return FALSE;
}
fout << "</workspace>" << endl << endl;
fout.close();
return TRUE;
}
BOOL CFileWindow::OpenRegularWorkspace(LPCTSTR lpszPathName)
{
if( ! RemoveAllProjectItems() ) return FALSE;
ifstream fin(lpszPathName, ios::in | ios::nocreate);
CMapStringToString mapAttr; TCHAR szText[4096];
fin >> szText; // get first tocken
// load workspace
if( ! _stricmp(szText, "<workspace") ) {
fin.getline(szText, 4096, '>'); // get attributes
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szVersion; BOOL bLookup = mapAttr.Lookup("version", szVersion);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
CCedtApp * pApp = (CCedtApp *)AfxGetApp();
fin >> szText; // get next tocken
while( _stricmp(szText, "</workspace>") ) {
if( ! LoadWorkspaceItem(fin, szText, pApp) ) return FALSE;
}
fin >> szText; // get next tocken
} else { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
if( ! EnableAllProjectButtons(FALSE) ) return FALSE;
fin.close();
return TRUE;
}
BOOL CFileWindow::AddCategoryToProject(LPCTSTR lpszCategory)
{
HTREEITEM hParent = m_treProjectTree.GetSelectedItem();
if( ! hParent ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
// if selected item is not a category, then get parent item
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hParent, nImage, nSelectedImage);
if( nImage != PROJECT_ITEM_PROJECT && nImage != PROJECT_ITEM_CATEGORY ) hParent = m_treProjectTree.GetParentItem(hParent);
HTREEITEM hFound = FindChildProjectItem(hParent, lpszCategory);
if( hFound ) { AfxMessageBox(IDS_ERR_DUPLICATE_PRJ_ITEM); return FALSE; }
HTREEITEM hInsert = InsertProjectItem(hParent, lpszCategory, PROJECT_ITEM_CATEGORY, 0, "", 0);
if( ! hInsert ) { AfxMessageBox(IDS_ERR_INSERT_PRJ_ITEM_FAILED); return FALSE; }
// expand parent category item
if( ! m_treProjectTree.Expand( hParent, TVE_EXPAND ) ) return FALSE;
return TRUE;
}
BOOL CFileWindow::AddLocalFileToProject(LPCTSTR lpszPathName)
{
HTREEITEM hParent = m_treProjectTree.GetSelectedItem();
if( ! hParent ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
// if selected item is not a category, then get parent item
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hParent, nImage, nSelectedImage);
if( nImage != PROJECT_ITEM_PROJECT && nImage != PROJECT_ITEM_CATEGORY ) hParent = m_treProjectTree.GetParentItem(hParent);
HTREEITEM hFound = FindChildProjectItem(hParent, GetFileName(lpszPathName));
if( hFound ) { /* AfxMessageBox(IDS_ERR_DUPLICATE_PROJECT_ITEM); */ return FALSE; }
HTREEITEM hInsert = InsertProjectItem(hParent, GetFileName(lpszPathName), PROJECT_ITEM_LOCAL_FILE, 0, lpszPathName, 0);
if( ! hInsert ) { AfxMessageBox(IDS_ERR_INSERT_PRJ_ITEM_FAILED); return FALSE; }
// expand parent category item
if( ! m_treProjectTree.Expand( hParent, TVE_EXPAND ) ) return FALSE;
return TRUE;
}
BOOL CFileWindow::AddRemoteFileToProject(INT nAccount, LPCTSTR lpszPathName, DWORD dwSize)
{
HTREEITEM hParent = m_treProjectTree.GetSelectedItem();
if( ! hParent ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
// if selected item is not a category, then get parent item
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hParent, nImage, nSelectedImage);
if( nImage != PROJECT_ITEM_PROJECT && nImage != PROJECT_ITEM_CATEGORY ) hParent = m_treProjectTree.GetParentItem(hParent);
HTREEITEM hFound = FindChildProjectItem(hParent, GetFileName(lpszPathName));
if( hFound ) { /* AfxMessageBox(IDS_ERR_DUPLICATE_PROJECT_ITEM); */ return FALSE; }
HTREEITEM hInsert = InsertProjectItem(hParent, GetFileName(lpszPathName), PROJECT_ITEM_REMOTE_FILE, nAccount, lpszPathName, dwSize);
if( ! hInsert ) { AfxMessageBox(IDS_ERR_INSERT_PRJ_ITEM_FAILED); return FALSE; }
// expand parent category item
if( ! m_treProjectTree.Expand( hParent, TVE_EXPAND ) ) return FALSE;
return TRUE;
}
BOOL CFileWindow::OpenProjectItem()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
if( ! hItem ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hItem, nImage, nSelectedImage);
if( nImage != PROJECT_ITEM_LOCAL_FILE && nImage != PROJECT_ITEM_REMOTE_FILE ) return FALSE;
return OpenProjectItem(hItem);
}
BOOL CFileWindow::ExecuteProjectItem()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
if( ! hItem ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hItem, nImage, nSelectedImage);
if( nImage != PROJECT_ITEM_LOCAL_FILE ) return FALSE;
return ExecuteProjectItem(hItem);
}
BOOL CFileWindow::RenameProjectItem()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
if( ! hItem ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hItem, nImage, nSelectedImage);
if( nImage == PROJECT_ITEM_PROJECT ) return FALSE;
CEdit * pEdit = m_treProjectTree.EditLabel(hItem);
TRACE0("Begin EditLabel\n");
return (pEdit != NULL);
}
BOOL CFileWindow::RemoveProjectItem()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
if( ! hItem ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return FALSE; }
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage(hItem, nImage, nSelectedImage);
if( nImage == PROJECT_ITEM_PROJECT ) return FALSE;
return RemoveProjectItem(hItem);
}
BOOL CFileWindow::IsSelectedProjectItemRoot()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
HTREEITEM hRoot = m_treProjectTree.GetRootItem();
return (hItem == hRoot);
}
CString CFileWindow::GetSelectedProjectItemText()
{
HTREEITEM hItem = m_treProjectTree.GetSelectedItem();
if( ! hItem ) { AfxMessageBox(IDS_ERR_NO_PRJ_ITEM_SELECTED); return ""; }
return m_treProjectTree.GetItemText(hItem);
}
/////////////////////////////////////////////////////////////////////////////
// Inner Functions
BOOL CFileWindow::SaveProjectItem(ostream & os, INT nLevel, HTREEITEM hItem)
{
INT nImage, nSelectedImage; m_treProjectTree.GetItemImage( hItem, nImage, nSelectedImage );
CString szContents, szIndent('\t', nLevel), szText = m_treProjectTree.GetItemText( hItem );
CString szExpanded = (m_treProjectTree.GetItemState(hItem, TVIS_EXPANDED) & TVIS_EXPANDED) ? "yes" : "no";
if( nImage == PROJECT_ITEM_CATEGORY ) {
szContents.Format("<category name=\"%s\" expanded=\"%s\">", szText, szExpanded);
os << szIndent << szContents << endl;
HTREEITEM hChild = m_treProjectTree.GetChildItem( hItem );
while( hChild ) { // recursive call to all child items
SaveProjectItem(os, nLevel+1, hChild);
hChild = m_treProjectTree.GetNextSiblingItem( hChild );
}
os << szIndent << "</category>" << endl;
} else if( nImage == PROJECT_ITEM_LOCAL_FILE ) {
CString szPath = GetProjectItemPathName( hItem );
szContents.Format("<localfile path=\"%s\" />", szPath);
os << szIndent << szContents << endl;
} else if( nImage == PROJECT_ITEM_REMOTE_FILE ) {
LPPROJECTITEMINFO lpInfo = GetProjectItemInfo( hItem );
INT nAccount = lpInfo->nFtpAccount;
CString szPath = lpInfo->szPathName;
LONG nFileSize = lpInfo->nFileSize;
szContents.Format("<remotefile account=\"%d\" path=\"%s\" filesize=\"%d\" />", nAccount, szPath, nFileSize);
os << szIndent << szContents << endl;
} else return FALSE;
return TRUE;
}
BOOL CFileWindow::LoadProjectItem(istream & is, TCHAR szText[], HTREEITEM hParent)
{
CMapStringToString mapAttr;
if( ! _stricmp(szText, "<category") ) {
is.getline(szText, 4096, '>'); // get attributes
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szName; BOOL bLookup = mapAttr.Lookup("name", szName);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
CString szExpanded; bLookup = mapAttr.Lookup("expanded", szExpanded);
if( ! bLookup ) { szExpanded = "no"; }
HTREEITEM hItem = InsertProjectItem(hParent, szName, PROJECT_ITEM_CATEGORY, 0, "", 0);
is >> szText; // get next tocken
while( _stricmp(szText, "</category>") ) {
if( ! LoadProjectItem(is, szText, hItem) ) return FALSE;
}
is >> szText; // get next tocken
// expand category item if it is checked
if( ! szExpanded.Compare("yes") ) m_treProjectTree.Expand( hItem, TVE_EXPAND );
} else if( ! _stricmp(szText, "<localfile") ) {
is.getline(szText, 4096, '>'); // get attributes
INT nLen = strlen(szText); if( szText[nLen-1] == '/' ) szText[nLen-1] = '\0';
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szPath; BOOL bLookup = mapAttr.Lookup("path", szPath);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
HTREEITEM hItem = InsertProjectItem(hParent, GetFileName(szPath), PROJECT_ITEM_LOCAL_FILE, 0, szPath, 0);
is >> szText; // get next tocken
} else if( ! _stricmp(szText, "<remotefile") ) {
is.getline(szText, 4096, '>'); // get attributes
INT nLen = strlen(szText); if( szText[nLen-1] == '/' ) szText[nLen-1] = '\0';
if( ! ParseProjectItemAttr( szText, mapAttr ) ) return FALSE;
CString szAccount; BOOL bLookup = mapAttr.Lookup("account", szAccount);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
CString szPath; bLookup = mapAttr.Lookup("path", szPath);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
CString szFileSize; bLookup = mapAttr.Lookup("filesize", szFileSize);
if( ! bLookup ) { AfxMessageBox(IDS_ERR_WRONG_PRJ_FILE); return FALSE; }
HTREEITEM hItem = InsertProjectItem(hParent, GetFileName(szPath), PROJECT_ITEM_REMOTE_FILE, atoi(szAccount), szPath, atol(szFileSize));
is >> szText; // get next tocken
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -