📄 textview.cpp
字号:
lst.DeleteAllItems();
}
void CTextView::OnTextEditItem()
{
// Edit the text, add the function to modify the ID
CListCtrl &ls = GetListCtrl();
POSITION pos = ls.GetFirstSelectedItemPosition();
_ASSERTE( pos != NULL );
int nSel = ls.GetNextSelectedItem(pos);
CTextExDlg dlg;
dlg.m_bCanModifyID = FALSE;
dlg.m_strID = ls.GetItemText( nSel, 0);
dlg.m_strArrLangValues.RemoveAll();
int i=0;
for(i = 1;i<=g_theApp.m_nUsedLangNum;i++)
dlg.m_strArrLangValues.Add(ls.GetItemText( nSel, i ));
dlg.m_strLangValue = dlg.m_strArrLangValues.GetAt(0);
if( dlg.DoModal() == IDOK )
{
if( g_theApp.m_MMIRes.ReplaceTextEx( dlg.m_strID, dlg.m_strArrLangValues ) )
{
for(i = 1;i<=g_theApp.m_nUsedLangNum;i++)
ls.SetItemText( nSel, i, dlg.m_strArrLangValues.GetAt(i-1) );
SetModifiedFlag();
}
else
{
AfxMessageBox(g_theApp.m_MMIRes.GetErrMsg());
}
}
}
BOOL CTextView::AddItemFromFile(LPCTSTR pszFileName)
{
_ASSERTE( pszFileName != NULL );
BOOL bAdd = FALSE;
BOOL bValid = FALSE;
CStdioFile stdFile;
try{
if( !stdFile.Open(pszFileName, CFile::modeRead) )
{
CString strMsg;
strMsg.Format(_T("open file %s fail!"), pszFileName);
AfxMessageBox(strMsg);
return FALSE;
}
WORD wNoneAscFlag = 0;
stdFile.Read(&wNoneAscFlag, sizeof(WORD));
if( wNoneAscFlag == 0xFEFF || wNoneAscFlag == 0xFFFE)
{
stdFile.Close();
CString strMsg;
strMsg.Format(_T("the file isn't a ascii type file!"), pszFileName);
AfxMessageBox(strMsg);
return FALSE;
}
stdFile.SeekToBegin();
BOOL bFlag = FALSE;
BOOL bRet = TRUE;
CString strSinglLine;
CString strMultiLine;
while( stdFile.ReadString(strSinglLine) )
{
strSinglLine.TrimLeft();
strSinglLine.TrimRight();
if( strSinglLine.IsEmpty() )
continue;
if( strSinglLine[strSinglLine.GetLength() - 1] == _T('\\') )
{
strSinglLine.TrimRight(_T('\\'));
strMultiLine += strSinglLine;
continue;
}
strMultiLine += strSinglLine;
bRet = ProcessRow(strMultiLine, bFlag, bAdd);
if( bFlag )
bValid = TRUE;
strMultiLine.Empty();
strSinglLine.Empty();
if( !bRet )
break;
}
stdFile.Close();
}
catch( CFileException * fex )
{
fex->ReportError();
fex->Delete();
stdFile.Close();
return FALSE;
}
if( !bValid && !bAdd )
{
//*@Hongliang Xin 2006-5-30 CR48325*/
AfxMessageBox(_T("This file is invalid file!"));
}
return bAdd;
}
BOOL CTextView::ProcessRow(LPCTSTR pContent, BOOL &bContainFlag, BOOL &bAdd)
{
_ASSERTE( pContent != NULL );
bContainFlag = FALSE;
static const _TCHAR SZ_FLAG[] = _T("TEXT_DEF");
LPTSTR pFlag = _tcsstr(pContent, SZ_FLAG);
if( pFlag == NULL || pFlag - pContent != 0 )
{
// 无标记或该行被注释
return TRUE;
}
bContainFlag = TRUE;
pContent += _tcslen(SZ_FLAG);
LPTSTR pSep = _tcschr(pContent, _T('('));
_ASSERTE( pSep != NULL );
pContent = pSep + 1;
// ID
pSep = _tcschr(pContent, _T(','));
_ASSERTE( pSep != NULL );
CString strID;
*pSep = 0;
strID = pContent;
pContent = pSep + 1;
*pSep = _T(',');
strID.TrimLeft();
strID.TrimRight();
CMMIRes &mmires = g_theApp.m_MMIRes;
if( mmires.TextIDIsExist(strID) )
{
CString strMsg;
strMsg.Format(_T("This file contains ID: '%s' already exist!\r\n Continue ?"), strID);
return AfxMessageBox(strMsg, MB_YESNO) == IDYES;
}
// english
pSep = _tcschr( pContent, _T('"'));
_ASSERTE( pSep != NULL );
pContent = pSep + 1;
pSep = _tcschr( pContent, _T('"'));
_ASSERTE( pSep != NULL );
CString strEng;
*pSep = 0;
strEng = pContent;
*pSep = _T('"');
pContent = pSep + 1;
// chinese
pSep = _tcschr(pContent, _T(','));
_ASSERTE( pSep != NULL );
pContent = pSep + 1;
pSep = _tcschr( pContent, _T('"'));
_ASSERTE( pSep != NULL );
pContent = pSep + 1;
pSep = _tcschr( pContent, _T('"'));
CString strChinese;
*pSep = 0;
strChinese = pContent;
*pSep = _T('"');
pContent = pSep + 1;
if( !strChinese.IsEmpty() && strChinese[0] == _T('\\') )
{
pSep = _tcsstr(pContent, _T("//"));
if( pSep != NULL )
{
pContent = pSep + 2;
strChinese = pContent;
strChinese.TrimLeft();
}
else
{
pSep = _tcsstr(pContent, _T("/*"));
if( pSep != NULL )
{
pContent = pSep + 2;
strChinese = pContent;
strChinese.TrimLeft();
strChinese.TrimRight(_T("*/"));
strChinese.TrimRight();
}
}
}
CStringArray arrValue;
arrValue.Add(strEng);
arrValue.Add(strChinese);
if( mmires.AddTextEx( strID, arrValue ) )
{
CListCtrl &ls = GetListCtrl();
int nCount = ls.GetItemCount();
ls.InsertItem( nCount, strID );
ls.SetItemText(nCount, 1, strEng);
ls.SetItemText(nCount, 2, strChinese);
CDirFileNode * pRoot = (CDirFileNode *)m_pInfo->GetRoot();
CDirFileNode * pChild = (CDirFileNode *)m_pInfo->MallocNode();
if( NULL == pChild )
return FALSE;
_tcscpy( pChild->szID, strID );
m_pInfo->AddChild( pRoot, pChild );
ls.SetItemData(nCount, (DWORD)pChild);
bAdd = TRUE;
return TRUE;
}
return FALSE;
}
void CTextView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
LPNMITEMACTIVATE pnia = (LPNMITEMACTIVATE)pNMHDR;
if( pnia->iItem != -1 )
{
OnTextEditItem();
}
*pResult = 0;
}
void CTextView::OnUpdateTextAddFile(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CTextView::OnUpdateTextAddItem(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CTextView::OnUpdateTextDelItem(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CListCtrl &ls = GetListCtrl();
pCmdUI->Enable( ls.GetSelectedCount() > 0 );
}
void CTextView::OnUpdateTextEditItem(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CListCtrl &ls = GetListCtrl();
pCmdUI->Enable( ls.GetSelectedCount() == 1 );
}
void CTextView::OnTextFindItem()
{
// TODO: Add your command handler code here
CListCtrl &lst = GetListCtrl();
POSITION pos = lst.GetFirstSelectedItemPosition();
while( pos != NULL )
{
int nSel = lst.GetNextSelectedItem(pos);
lst.SetItemState(nSel, ~LVIS_SELECTED, LVIS_SELECTED);
}
m_FindInfo.strFind.Empty();
CFindDlg dlg;
dlg.SetLstCtrl( &lst );
if( dlg.DoModal() == IDOK )
{
m_FindInfo.bMatchCase = dlg.m_bCase;
m_FindInfo.bMatchWhole = dlg.m_bWhole;
m_FindInfo.nCurIdx = 0;
m_FindInfo.strFind = dlg.m_strFind;
CDocument * pDoc = GetDocument();
ASSERT_VALID( pDoc );
CString strTxt;
strTxt.Format( _T("Seaching for '%s'..."), dlg.m_strFind );
OUT_BAR_PARAM obp;
obp.arrTxt.Add(strTxt);
pDoc->UpdateAllViews(this, WM_OUTBAR_INSERT_CLMN, (CObject *)&obp);
DoFindAct();
}
}
void CTextView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CListView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CTextView::DoFindAct()
{
CListCtrl &lst = GetListCtrl();
int nCount = lst.GetItemCount();
_ASSERTE( nCount > 0 );
CHeaderCtrl * pHdrCtrl = lst.GetHeaderCtrl();
_ASSERTE( pHdrCtrl != NULL );
int nClmnCount = pHdrCtrl->GetItemCount();
_ASSERTE( nClmnCount > 0 );
BOOL bNoFind = TRUE;
BOOL bFind = FALSE;
CString strFind = m_FindInfo.strFind;
CString strTxt;
for( int i = 0; i < nCount; ++i )
{
for( int j = 0; j < nClmnCount; ++j )
{
strTxt = lst.GetItemText(i, j);
if( m_FindInfo.bMatchWhole )
{
if( m_FindInfo.bMatchCase )
{
bFind = ( 0 == strTxt.Compare(strFind) );
}
else
{
bFind = ( 0 == strTxt.CompareNoCase(strFind) );
}
}
else
{
if( !m_FindInfo.bMatchCase )
{
strTxt.MakeLower();
strFind.MakeLower();
}
bFind = ( strTxt.Find(strFind, 0) != -1 );
}
if( bFind )
{
bNoFind = FALSE;
CDocument * pDoc = GetDocument();
ASSERT_VALID( pDoc );
OUT_BAR_PARAM obp;
obp.dwData = i;
obp.arrTxt.Add( lst.GetItemText(i, j) );
pDoc->UpdateAllViews(this, WM_OUTBAR_ADD_ITEM, (CObject *)&obp);
break;
}
}
}
if( bNoFind )
{
CString strMsg;
strMsg.Format(_T("Can not find the string '%s'."), m_FindInfo.strFind);
AfxMessageBox(strMsg);
}
else
{
CWnd * pFrame = (CWnd *)GetParentFrame();
_ASSERTE( pFrame != NULL );
pFrame->SendMessage(WM_SHOW_OUTBAR, SHOW_OUTBAR, 0);
}
}
//DEL void CTextView::OnTextSavetoFile()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL CFileDlg dlg(FALSE);
//DEL dlg.SetFileExt(_T(".def"));
//DEL dlg.SetFileFilter(_T("text define files (*.def)|*.def|All Files (*.*)|*.*||"));
//DEL
//DEL CString strFileName = dlg.GetPathName();
//DEL if( strFileName.IsEmpty() )
//DEL return;
//DEL
//DEL CWaitCursor waitCur;
//DEL
//DEL SaveTxtToFile(strFileName);
//DEL }
void CTextView::OnUpdateTextSavetoFile(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CListCtrl &ls = GetListCtrl();
pCmdUI->Enable( ls.GetSelectedCount() > 0 );
}
BOOL CTextView::SaveTxtToFile(LPCTSTR pszFileName)
{
_ASSERTE( pszFileName != NULL );
CListCtrl &lst = GetListCtrl();
int nCount = lst.GetItemCount();
if( nCount == 0 )
return TRUE;
CStdioFile stdFile;
try{
if( !stdFile.Open(pszFileName, CFile::modeCreate | CFile::modeWrite) )
{
CString strMsg;
strMsg.Format(_T("create file %s fail!"), pszFileName);
AfxMessageBox(strMsg);
return FALSE;
}
CString strFmt = _T("TEXT_DEF (%s, \"%s\", \"\\\") // ");
CString strLine;
for( int i = 0; i < nCount; ++i )
{
strLine.Format( strFmt, lst.GetItemText(i, 0), lst.GetItemText(i, 1) );
stdFile.WriteString(strLine);
strLine = lst.GetItemText(i, 2);
stdFile.WriteString(strLine);
stdFile.WriteString(_T("\n"));
}
stdFile.Close();
}
catch( CFileException * fex )
{
fex->ReportError();
fex->Delete();
stdFile.Close();
return FALSE;
}
return TRUE;
}
void CTextView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -