todolistwnd.cpp
来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,024 行 · 第 1/5 页
CPP
2,024 行
if (m_toolbar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_WRAPABLE,
WS_CHILD | WS_VISIBLE | CBRS_TOP |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
{
if (m_toolbar.LoadToolBar(IDR_TOOLBAR))
{
m_toolbar.SetImage(nIDToolbarImage, RGB(255, 0, 255));
m_toolbar.GetToolBarCtrl().HideButton(ID_TOOLS_TOGGLECHECKIN, !Prefs().GetEnableSourceControl());
}
}
}
return (NULL != m_toolbar.GetSafeHwnd());
}
BOOL CToDoListWnd::PreTranslateMessage(MSG* pMsg)
{
// the only way we get a WM_CLOSE here is if it was sent from an external app
// so we shut down as gracefully as possible
if (pMsg->message == WM_CLOSE && IsWindowEnabled())
{
DoExit();
return TRUE;
}
if (ProcessDialogControlShortcut(pMsg))
return TRUE;
// process for app level shortcuts first so we can handle
// reserved shortcuts
DWORD dwShortcut = 0;
UINT nCmdID = m_mgrShortcuts.ProcessMessage(pMsg, &dwShortcut);
// if it's a reserved shortcut let's notify the user to change it
if (CToDoCtrl::IsReservedShortcut(dwShortcut))
{
int nRet = MessageBox(IDS_RESERVEDSHORTCUT_MSG, IDS_RESERVEDSHORTCUT_TITLE, MB_YESNOCANCEL);
if (nRet == IDYES)
DoPreferences(PREFPAGE_SHORTCUT);
// and keep eating it until the user changes it
return TRUE;
}
// now try active task list
if (GetTDCCount() && GetToDoCtrl().PreTranslateMessage(pMsg))
return TRUE;
if (nCmdID)
{
BOOL bSendMessage = TRUE; // default
// focus checks
switch (nCmdID)
{
case ID_EDIT_CUT:
case ID_EDIT_COPY:
// tree must have the focus
if (!GetToDoCtrl().TreeHasFocus())
{
bSendMessage = FALSE;
GetToDoCtrl().ClearCopiedItem();
}
break;
// tree must have the focus
case ID_EDIT_SELECT_ALL:
case ID_EDIT_PASTE:
case ID_DELETEALLTASKS:
case ID_DELETETASK:
bSendMessage = GetToDoCtrl().TreeHasFocus();
break;
}
// send off
if (bSendMessage)
{
SendMessage(WM_COMMAND, nCmdID);
return TRUE;
}
}
// we need to check for <escape>
if (Prefs().GetEscapeMinimizes())
{
switch (pMsg->message)
{
case WM_KEYDOWN:
{
switch (pMsg->wParam)
{
case VK_ESCAPE:
if (GetCapture() == NULL)
{
// if the window with the target is either a combobox or
// the child edit of a combobox and the combo is
// dropped down then let it thru else if the target is
// a child of ours then treat as a cancel
BOOL bHandle = TRUE;
if (CWinClasses::IsClass(pMsg->hwnd, WC_COMBOBOX))
bHandle = !ComboBox_GetDroppedState(pMsg->hwnd);
else if (CWinClasses::IsClass(::GetParent(pMsg->hwnd), WC_COMBOBOX))
bHandle = !ComboBox_GetDroppedState(::GetParent(pMsg->hwnd));
if (bHandle && ::IsChild(*this, pMsg->hwnd))
{
OnCancel();
return TRUE;
}
}
}
}
break;
}
}
return CFrameWnd::PreTranslateMessage(pMsg);
}
void CToDoListWnd::OnCancel()
{
ASSERT (Prefs().GetEscapeMinimizes());
// if the close button has been configured to Minimize to tray
// then do that here else normal minimize
int nOption = Prefs().GetSysTrayOption();
if (nOption == STO_ONMINCLOSE || nOption == STO_ONCLOSE)
MinimizeToTray();
else
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
}
void CToDoListWnd::OnDeleteTask()
{
if (GetToDoCtrl().GetSelectedItem())
GetToDoCtrl().DeleteSelectedTask();
}
void CToDoListWnd::OnDeleteAllTasks()
{
if (GetToDoCtrl().DeleteAllTasks())
{
m_mgrToDoCtrls.ClearFilePath(GetSelToDoCtrl()); // this will ensure that the user must explicitly overwrite the original file
UpdateStatusbar();
}
}
void CToDoListWnd::OnSave()
{
if (!SaveTaskList(GetSelToDoCtrl()))
{
// check readonly status and notify user
// TODO
}
else
UpdateCaption();
}
BOOL CToDoListWnd::SaveTaskList(int nIndex, LPCTSTR szFilePath)
{
CAutoFlag af(m_bSaving, TRUE);
CString sFilePath = szFilePath ? szFilePath : m_mgrToDoCtrls.GetFilePath(nIndex);
CFilteredToDoCtrl& tdc = GetToDoCtrl(nIndex);
tdc.Flush();
// build dialog title, incorporating tasklist name
CString sName = m_mgrToDoCtrls.GetFriendlyProjectName(nIndex);
CEnString sTitle(IDS_SAVETASKLIST_TITLE, sName);
// conditions for saving
// 1. Save As... ie szFilePath != NULL and not empty
// 2. tasklist has been modified
if ((szFilePath && !sFilePath.IsEmpty()) || tdc.IsModified())
{
CPreferencesDlg& prefs = Prefs();
// do this in a loop in case the save fails for _any_ reason
while (TRUE)
{
if (sFilePath.IsEmpty()) // means first time save
{
// activate tasklist
if (!SelectToDoCtrl(nIndex, (nIndex != GetSelToDoCtrl())))
return FALSE;
// use tab text as hint to user
sFilePath = m_mgrToDoCtrls.GetFilePath(nIndex, FALSE);
CEnFileDialog dialog(FALSE, GetDefaultFileExt(), sFilePath,
OFN_OVERWRITEPROMPT, GetFileFilter(), this);
dialog.m_ofn.lpstrTitle = sTitle;
if (dialog.DoModal() != IDOK)
return FALSE; // user elected not to proceed
// else make sure the file is not readonly
sFilePath = dialog.GetPathName();
if (CDriveInfo::IsReadonlyPath(sFilePath) > 0)
{
CEnString sMessage(IDS_SAVEREADONLY, sFilePath);
if (MessageBox(sMessage, sTitle, MB_OKCANCEL) == IDCANCEL)
return FALSE; // user elected not to proceed
else
{
sFilePath.Empty(); // try again
continue;
}
}
}
// update source control status
BOOL bSrcControl = m_mgrToDoCtrls.PathSupportsSourceControl(sFilePath);
tdc.SetStyle(TDCS_ENABLESOURCECONTROL, bSrcControl);
tdc.SetStyle(TDCS_CHECKOUTONLOAD, bSrcControl ? prefs.GetAutoCheckOut() : FALSE);
CWaitCursor cursor;
CTaskFile tasks;
TDC_FILE nResult = tdc.Save(tasks, sFilePath, prefs.GetCheckForChangesBeforeSaving());
if (nResult == TDCO_SUCCESS)
{
m_mgrToDoCtrls.SetModifiedStatus(nIndex, FALSE);
m_mgrToDoCtrls.RefreshLastModified(nIndex);
m_mgrToDoCtrls.RefreshReadOnlyStatus(nIndex);
m_mgrToDoCtrls.RefreshPathType(nIndex);
if (prefs.GetAddFilesToMRU())
m_mruList.Add(sFilePath);
UpdateCaption();
UpdateStatusbar();
// export to html?
if (prefs.GetAutoHtmlExport())
{
CString sExportPath = prefs.GetAutoExportFolderPath();
CString sDrive, sFolder, sFName;
//fabio_2005
FileMisc::SplitPath(sFilePath, &sDrive, &sFolder, &sFName);
if (!sExportPath.IsEmpty() && FileMisc::CreateFolder(sExportPath))
FileMisc::MakePath(sFilePath, NULL, sExportPath, sFName, ".html");
else
FileMisc::MakePath(sFilePath, sDrive, sFolder, sFName, ".html");
// if the users wants all attributes exported then
// we can simply export the tasks retrieved from CToDoCtrl::Save
if (prefs.GetExportVisibleColsOnly())
Export2Html(tdc, sFilePath, prefs.GetSaveExportStylesheet());
else
Export2Html(tasks, sFilePath, prefs.GetSaveExportStylesheet());
}
// we're done
break;
}
// error handling
else if (nResult == TDCO_NOTALLOWED)
{
CEnString sMessage(IDS_SAVEACCESSDENIED, sFilePath);
if (IDYES == MessageBox(sMessage, sTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION))
{
sFilePath.Empty(); // try again
continue;
}
else // clear modified status
{
tdc.SetModified(FALSE);
m_mgrToDoCtrls.SetModifiedStatus(nIndex, FALSE);
}
}
else
{
CString sMessage;
switch (nResult)
{
case TDCO_CANCELLED:
break;
case TDCO_BADMSXML:
sMessage.Format(IDS_SAVEBADXML, sFilePath);
break;
case TDCO_INUSE:
sMessage.Format(IDS_SAVESHARINGVIOLATION, sFilePath);
break;
default:
sMessage.Format(IDS_UNKNOWNSAVEERROR2, sFilePath, (nResult - (int)TDCO_OTHER));
break;
}
if (!sMessage.IsEmpty())
MessageBox(sMessage, sTitle, MB_OK);
return FALSE;
}
}
}
return TRUE;
}
LPCTSTR CToDoListWnd::GetFileFilter()
{
static CEnString TDLFILEFILTER(IDS_TDLFILEFILTER);
static CEnString XMLFILEFILTER(IDS_XMLFILEFILTER);
if (Prefs().GetEnableTDLExtension())
return TDLFILEFILTER;
else
return XMLFILEFILTER;
}
LPCTSTR CToDoListWnd::GetDefaultFileExt()
{
static LPCTSTR TDLEXT = "tdl";
static LPCTSTR XMLEXT = "xml";
if (Prefs().GetEnableTDLExtension())
return TDLEXT;
else
return XMLEXT;
}
void CToDoListWnd::UpdateStatusbar()
{
if (m_bShowStatusBar && GetTDCCount())
{
CFilteredToDoCtrl& tdc = GetToDoCtrl();
CString sText = tdc.GetFilePath();
if (sText.IsEmpty())
sText.LoadString(ID_SB_FILEPATH);
m_statusBar.SetPaneText(m_statusBar.CommandToIndex(ID_SB_FILEPATH), sText);
UINT nVisibleTasks;
UINT nTotalTasks = tdc.GetTaskCount(&nVisibleTasks);
sText.Format(ID_SB_TASKCOUNT, nVisibleTasks, nTotalTasks);
m_statusBar.SetPaneText(m_statusBar.CommandToIndex(ID_SB_TASKCOUNT), sText);
sText.Format(ID_SB_FILEVERSION, tdc.GetFileVersion());
m_statusBar.SetPaneText(m_statusBar.CommandToIndex(ID_SB_FILEVERSION), sText);
}
}
void CToDoListWnd::OnLoad()
{
CEnFileDialog dialog(TRUE, GetDefaultFileExt(), NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST,
GetFileFilter(), this);
const UINT BUFSIZE = 1024 * 5;
static char FILEBUF[BUFSIZE] = { 0 };
CEnString sTitle(IDS_OPENTASKLIST_TITLE);
dialog.m_ofn.lpstrTitle = (LPCSTR)(LPCTSTR)sTitle;
dialog.m_ofn.lpstrFile = FILEBUF;
dialog.m_ofn.nMaxFile = BUFSIZE;
if (dialog.DoModal() == IDOK)
{
UpdateWindow();
CWaitCursor cursor;
POSITION pos = dialog.GetStartPosition();
while (pos)
{
CString sTaskList = dialog.GetNextPathName(pos);
TDC_FILE nResult = OpenTaskList(sTaskList);
if (nResult == TDCO_SUCCESS)
{
ResizeDlg();
UpdateWindow();
}
else
HandleLoadTasklistError(nResult, sTaskList);
}
RefreshTabOrder();
}
}
void CToDoListWnd::HandleLoadTasklistError(TDC_FILE nErr, LPCTSTR szTaskList)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?