// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "icrEdit.h" #include "MainFrm.h" #include "IcrEditView.h" #include "resource.h" #include "..\include\kernel.h" #include "Const.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif DWORD CALLBACK EditStreamCallbackWriteToFile(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb); DWORD CALLBACK EditStreamCallbackOut(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb); ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() ON_CBN_SELCHANGE(IDC_COMBOX, OnSelchangeComboFind) ON_COMMAND(ID_EDITPASTE, OnEditPaste) ON_COMMAND(ID_EDITCUT, OnEditcut) ON_UPDATE_COMMAND_UI(ID_EDITCUT, OnUpdateEditcut) ON_COMMAND(ID_EDITCOPY, OnEditcopy) ON_UPDATE_COMMAND_UI(ID_EDITCOPY, OnUpdateEditcopy) ON_UPDATE_COMMAND_UI(ID_EDITPASTE, OnUpdateEditpaste) ON_COMMAND(ID_EDITUNDO, OnEditundo) ON_UPDATE_COMMAND_UI(ID_EDITUNDO, OnUpdateEditundo) ON_COMMAND(ID_EDITREDO, OnEditredo) ON_UPDATE_COMMAND_UI(ID_EDITREDO, OnUpdateEditredo) ON_COMMAND(ID_EDITSELECTALL, OnEditselectall) ON_COMMAND(ID_EDITREPEAT, OnEditrepeat) ON_COMMAND(ID_EDITREPEATNEXT, OnEditrepeatnext) ON_UPDATE_COMMAND_UI(ID_EDITREPEAT, OnUpdateEditrepeat) ON_UPDATE_COMMAND_UI(ID_EDITREPEATNEXT, OnUpdateEditrepeatnext) ON_COMMAND(ID_VIEW_FONT, OnViewFont) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) ON_COMMAND(ID_EDIT_TRIMTRAILLINGSPACE, OnEditTrimtraillingspace) ON_COMMAND(ID_EDIT_TRIMTRAILLINGTAB, OnEditTrimtraillingtab) ON_COMMAND(ID_EDIT_TABTOSPACE, OnEditTabtospace) ON_COMMAND(ID_EDIT_SPACETOTAB, OnEditSpacetotab) ON_COMMAND(ID_EDIT_LEADINGSPACETOTAB, OnEditLeadingspacetotab) ON_COMMAND(ID_EDIT_MOVERIGHT, OnEditMoveright) ON_COMMAND(ID_EDIT_UNCOMMENT, OnEditUncomment) ON_COMMAND(ID_EDIT_MOVELEFT, OnEditMoveleft) ON_COMMAND(ID_EDIT_COMMENT, OnEditComment) ON_COMMAND(ID_EDIT_LCASE, OnEditLcase) ON_COMMAND(ID_EDIT_UCASE, OnEditUcase) ON_COMMAND(ID_EDIT_INVERT, OnEditInvert) ON_COMMAND(ID_EDIT_CAPITALIZE, OnEditCapitalize) ON_COMMAND(ID_EDIT_SENTANCECASE, OnEditSentancecase) ON_UPDATE_COMMAND_UI(ID_EDIT_LCASE, OnUpdateEditLcase) ON_UPDATE_COMMAND_UI(ID_EDIT_INVERT, OnUpdateEditInvert) ON_UPDATE_COMMAND_UI(ID_EDIT_SENTANCECASE, OnUpdateEditSentancecase) ON_UPDATE_COMMAND_UI(ID_EDIT_UCASE, OnUpdateEditUcase) ON_COMMAND(ID_FILE_NEW, OnFileNew) ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) ON_COMMAND(ID_FILESAVE, OnFilesave) ON_UPDATE_COMMAND_UI(ID_EDIT_CAPITALIZE, OnUpdateEditCapitalize) ON_WM_CLOSE() ON_COMMAND(ID_FILE_SAVE_AS_, OnFileSaveAs) //}}AFX_MSG_MAP ON_WM_GETMINMAXINFO() END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { m_strFileName = ""; } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } m_wndStatusBar.SetPaneInfo(1, ID_INDICATOR_CAPS, CBRS_BOTTOM,100); m_bAutoMenuEnable = FALSE; if(-1 == LoadToolbarCombo()) return FALSE; // 调用kernal动态库初始化SOCKET库; CreateSystem(); CHAR strDirectory[MAX_PATH_LENGTH + 1]; // 软件所在路径 CHAR strAppName[MAX_PATH_LENGTH + 1] = {0}; // 软件名称 CHAR strFile[MAX_FILE_LENGTH + 1] = ""; CHAR strValue[MAX_VALUE_LENGTH + 1] = ""; int iPosFile = 0; int iLenghtFile = 0; //////////////////////////////////////////////////////////////////////////// //获取应用名 GetModuleFileName(AfxGetApp()->m_hInstance, strDirectory, MAX_PATH_LENGTH); iLenghtFile = strlen(strDirectory); for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--) { if (strDirectory[iPosFile] == '\\') { memset(strAppName, 0, sizeof(strAppName)); memcpy(strAppName, strDirectory + iPosFile + 1, iLenghtFile - iPosFile - 1); break; } } //获取系统路径 #ifdef _DEBUG GetCurrentDirectory(MAX_PATH_LENGTH, strDirectory); #else GetModuleFileName(AfxGetApp()->m_hInstance, strDirectory, MAX_PATH_LENGTH); iLenghtFile = strlen(strDirectory); for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--) { if (strDirectory[iPosFile] == '\\') { strDirectory[iPosFile] = '\0'; break; } } #endif if( pVariantsManager != NULL ) { pVariantsManager->Load(strDirectory); } return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers int CMainFrame::LoadToolbarCombo() { return 0; } void CMainFrame::OnEditPaste() { char * buffer; if(!OpenClipboard()) return; buffer = (char*)::GetClipboardData(CF_TEXT); CloseClipboard(); CString str = buffer; CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CWnd *pWnd = GetFocus(); if(pWnd->m_hWnd!=edit.m_hWnd) { pWnd->SendMessage(WM_PASTE, 0, 0); return; } CHARRANGE cr; edit.GetSel(cr); icrEditView->SetParseCookieZeroFromGivenLine(edit.LineFromChar(cr.cpMin)); //粘贴时需重置解析缓冲区 edit.ReplaceSel(str, TRUE); if(cr.cpMax != cr.cpMin) { icrEditView->ReCalcCursorPostion(); } } void CMainFrame::OnEditcut() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &SynCtrl = icrEditView->GetRichEditCtrl(); CWnd *pWnd = GetFocus(); if(pWnd->m_hWnd != SynCtrl.m_hWnd) { pWnd->SendMessage(WM_CUT, 0, 0); return; } CHARRANGE cr; SynCtrl.GetSel( cr ); icrEditView->GetRichEditCtrl().Cut(); if(cr.cpMax != cr.cpMin) { icrEditView->ReCalcCursorPostion(); } else { SCROLLINFO si; si.cbSize = sizeof(si); icrEditView->GetScrollInfo(SB_HORZ, &si); icrEditView->m_nHorzMaxOld = si.nMax; } } void CMainFrame::OnUpdateEditcut(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl& editctrl = icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnEditcopy() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CWnd *pWnd = GetFocus(); if(pWnd->m_hWnd != edit.m_hWnd) { pWnd->SendMessage(WM_COPY, 0, 0); } else { edit.Copy(); } } void CMainFrame::OnUpdateEditcopy(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl& editctrl = icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnUpdateEditpaste(CCmdUI* pCmdUI) { OpenClipboard(); if(::GetClipboardData(CF_TEXT)==FALSE) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); CloseClipboard(); } void CMainFrame::OnEditundo() { GetFocus()->SendMessage(EM_UNDO, 0, 0); } void CMainFrame::OnUpdateEditundo(CCmdUI* pCmdUI) { CWnd *pWnd = GetFocus(); if(!pWnd) return; BOOL bCanUndo = pWnd->SendMessage(EM_CANUNDO, 0, 0); pCmdUI->Enable(bCanUndo); } void CMainFrame::OnEditredo() { GetFocus()->SendMessage(EM_REDO, 0, 0); } void CMainFrame::OnUpdateEditredo(CCmdUI* pCmdUI) { CWnd *pWnd = GetFocus(); if(!pWnd) return; BOOL bCanRedo = pWnd->SendMessage(EM_CANREDO, 0, 0); pCmdUI->Enable(bCanRedo); } void CMainFrame::OnEditselectall() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CWnd *pWnd = GetFocus(); if(pWnd->m_hWnd != editctrl.m_hWnd) { CString str; pWnd->GetWindowText(str); CHARRANGE ichCharRange; ichCharRange.cpMax = str.GetLength(); ichCharRange.cpMin = 0; pWnd->SendMessage(EM_EXSETSEL, 0, (LPARAM)&ichCharRange); return; } editctrl.SetSel(0, -1); editctrl.SetFocus(); editctrl.PostMessage( WM_PAINT, 0, 0 ); } void CMainFrame::OnEditrepeat() { CWinApp *app = AfxGetApp(); CMainFrame *mainfrm = (CMainFrame *)AfxGetMainWnd(); CIcrEditView *icrEditView=(CIcrEditView *)GetActiveView(); CString strEdit; BOOL bWholeWord = app->GetProfileInt(_T("CFind"), _T("WholeWord"), 0); BOOL bCase = app->GetProfileInt(_T("CFind"), _T("Case"), 0); if(strEdit.IsEmpty()) return; int dwFlags = 0; if(bCase) dwFlags|=FR_MATCHCASE; if(bWholeWord) dwFlags|=FR_WHOLEWORD; CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); FINDTEXTEX *ft = new FINDTEXTEX[1]; ft->chrg.cpMin = cr.cpMin-1; ft->chrg.cpMax = -1;// search through end of the text TCHAR *t = new TCHAR[strEdit.GetLength()+1]; strcpy(t, strEdit); ft->lpstrText = t; icrEditView->ShowWindow(SW_HIDE); int nPos = edit.SendMessage(EM_FINDTEXTEX, dwFlags, (LPARAM)ft); if(nPos!=-1) { edit.SetSel(ft->chrgText.cpMin, ft->chrgText.cpMax); icrEditView->SetFocus(); SetFocus(); icrEditView->ShowWindow(SW_SHOW); } else { icrEditView->ShowWindow(SW_SHOW); CString strShow; strShow.Format(_T("Cannot find the string: \"%s\", search again from the end?"), strEdit); if(MessageBox(strShow,_T("Find"), MB_ICONINFORMATION|MB_YESNO)==IDYES) { icrEditView->ShowWindow(SW_HIDE); edit.SetSel(0, -1); icrEditView->ShowWindow(SW_SHOW); CHARRANGE cr; edit.GetSel(cr); edit.SetSel(cr.cpMax, cr.cpMax); OnEditrepeat(); if(t) delete t; if(ft) delete ft; return; } } if(t) delete t; if(ft) delete ft; } void CMainFrame::OnEditrepeatnext() { CWinApp *app = AfxGetApp(); CMainFrame *mainfrm = (CMainFrame *)AfxGetMainWnd(); CIcrEditView *icrEditView=(CIcrEditView *)GetActiveView(); CString strEdit; BOOL bWholeWord = app->GetProfileInt(_T("CFind"), _T("WholeWord"), 0); BOOL bCase = app->GetProfileInt(_T("CFind"), _T("Case"), 0); if(strEdit.IsEmpty()) return; int dwFlags = 0; if(bCase) dwFlags|=FR_MATCHCASE; if(bWholeWord) dwFlags|=FR_WHOLEWORD; dwFlags|=FR_DOWN;; CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); FINDTEXTEX *ft = new FINDTEXTEX[1]; ft->chrg.cpMin = cr.cpMin+1; ft->chrg.cpMax = -1;// search through end of the text TCHAR *t = new TCHAR[strEdit.GetLength()+1]; strcpy(t, strEdit); ft->lpstrText = t; icrEditView->ShowWindow(SW_HIDE); int nPos = edit.SendMessage(EM_FINDTEXTEX, dwFlags, (LPARAM)ft); if(nPos!=-1) { edit.SetSel(ft->chrgText.cpMin, ft->chrgText.cpMax); icrEditView->SetFocus(); SetFocus(); icrEditView->ShowWindow(SW_SHOW); } else { icrEditView->ShowWindow(SW_SHOW); CString strShow; strShow.Format(_T("Cannot find the string: \"%s\", search again from the beginning?"), strEdit); if(MessageBox(strShow, _T("Find"), MB_ICONINFORMATION|MB_YESNO)==IDYES) { edit.SetSel(0, 0); OnEditrepeatnext(); if(t) delete t; if(ft) delete ft; return; } } if(t) delete t; if(ft) delete ft; } void CMainFrame::OnUpdateEditrepeat(CCmdUI* pCmdUI) { } void CMainFrame::OnUpdateEditrepeatnext(CCmdUI* pCmdUI) { } void CMainFrame::OnViewFont() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); BOOL bModify = edit.GetModify(); edit.ShowWindow(SW_HIDE); CString strText; EDITSTREAM stream; stream.dwCookie = (DWORD)&strText; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); CHARFORMAT cr; if(strText.IsEmpty()) { strText = _T(" "); edit.SetSel(0, 1); cr = icrEditView->GetCharFormatSelection(); edit.SetSel(0, 0); } else { CHARRANGE range; edit.GetSel(range); int ntemp; if(range.cpMax < range.cpMin) { ntemp = range.cpMax; range.cpMax = range.cpMin; range.cpMin = ntemp; } if(range.cpMax == range.cpMin) { if(range.cpMin == 0) { edit.SetSel(0, 1); } else { edit.SetSel(range.cpMin-1, range.cpMin); } cr = icrEditView->GetCharFormatSelection(); edit.SetSel(range.cpMin, range.cpMin); } else { cr = icrEditView->GetCharFormatSelection(); } } edit.ShowWindow(SW_SHOW); CFontDialog dlg(cr, CF_BOTH|CF_NOOEMFONTS); if (dlg.DoModal() == IDOK) { CHARFORMAT cr; dlg.GetCharFormat(cr); LockWindowUpdate(); icrEditView->SetWindowText(_T("")); icrEditView->SetSynEditViewFont(*dlg.m_cf.lpLogFont); icrEditView->SetWindowText(strText); edit.SetModify(bModify); UnlockWindowUpdate(); } } void CMainFrame::SaveTheContext() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &richctrl=icrEditView->GetRichEditCtrl(); if(0 == m_strFileName.GetLength()) return; CFile cFile(m_strFileName, CFile::modeCreate|CFile::modeWrite); EDITSTREAM es; es.dwCookie = (DWORD) &cFile; es.pfnCallback = EditStreamCallbackWriteToFile; richctrl.StreamOut(SF_TEXT, es); richctrl.SetModify(FALSE); } void CMainFrame::OnFileOpen() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); if(edit.GetModify()) { if(MessageBox("文件已经修改,需要保存吗??", "", MB_YESNO|MB_ICONQUESTION)==IDNO) ; else //保存 { OnFilesave(); } } CFileDialog fd (TRUE, _T(""), NULL, OFN_HIDEREADONLY|OFN_EXPLORER, "*.srp|*.srp||"); if(IDCANCEL == fd.DoModal()) { return; } m_strFileName = fd.GetPathName(); icrEditView->LoadFile(m_strFileName); } void CMainFrame::OnEditTrimtraillingspace() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CWaitCursor wc; CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); int nfirstline = edit.GetFirstVisibleLine(); CHARRANGE cr; edit.GetSel(cr); int nline = edit.LineFromChar(cr.cpMin); CString strtext; EDITSTREAM stream; stream.dwCookie = (DWORD)&strtext; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); CString strnewtext = _T(""), strline; int npos = strtext.Find('\n'); while(npos!=-1) { strline = strtext.Left(npos-1); strline.TrimRight(0x20); strline += '\n'; strtext = strtext.Right(strtext.GetLength()-npos-1); strnewtext += strline; npos = strtext.Find('\n'); } icrEditView->LoadText(strnewtext); icrEditView->GotoLine(nfirstline+1); int nlineindex = edit.LineIndex(nline); edit.SetSel(nlineindex, nlineindex); icrEditView->ShowCursorPosition(); icrEditView->SetFocus(); SaveTheContext(); } void CMainFrame::OnEditTrimtraillingtab() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CWaitCursor wc; CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); int nfirstline = edit.GetFirstVisibleLine(); CHARRANGE cr; edit.GetSel(cr); int nline = edit.LineFromChar(cr.cpMin); CString strtext; EDITSTREAM stream; stream.dwCookie = (DWORD)&strtext; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); CString strnewtext = _T(""), strline; int npos = strtext.Find('\n'); while(npos!=-1) { strline = strtext.Left(npos-1); strline.TrimRight(0x9); strline += '\n'; strtext = strtext.Right(strtext.GetLength()-npos-1); strnewtext += strline; npos = strtext.Find('\n'); } icrEditView->LoadText(strnewtext); icrEditView->GotoLine(nfirstline+1); int nlineindex = edit.LineIndex(nline); edit.SetSel(nlineindex, nlineindex); icrEditView->ShowCursorPosition(); icrEditView->SetFocus(); SaveTheContext(); } void CMainFrame::OnEditTabtospace() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CWaitCursor wc; TCHAR chr = 0x9; CString strtab = chr; int ntabsize = icrEditView->GetTabSize(); CString strspace((char)0x20, ntabsize); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); int nfirstline = edit.GetFirstVisibleLine(); CHARRANGE cr; edit.GetSel(cr); int nline = edit.LineFromChar(cr.cpMin); CString strtext; EDITSTREAM stream; stream.dwCookie = (DWORD)&strtext; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); strtext.Replace(strtab, strspace); icrEditView->LoadText(strtext); icrEditView->GotoLine(nfirstline+1); int nlineindex = edit.LineIndex(nline); edit.SetSel(nlineindex, nlineindex); icrEditView->ShowCursorPosition(); icrEditView->SetFocus(); SaveTheContext(); } void CMainFrame::OnEditSpacetotab() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CWaitCursor wc; TCHAR chr = 0x9; CString strtab = chr; int ntabsize = icrEditView->GetTabSize(); CString strspace((char)0x20, ntabsize); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); int nfirstline = edit.GetFirstVisibleLine(); CHARRANGE cr; edit.GetSel(cr); int nline = edit.LineFromChar(cr.cpMin); CString strtext; EDITSTREAM stream; stream.dwCookie = (DWORD)&strtext; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); strtext.Replace(strspace, strtab); icrEditView->LoadText(strtext); icrEditView->GotoLine(nfirstline+1); int nlineindex = edit.LineIndex(nline); edit.SetSel(nlineindex, nlineindex); icrEditView->ShowCursorPosition(); icrEditView->SetFocus(); SaveTheContext(); } void CMainFrame::OnEditLeadingspacetotab() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CWaitCursor wc; int ntabsize = icrEditView->GetTabSize(); CString strspace((char)0x20, ntabsize); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); int nfirstline = edit.GetFirstVisibleLine(); CHARRANGE cr; edit.GetSel(cr); int nline = edit.LineFromChar(cr.cpMin); CString strtext; EDITSTREAM stream; stream.dwCookie = (DWORD)&strtext; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT, stream); CString strnewtext = _T(""), strline; int npos = strtext.Find('\n'); while(npos!=-1) { int i=0; strline = strtext.Left(npos); while(strline[i]==0x20 || strline[i]==0x9) i++; CString strtmp = strline.Left(i); strtmp.Replace( strspace, _T("\t")); strline = strtmp + strline.Right(strline.GetLength()-i); strline += '\n'; strtext = strtext.Right(strtext.GetLength()-npos-1); strnewtext += strline; npos = strtext.Find('\n'); } icrEditView->LoadText(strnewtext); icrEditView->GotoLine(nfirstline+1); int nlineindex = edit.LineIndex(nline); edit.SetSel(nlineindex, nlineindex); icrEditView->ShowCursorPosition(); icrEditView->SetFocus(); SaveTheContext(); } void CMainFrame::OnEditMoveright() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &SynCtrl = icrEditView->GetRichEditCtrl(); icrEditView->ShowWindow(SW_HIDE); int nfirstline, nendline; icrEditView->GetSelLine(nfirstline, nendline); CString strtext=_T(""), strline; BOOL bpriorlinerealreturn; int linenumber = nendline - nfirstline + 1; for(int i=0; iGetLineString(nfirstline+i-1, strline); icrEditView->GetLineString(nfirstline+i, strline); if(bpriorlinerealreturn || nfirstline==nendline) { //如果上一行是真的换行 int nselstart = SynCtrl.LineIndex(nfirstline+i); SynCtrl.SetSel(nselstart, nselstart); int noldlinecount = SynCtrl.GetLineCount(); SynCtrl.ReplaceSel(_T("\t"), TRUE); //替换后可能引总行数增加 int nnewlinecount = SynCtrl.GetLineCount(); linenumber+=nnewlinecount-noldlinecount; } } int nselstart = SynCtrl.LineIndex(nfirstline); int nselend = SynCtrl.LineIndex(nfirstline+linenumber); SynCtrl.SetSel(nselstart, nselend); icrEditView->ShowWindow(SW_SHOW); } void CMainFrame::OnEditMoveleft() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &SynCtrl = icrEditView->GetRichEditCtrl(); icrEditView->ShowWindow(SW_HIDE); int nfirstline, nendline; icrEditView->GetSelLine(nfirstline, nendline); CString strtext=_T(""), strline; BOOL bpriorlinerealreturn; int linenumber = nendline - nfirstline + 1; for(int i=0; iGetLineString(nfirstline+i-1, strline); icrEditView->GetLineString(nfirstline+i, strline); if(bpriorlinerealreturn || nfirstline==nendline) { //如果上一行是真的换行 int nselstart = SynCtrl.LineIndex(nfirstline+i); SynCtrl.SetSel(nselstart, nselstart); if(strline.Left(1)==_T("\t")) { SynCtrl.SetSel(nselstart, nselstart+1); SynCtrl.ReplaceSel(_T(""), TRUE); //替换后可能引总行数减少 } else { if(strline!=_T(" ")) { //之所以有此判断是因为如果是空行icrEditView->GetLineString会返回一个空格 int ncount = (strline.GetLength()>icrEditView->GetTabSize()? strline.GetLength():icrEditView->GetTabSize()); for(int j=0; jShowWindow(SW_SHOW); } void CMainFrame::OnEditUncomment() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &SynCtrl = icrEditView->GetRichEditCtrl(); int nfirstline, nendline; icrEditView->GetSelLine(nfirstline, nendline); //取当前语言对应的单行注释字符串 CString strsinglecomment=icrEditView->GetSingleCommentString(icrEditView->GetLanguage()); if(strsinglecomment.IsEmpty()) //若为空串表示当前没有进行语法关键字显示 return; CString strtext=_T(""), strline; icrEditView->ShowWindow(SW_HIDE); BOOL bpriorlinerealreturn; int linenumber = nendline - nfirstline + 1; for(int i=0; iGetLineString(nfirstline+i-1, strline); icrEditView->GetLineString(nfirstline+i, strline); if(bpriorlinerealreturn || nfirstline==nendline) { //如果上一行是真的换行 int nselstart = SynCtrl.LineIndex(nfirstline+i); SynCtrl.SetSel(nselstart, nselstart); if(strline!=_T(" ")) { if(strline.Left(strsinglecomment.GetLength())==strsinglecomment) SynCtrl.SetSel(nselstart, nselstart+strsinglecomment.GetLength()); else { CString strtmp = strline; if(strtmp.GetLength()!=0) { while(strtmp.GetAt(0)==0x20 || strtmp.GetAt(0)=='\t') { strtmp.TrimLeft(0x20); strtmp.TrimLeft('\t'); if(strtmp.GetLength()==0) break; } if(strtmp.GetLength()>=strsinglecomment.GetLength() ) { if(strtmp.Left(strsinglecomment.GetLength())==strsinglecomment) { int npos = strline.Find(strsinglecomment); SynCtrl.SetSel( nselstart+npos, nselstart+npos+strsinglecomment.GetLength()); } } } } } int noldlinecount=SynCtrl.GetLineCount(); SynCtrl.ReplaceSel(_T(""), TRUE); //此行可能引起总的行数减少 int nnewlinecount=SynCtrl.GetLineCount(); linenumber+=nnewlinecount-noldlinecount; } } int nselstart = SynCtrl.LineIndex(nfirstline); int nselend = SynCtrl.LineIndex(nfirstline+linenumber); SynCtrl.SetSel(nselstart, nselend); icrEditView->ShowWindow(SW_SHOW); } void CMainFrame::OnEditComment() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &SynCtrl = icrEditView->GetRichEditCtrl(); int nfirstline, nendline; icrEditView->GetSelLine(nfirstline, nendline); //取当前语言对应的单行注释字符串 CString strsinglecomment=icrEditView->GetSingleCommentString(icrEditView->GetLanguage()); if(strsinglecomment.IsEmpty()) //若为空串表示当前没有进行语法关键字显示 return; icrEditView->ShowWindow(SW_HIDE); CString strtext=_T(""), strline; BOOL bpriorlinerealreturn; int linenumber = nendline - nfirstline + 1; for(int i=0; iGetLineString(nfirstline+i-1, strline); icrEditView->GetLineString(nfirstline+i, strline); if(bpriorlinerealreturn || nfirstline==nendline) { //如果上一行是真的换行 int nselstart = SynCtrl.LineIndex(nfirstline+i); SynCtrl.SetSel(nselstart, nselstart); int noldlinecount = SynCtrl.GetLineCount(); SynCtrl.ReplaceSel(strsinglecomment, TRUE); int nnewlinecount = SynCtrl.GetLineCount(); linenumber+=nnewlinecount-noldlinecount; } } int nselstart = SynCtrl.LineIndex(nfirstline); int nselend = SynCtrl.LineIndex(nfirstline+linenumber); SynCtrl.SetSel(nselstart, nselend); icrEditView->ShowWindow(SW_SHOW); } void CMainFrame::OnEditLcase() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); CString strsel; EDITSTREAM stream; stream.dwCookie = (DWORD)&strsel; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT|SFF_SELECTION, stream); strsel.MakeLower(); edit.ReplaceSel(strsel, TRUE); edit.SetSel(cr); } void CMainFrame::OnEditUcase() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); CString strsel; EDITSTREAM stream; stream.dwCookie = (DWORD)&strsel; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT|SFF_SELECTION, stream); strsel.MakeUpper(); edit.ReplaceSel(strsel, TRUE); edit.SetSel(cr); } void CMainFrame::OnEditInvert() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); CString strsel; EDITSTREAM stream; stream.dwCookie = (DWORD)&strsel; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT|SFF_SELECTION, stream); for(int i=0; i='A' && ch <='Z' ) strsel.SetAt(i, ch + 'a' - 'A'); else if( ch >='a' && ch <='z' ) strsel.SetAt(i, ch + 'A' - 'a'); } edit.ReplaceSel(strsel, TRUE); edit.SetSel(cr); } void CMainFrame::OnEditCapitalize() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); CString strsel; EDITSTREAM stream; stream.dwCookie = (DWORD)&strsel; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT|SFF_SELECTION, stream); strsel.MakeLower(); BOOL bIsChar = FALSE, bInString=FALSE; for(int i=0; i='a' && ch <='z' && !bIsChar) { strsel.SetAt(i, ch + 'A' - 'a'); } if( (ch >='a' && ch <='z') || (ch >='A' && ch <='Z') ) bIsChar = TRUE; else bIsChar = FALSE; } edit.ReplaceSel(strsel, TRUE); edit.SetSel(cr); } void CMainFrame::OnEditSentancecase() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); CHARRANGE cr; edit.GetSel(cr); CString strsel; EDITSTREAM stream; stream.dwCookie = (DWORD)&strsel; stream.pfnCallback = EditStreamCallbackOut; edit.StreamOut(SF_TEXT|SFF_SELECTION, stream); BOOL bDo = TRUE; for(int i=0; i='a' && ch <='z') || (ch >='A' && ch <='Z')) && bDo) { if( ch >='A' && ch <='Z' ) strsel.SetAt(i, ch + 'a' - 'A'); else strsel.SetAt(i, ch + 'A' - 'a'); bDo = FALSE; } } edit.ReplaceSel(strsel, TRUE); edit.SetSel(cr); } CString CMainFrame::GetFileContext(CString strfile) { CString str=_T("");; CFileException fe; CFile file; file.Open(strfile, CFile::modeRead, &fe); if(fe.m_cause != CFileException::none) { fe.ReportError(); return _T(""); } DWORD dwSize=(DWORD)file.GetLength(); if(dwSize==0) return _T(""); try { LPSTR lpszContext=new TCHAR[1]; file.Read(lpszContext, 1); if(*lpszContext==-1) { if(lpszContext!=NULL) delete lpszContext; LPWSTR lpsztext= new WCHAR[1]; file.Read(lpsztext, 1); if(lpsztext!=NULL) delete lpsztext; lpsztext= new WCHAR[dwSize-1]; DWORD dwRead=file.Read(lpsztext, dwSize-2); lpsztext[dwRead/2]=0; str=lpsztext; if(lpsztext!=NULL) delete lpsztext; } else { if(lpszContext!=NULL) delete lpszContext; lpszContext=new TCHAR[dwSize+2]; file.Seek(0, CFile::begin); DWORD dwRead=file.Read(lpszContext, dwSize); file.Close(); lpszContext[dwRead]='\0'; str=lpszContext; if(lpszContext!=NULL) delete lpszContext; } } catch(...){} return str; } void CMainFrame::OnSelchangeComboFind() { } void CMainFrame::OnUpdateEditLcase(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnUpdateEditInvert(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnUpdateEditSentancecase(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnUpdateEditUcase(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnFileNew() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); if(!m_strFileName.IsEmpty()) { if(edit.GetModify()) { if(MessageBox("文件已经修改,需要保存吗?", "", MB_YESNO|MB_ICONQUESTION)==IDYES) { SaveTheContext(); } } } edit.SetWindowText(""); m_strFileName = ""; icrEditView->SetSynEditViewFont(icrEditView->GetSynEditViewFont()); } void CMainFrame::OnFileSaveAs() { if(m_strFileName.IsEmpty()) { CFileDialog fd ( FALSE, _T(""), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("*.srp|*.srp||")); if(fd.DoModal()==IDCANCEL) return; m_strFileName = fd.GetPathName(); } else { CString strFileName = m_strFileName; int nPos= strFileName.ReverseFind('\\'); strFileName = strFileName.Right(strFileName.GetLength()-nPos-1); CFileDialog fd ( FALSE, "", NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("*.srp|*.srp||")); wsprintf(fd.m_ofn.lpstrFile, strFileName); if(fd.DoModal()==IDCANCEL) return; m_strFileName = fd.GetPathName(); } SaveTheContext(); } void CMainFrame::OnFilesave() { if(m_strFileName.IsEmpty()) { CFileDialog fd ( FALSE, _T(""), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("*.srp|*.srp||")); if(fd.DoModal()==IDCANCEL) return; m_strFileName = fd.GetPathName(); } SaveTheContext(); } void CMainFrame::OnUpdateEditCapitalize(CCmdUI* pCmdUI) { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &editctrl=icrEditView->GetRichEditCtrl(); CHARRANGE cr; editctrl.GetSel(cr); pCmdUI->Enable(cr.cpMin!=cr.cpMax); } void CMainFrame::OnClose() { CIcrEditView *icrEditView = (CIcrEditView *)GetActiveView(); CRichEditCtrl &edit = icrEditView->GetRichEditCtrl(); if(edit.GetModify()) { int nRet = MessageBox("文件已经修改,需要保存吗?", "", MB_YESNOCANCEL|MB_ICONQUESTION); if(nRet==IDYES) { if(!m_strFileName.IsEmpty()) { SaveTheContext(); } else { CFileDialog fd ( FALSE, _T(""), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("*.*|*.*||")); if(fd.DoModal()==IDCANCEL) return; m_strFileName = fd.GetPathName(); SaveTheContext(); } } else if(nRet==IDCANCEL) { return; } } CFrameWnd::OnClose(); }