📄 commands.cpp
字号:
// else delete the file first
DeleteFile(lpszOutputFile);
}
CZipper zip;
TCHAR szRootPath[_MAX_PATH];
lstrcpy(szRootPath,lpszFolder);
int nLen = lstrlen(szRootPath);
if(szRootPath[nLen - 1] != _T('\\'))
lstrcat(szRootPath,_T("\\"));
if(zip.OpenZip(lpszOutputFile, szRootPath))
{
bRtn = ZipFolder(zip,szRootPath,lpszFileTypes);
zip.CloseZip();
}
if(bRtn)
{
}
else
{
}
return bRtn;
}
void CCommands::OpenZippedWorkspace()
{
CFileDialog dialog(TRUE, _T("zip"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST, "Zip Files (*.zip)|*.zip||");
#ifdef _VER_CHINESE
dialog.m_ofn.lpstrTitle = _T("打开工程 - 打开Zip文件");
#else
dialog.m_ofn.lpstrTitle = _T("Load Project - Open Zip File");
#endif
if(dialog.DoModal() == IDOK)
{
CString strZipFile = dialog.GetPathName();
// append the zip's filetitle
CString strName;
CString strPath;
int index = strZipFile.ReverseFind(_T('\\'));
ASSERT(index != -1);
strPath = strZipFile.Left(index + 1);//include '\'
int index2 = strZipFile.ReverseFind(_T('.'));
ASSERT(index2 != -1);
strName = strZipFile.Mid(index + 1,index2 - index - 1);//not include ext name
CIni ini(g_szIniPathName);
CString strUnzipPath;
ini.GetValue(lpszZip,_T("sUnzipPath"),strUnzipPath);// no '\'
//check the folder
if(strUnzipPath.IsEmpty() || (GetFileAttributes(strUnzipPath) == 0xffffffff))//not assign unzip path or path is not validated now
{
CDirDialog dlg;
#ifdef _VER_CHINESE
if(dlg.DoBrowse(NULL,_T("选择解压缩目录"),lpszNull,strPath) == 1)
#else
if(dlg.DoBrowse(NULL,_T("Select Folder to Extract To"),lpszNull,strPath) == 1)
#endif
{
strUnzipPath = dlg.m_strPath;// no '\'
}
else//cancel by user
return;
}
//folder name ok!
strUnzipPath += _T('\\');
strUnzipPath += strName;
if(GetFileAttributes(strUnzipPath) != 0xffffffff)//this folder exist
{
CString sMessage;
#ifdef _VER_CHINESE
sMessage.Format(_T("目录 '%s' 已经存在.\n\n是否要覆盖该目录下的文件?"), strUnzipPath);
#else
sMessage.Format(_T("The folder '%s' already exists.\n\nAre you sure that you want to overwrite it and the files it contains?"), strUnzipPath);
#endif
if(MessageBox(NULL, sMessage, lpszMsgBoxTitle, MB_YESNO) == IDNO)
return;
}
//all check over,no unzip file
CUnzipper uzip;
CWaitCursor cursor;
if(uzip.OpenZip(strZipFile) && uzip.UnzipTo(strUnzipPath))
{
cursor.Restore();
CStringArray aDswFiles;
int nDswFile = FindWorkspaceFiles(uzip, strUnzipPath, aDswFiles);//find dsw file
CString sProjectFile;
if(nDswFile == 0)//not find dsw file
{
CString sMessage;
#ifdef _VER_CHINESE
sMessage.Format(_T("文件 '%s' 不含VC6的工作区文件.\n\n是否要浏览全部文件查找工程文件(dsp)?"), strZipFile);
#else
sMessage.Format(_T("The file '%s' does not appear to contain a project file.\n\nWould you like to browse the extracted files for dsp file?"), strZipFile);
#endif
if(MessageBox(NULL, sMessage, lpszMsgBoxTitle, MB_YESNO) == IDNO)
return;
// show open dialog
CFileDialog dialog(TRUE, NULL, strUnzipPath + _T("\\."), 0, _T("Project Files (*.dsw, *.dsp)|*.dsw;*.dsp||"));
#ifdef _VER_CHINESE
dialog.m_ofn.lpstrTitle = _T("打开工程 - 打开Zip文件");
#else
dialog.m_ofn.lpstrTitle = _T("Load Project - Open Zip File");
#endif
if(dialog.DoModal() == IDOK)
sProjectFile = dialog.GetPathName();
}
else // open it
{
ASSERT(nDswFile == aDswFiles.GetSize());
if(nDswFile == 1)
sProjectFile = aDswFiles[0];
else
{
CSelectDswDlg dialog(aDswFiles);
if(dialog.DoModal() == IDOK)
sProjectFile = dialog.GetPathName();
}
}
// here sProjectFile is ok,open the file
if(!sProjectFile.IsEmpty())
{
IDocuments* pDocs = NULL;
if (SUCCEEDED(m_pApplication->get_Documents((IDispatch**)&pDocs)))
{
IGenericDocument* pDoc = NULL;
CComBSTR bstrPrjFile = sProjectFile;
HRESULT hr = pDocs->Open(bstrPrjFile, CComVariant(_T("Auto")), CComVariant(VARIANT_FALSE), (IDispatch**)&pDoc);
pDocs->Release();
if (FAILED(hr))
{
CString sMessage;
#ifdef _VER_CHINESE
sMessage.Format(_T("无法打开工作区或工程 '%s' ."), sProjectFile);
#else
sMessage.Format(_T("The project '%s' could not be opened."), sProjectFile);
#endif
MessageBox(NULL, sMessage, lpszMsgBoxTitle, MB_OK);
}
}
}
uzip.CloseZip();
}
}
}
#ifdef _VER_CHINESE
LPCTSTR lpszPlotterError = _T("此功能只适用于文本窗口");
#else
LPCTSTR lpszPlotterError = _T("Please open a text document and try again");
#endif
void CCommands::DoDiagram()
{
IDocuments* pDocs = NULL;
if (SUCCEEDED(m_pApplication->get_Documents((IDispatch**)&pDocs)) && pDocs)
{
long lCount = 0;
if (SUCCEEDED(pDocs->get_Count(&lCount)) && lCount)
{
// make sure the current document is text
ITextDocument* pDoc = NULL;
if (SUCCEEDED(m_pApplication->get_ActiveDocument((IDispatch**)&pDoc)) && pDoc)
{
CComBSTR bsType;
if (SUCCEEDED(pDoc->get_Type(&bsType)))
{
CString sType = bsType;
if (sType == _T("Text"))
{
// search back from the current selection looking for the start of a diagram
ITextSelection* pSelection = NULL;
if (SUCCEEDED(pDoc->get_Selection((IDispatch**)&pSelection)) && pSelection)
{
// save the current insertion point
int nLineSel = 0;
VERIFY(SUCCEEDED(pSelection->get_TopLine((long*)&nLineSel)));
// select all
pSelection->Cancel();
pSelection->SelectAll();
// get the text for the entire document and locate the diagram at the cursor pos
CComBSTR bsText;
CString sText;
BOOL bContinue = TRUE;
BOOL bGotText = SUCCEEDED(pSelection->get_Text(&bsText));
BOOL bNew = TRUE;
// move the selection back to where it was originally
pSelection->GoToLine((long)nLineSel, CComVariant(dsMove));
if (bGotText)
{
bNew = !FindDiagram(CString(bsText), nLineSel);
}
else
{
// TODO
bContinue = FALSE;
}
if (bContinue)
{
CString sDiagram;
// if we are editing then select the diagram (so we can later replace it)
if (!bNew)
{
VARIANT_BOOL bFoundStart(FALSE), bFoundEnd(FALSE);
// end then start so we finish where we want to start selecting
pSelection->FindText(BDIAGRAMEND, CComVariant(dsMatchForward), &bFoundEnd);
pSelection->FindText(BDIAGRAMSTART, CComVariant(dsMatchBackward), &bFoundStart);
ASSERT (bFoundStart && bFoundEnd);
if (bFoundStart && bFoundEnd)
{
// go to start of line
pSelection->StartOfLine(CComVariant(dsMove), CComVariant(1));
do
{
// walk the text line by line until we find the end marker
pSelection->LineDown(CComVariant(dsExtend), CComVariant(1));
CComBSTR bsText;
CString sPrevText(sDiagram);
if (SUCCEEDED(pSelection->get_Text(&bsText)))
sDiagram = bsText;
else
break;
// if no change then break
if (sPrevText == sDiagram)
break;
}
while (sDiagram.Find(DIAGRAMEND) == -1);
if (sDiagram.Find(DIAGRAMEND) != -1)
{
// remove the start and end lines
int nFindStart = sDiagram.Find("\r\n");
ASSERT (nFindStart != -1);
int nFindEnd = sDiagram.ReverseFind('\r');
ASSERT (nFindEnd != -1);
sDiagram = sDiagram.Mid(nFindStart + 2, nFindEnd - (nFindStart + 2));
// and all instances of '//'
sDiagram.Replace("//", "");
}
}
}
if (EditDiagram(sDiagram) && (!bNew || !sDiagram.IsEmpty()))
{
// add back the comments
CString sTemp(sDiagram);
sDiagram.Format(_T("//%s\r\n%s\r\n%s"), DIAGRAMSTART, sDiagram, DIAGRAMEND);
sDiagram.Replace(_T("\r\n"), _T("\r\n//"));
// make sure there is a trailing CR
sDiagram += "\r\n";
// replace the selection
pSelection->put_Text(CComBSTR(sDiagram));
}
else
pSelection->Cancel();
}
pSelection->Release();
}
}
else
MessageBox(NULL, lpszPlotterError, lpszMsgBoxTitle, MB_OK);
}
else
MessageBox(NULL, lpszPlotterError, lpszMsgBoxTitle, MB_OK);
pDoc->Release();
}
else
MessageBox(NULL, lpszPlotterError, lpszMsgBoxTitle, MB_OK);
}
else
MessageBox(NULL, lpszPlotterError, lpszMsgBoxTitle, MB_OK);
pDocs->Release();
}
else
MessageBox(NULL, lpszPlotterError, lpszMsgBoxTitle, MB_OK);
}
//this function copy from codeplotter add_in
BOOL CCommands::FindDiagram(const CString& sText, int nLineSel)
{
// search the text looking for a diagram which straddles nLineSel line by line
int nLineStart = 0, nLineEnd = sText.Find("\r\n");
int nLine = 1; // vc line numbers start at 1
int nFindStart = -1, nFindEnd = -1;
while (nLineEnd != -1)
{
CString sLine = sText.Mid(nLineStart, (nLineEnd - nLineStart));
if (nFindStart == -1 && sLine.Find(DIAGRAMSTART) > 0)
nFindStart = nLine;
else if (nFindStart != -1 && sLine.Find(DIAGRAMEND) > 0)
nFindEnd = nLine;
if (nFindStart != -1 && nFindEnd != -1)
{
if (nLineSel >= nFindStart && nLineSel <= nFindEnd)
return TRUE;
// else
nFindStart = nFindEnd = -1;
}
// next line
nLineStart = nLineEnd + 2;
nLineEnd = sText.Find("\r\n", nLineEnd + 2);
nLine++;
}
return FALSE;
}
//this function copy from codeplotter add_in
BOOL CCommands::EditDiagram(CString& sDiagram)
{
CDiagramDlg dlg(sDiagram);
if(dlg.DoModal() == IDOK)
{
return dlg.GetDiagram(sDiagram);
}
return FALSE;
}
int FindWorkspaceFiles(CUnzipper& uzip, LPCTSTR szFolder, CStringArray& aFiles)
{
LPCTSTR lpszExt = _T("dsw");
if (uzip.GotoFirstFile(lpszExt))
{
do
{
UZ_FileInfo fi;
if (uzip.GetFileInfo(fi) && !fi.bFolder)
{
char szFullPath[MAX_PATH];
_makepath(szFullPath, NULL, szFolder, fi.szFileName, NULL);
aFiles.Add(szFullPath);
}
}
while (uzip.GotoNextFile(lpszExt));
}
return aFiles.GetSize();
}
BOOL ZipFolder(CZipper & zip,LPCTSTR lpszFolder,LPCTSTR lpszTypes,LPZIPCALLBACK lpCallBack)
{
TCHAR szRootPath[_MAX_PATH];
_tcscpy(szRootPath,lpszFolder);
_tcscat(szRootPath,_T("*.*"));
WIN32_FIND_DATA data;
HANDLE hFindFile = FindFirstFile(szRootPath,&data);
BOOL bSuccess = FALSE;
if(hFindFile != INVALID_HANDLE_VALUE)
{
bSuccess = TRUE;
TCHAR szSubRootName[_MAX_PATH];
_tcscpy(szSubRootName,lpszFolder);
do
{
_tcscpy(szSubRootName,lpszFolder);
_tcscat(szSubRootName,data.cFileName);
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
if((_tcscmp(data.cFileName,_T(".")) == 0) || (_tcscmp(data.cFileName,_T("..")) == 0))
continue;//skip it
_tcscat(szSubRootName,_T("\\"));
bSuccess = ZipFolder(zip,szSubRootName,lpszTypes,lpCallBack);
}
else
{
TCHAR szExt[_MAX_EXT];
LPTSTR lpDot = _tcsrchr(data.cFileName,_T('.'));//get file ext name
if(lpDot != NULL)
{
_tcscpy(szExt,lpDot);
if(_tcsstr(lpszTypes,szExt) != NULL)//find this type in exclude type list
continue;
}
bSuccess = zip.AddFileToZip(szSubRootName) ? TRUE : FALSE;//add to zip file
}
if(lpCallBack)//call callback function if it not NULL
lpCallBack(bSuccess,szSubRootName);
if(!bSuccess)//stop if any error occur
break;
}while(FindNextFile(hFindFile,&data));
FindClose(hFindFile);
}
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -