📄 tvframe.cpp
字号:
}
bool CTVFrame::TryOpenFile(CString& filename) {
if (filename.GetLength()>0) { // selected some file
TextFile *tf=TextFile::Open(filename);
if (tf==NULL && filename.GetLength()>2 && filename[0]==_T('"') &&
filename[filename.GetLength()-1]==_T('"'))
{
filename=filename.Mid(1,filename.GetLength()-2);
tf=TextFile::Open(filename);
}
if (tf) {
if (m_realview) {
((CTView *)m_wndView.get())->SetFile(kilo::auto_ptr<TextFile>(tf));
SetWindowText(_T("HR: ")+FileName(filename));
}
return true;
} else {
CString msg;
msg.Format(_T("Can't open file '%s'"),filename);
MessageBox(msg,_T("Error"),MB_ICONERROR|MB_OK);
}
}
return false;
}
void CTVFrame::OnUpdateFileOpen(CCmdUI* pCmdUI) {
pCmdUI->Enable();
}
void CTVFrame::OnFileOpen() {
CString ipath=CTVApp::GetStr(_T("OpenPath"));
CString sv=ipath;
CString str=GetFileName(&ipath,this);
if (sv!=ipath)
CTVApp::SetStr(_T("OpenPath"),ipath);
TryOpenFile(str);
}
BOOL CTVFrame::OnCopyData(CWnd *pWnd,COPYDATASTRUCT *pcd) {
// filename is always unicode here
CString filename(Unicode::ToCS((const wchar_t *)pcd->lpData,pcd->cbData/sizeof(wchar_t)));
if (filename!=_T("-d")) // post a message to ourselves with a copy of filename
if (filename.GetLength()==0)
PostMessage(MSG_OPEN_FILE,0,0);
else
PostMessage(MSG_OPEN_FILE,0,(LPARAM)new CString(filename));
else
if (m_wndView.get())
((CTView*)m_wndView.get())->SwitchToDict();
return TRUE;
}
void CTVFrame::SaveWndPos() {
#ifndef _WIN32_WCE
WINDOWPLACEMENT pl;
memset(&pl,0,sizeof(pl));
pl.length=sizeof(pl);
::GetWindowPlacement(m_hWnd,&pl);
if (pl.showCmd!=SW_MAXIMIZE && pl.showCmd!=SW_SHOWMAXIMIZED)
pl.showCmd=SW_SHOWNORMAL;
memcpy(&m_wndpos,&pl,sizeof(m_wndpos));
#endif
}
bool CTVFrame::InitView() {
bool dictmode=false;
// initialize xml parser
XMLParser::LoadStyles();
bool triednoquote=false;
CString filename(AfxGetApp()->m_lpCmdLine);
if (filename==_T("-d")) { // show dictionary
filename=_T("NUL");
dictmode=true;
}
if (filename.GetLength()==0) // no filename provided, try to fetch most recent
filename=Bookmarks::find_last_file();
TextFile *tf;
for (;;) { // repeat this until it works, or until the user chooses to exit
if (filename.GetLength()==0) { // still no luck, open fs browser
CString ipath=CTVApp::GetStr(_T("OpenPath"));
CString sv(ipath);
filename=GetFileName(&ipath,this);
if (sv!=ipath)
CTVApp::SetStr(_T("OpenPath"),ipath);
} else if (filename.GetLength()>4 && filename.Right(4).CompareNoCase(_T(".zip"))==0) {
CString ipath=filename;
CString sv(ipath);
filename=GetFileName(&ipath,this);
if (sv!=ipath)
CTVApp::SetStr(_T("OpenPath"),ipath);
}
if (filename.GetLength()==0) { // everything failed, barf at the user
MessageBox(_T("No file name provided on the command line, and no recent files found."),
_T("Error"),MB_ICONERROR|MB_OK);
return false;
}
tf=TextFile::Open(filename);
if (tf==NULL) {
if (!triednoquote && filename.GetLength()>2 && filename[0]==_T('"') &&
filename[filename.GetLength()-1]==_T('"'))
{
triednoquote=true;
filename=filename.Mid(1,filename.GetLength()-2);
continue;
}
CString msg;
msg.Format(_T("Can't open file '%s'"),filename);
MessageBox(msg,_T("Error"),MB_ICONERROR|MB_OK);
filename.Empty();
} else
break;
}
// replace a dummy view with a real one
HWND wnd=m_wndView->Detach();
CTView *tv=new CTView;
tv->Attach(wnd);
tv->Init();
tv->SetFile(kilo::auto_ptr<TextFile>(tf));
if (dictmode) {
SetWindowText(_T("HR: Dictionary"));
if (!tv->SwitchToDict())
return false;
} else
SetWindowText(_T("HR: ")+FileName(filename));
m_wndView.reset(tv);
m_realview=true;
return true;
}
void CTVFrame::OnInitView() {
if (!InitView())
PostMessage(WM_COMMAND,ID_APP_EXIT);
}
void CTVFrame::UpdateRecentFiles(CMenu *menu) {
FILETIME tm;
m_recentlist.RemoveAll();
Bookmarks::get_recent_files(m_recentlist,RECENT_FILES,tm);
if (tm.dwLowDateTime!=m_toptime.dwLowDateTime || tm.dwHighDateTime!=m_toptime.dwHighDateTime) {
m_toptime=tm;
while (menu->RemoveMenu(0,MF_BYPOSITION)) ;
for (int ii=0;ii<m_recentlist.GetSize();++ii)
menu->AppendMenu(MF_STRING,RECENT_BASE+ii,FileName(m_recentlist[ii]));
if (m_recentlist.GetSize()==0)
menu->AppendMenu(MF_STRING|MF_GRAYED,1,_T("No Files"));
}
}
void CTVFrame::UpdateColors(CMenu *menu) {
for (;;) {
MENUITEMINFO ii;
memset(&ii,0,sizeof(ii));
ii.cbSize = sizeof(ii);
ii.fMask = MIIM_TYPE;
if (!menu->GetMenuItemInfo(2,&ii,TRUE))
break;
if (ii.fType & MFT_SEPARATOR)
break;
menu->DeleteMenu(2,MF_BYPOSITION);
}
AddColorProfileNames(menu,2);
}
void CTVFrame::UpdateDictionaries(CMenu *menu) {
CStringArray list;
int cur;
CDictSetupDlg::GetDictList(list,cur);
while (menu->RemoveMenu(0,MF_BYPOSITION)) ;
for (int ii=0;ii<list.GetSize();++ii) {
UINT flags=MF_STRING|(ii==cur ? MF_CHECKED : 0);
if (!list[ii].IsEmpty() && list[ii][0]==_T(':'))
menu->AppendMenu(flags,DICT_BASE+ii,(const TCHAR *)list[ii]+1);
else
menu->AppendMenu(flags,DICT_BASE+ii,FileName(list[ii]));
}
if (list.GetSize()>0)
menu->AppendMenu(MF_SEPARATOR);
menu->AppendMenu(MF_STRING,ID_DICT_SETUP,_T("Configure..."));
}
void CTVFrame::OnUpdateRecentFile(CCmdUI *pCmdUI) {
pCmdUI->Enable();
}
void CTVFrame::OnRecentFile(UINT cmd) {
if (cmd-RECENT_BASE<(UINT)m_recentlist.GetSize()) {
TextFile *tf=TextFile::Open(m_recentlist[cmd-RECENT_BASE]);
if (tf) {
if (m_realview) {
((CTView *)m_wndView.get())->SetFile(kilo::auto_ptr<TextFile>(tf));
SetWindowText(_T("HR: ")+FileName(m_recentlist[cmd-RECENT_BASE]));
}
} else {
CString msg;
msg.Format(_T("Can't open file '%s'"),m_recentlist[cmd-RECENT_BASE]);
MessageBox(msg,_T("Error"),MB_ICONERROR|MB_OK);
}
}
}
static void GetTbPopupPoint(CToolBarCtrl& tb,UINT cmd,int& x,int& y,int& align) {
RECT rc;
tb.GetItemRect(tb.CommandToIndex(cmd),&rc);
tb.ClientToScreen(&rc);
x=rc.left;
#if POCKETPC
y=rc.top;
align=TPM_BOTTOMALIGN;
#else
y=rc.bottom;
align=TPM_TOPALIGN;
#endif
}
void CTVFrame::OnUpdateMainTools(CCmdUI* pCmdUI) {
pCmdUI->Enable();
}
void CTVFrame::OnUpdateMainOptions(CCmdUI* pCmdUI) {
pCmdUI->Enable();
}
BOOL CTVFrame::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) {
TBNOTIFY *tn=(TBNOTIFY*)lParam;
if (tn->hdr.code==TBN_BEGINDRAG) {
// abuse the notification to show a popup menu
int sm=-1;
switch (tn->iItem) {
case ID_MAIN_TOOLS:
sm=0;
break;
case ID_MAIN_OPTIONS:
sm=1;
break;
}
if (sm>=0) {
#ifdef _WIN32_WCE
CMenu *menu=GetMenu();
#else
CMenu *menu=m_mainmenu;
#endif
if (menu) {
CMenu *sub=menu->GetSubMenu(sm);
if (sub) {
int x,y,align;
GetTbPopupPoint(m_wndCommandBar.GetToolBarCtrl(),tn->iItem,x,y,align);
sub->TrackPopupMenu(TPM_LEFTALIGN|align,x,y,this,NULL);
// unwedge the toolbar :)
m_wndCommandBar.GetToolBarCtrl().PressButton(tn->iItem,FALSE);
}
}
}
}
return CFrameWnd::OnNotify(wParam, lParam, pResult);
}
void CTVFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) {
CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
if (pPopupMenu->m_hMenu==m_dicts)
UpdateDictionaries(pPopupMenu);
else if (pPopupMenu->m_hMenu==m_recent)
UpdateRecentFiles(pPopupMenu);
else if (pPopupMenu->m_hMenu==m_colors)
UpdateColors(pPopupMenu);
}
void CTVFrame::OnSize(UINT nType, int cx, int cy) {
CFrameWnd::OnSize(nType, cx, cy);
#if POCKETPC
// in hires this old mfc gets the command bar size wrong
// so we force our own window size
if (!m_fullscreen) {
RECT rcW,rcCB;
GetWindowRect(&rcW);
m_wndCommandBar.GetWindowRect(&rcCB);
if (rcW.bottom > rcCB.top) {
rcW.bottom = rcCB.top;
ScreenToClient(&rcW);
m_wndView->MoveWindow(&rcW);
}
}
#endif
#ifndef _WIN32_WCE
if (cx>0 && cy>0 && !m_in_fullscreen)
GetWindowRect(&m_normsize);
#endif
SaveWndPos();
}
void CTVFrame::OnMove(int x, int y) {
CFrameWnd::OnMove(x, y);
#ifndef _WIN32_WCE
if (!m_in_fullscreen)
GetWindowRect(&m_normsize);
#endif
SaveWndPos();
}
LRESULT CTVFrame::OnOpenFile(WPARAM wParam,LPARAM lParam) {
CString *str=(CString*)lParam;
if (str) {
TryOpenFile(*str);
delete str;
}
return 0;
}
BOOL CTVFrame::OnEraseBkgnd(CDC* pDC) {
return FALSE; // frame has no background
}
/////////////////////////////////////////////////////
// some hires support
static int getDPI() {
HDC hDC = CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
int dpi = GetDeviceCaps(hDC,LOGPIXELSY);
DeleteDC(hDC);
return dpi;
}
UINT getIDR_DIALOG() {
return getDPI() >= 120 ? IDR_DIALOG_HR : IDR_DIALOG;
}
UINT getIDR_CONTENTS() {
return getDPI() >= 120 ? IDR_CONTENTS_HR : IDR_CONTENTS;
}
UINT getIDR_MAINFRAME() {
return getDPI() >= 120 ? IDR_MAINFRAME_HR : IDR_MAINFRAME;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -