todolistwnd.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,024 行 · 第 1/5 页

CPP
2,024 行
字号
{
	CEnString sMessage, sTitle(IDS_OPENTASKLIST_TITLE);
	
	switch (nErr)
	{
	case TDCO_SUCCESS:
		break; // not an error!
		
	case TDCO_CANCELLED:
		break; 
		
	case TDCO_NOTEXIST:
		sMessage.Format(IDS_TASKLISTNOTFOUND, szTaskList);
		break;
		
	case TDCO_NOTTASKLIST:
		sMessage.Format(IDS_INVALIDTASKLIST, szTaskList);
		break;
		
	case TDCO_NOTALLOWED:
		sMessage.Format(IDS_OPENACCESSDENIED, szTaskList);
		break;

	case TDCO_INUSE:
		sMessage.Format(IDS_OPENSHARINGVIOLATION, szTaskList);
		break;
		
	case TDCO_BADMSXML:
		sMessage.Format(IDS_BADXML, szTaskList);
		break;
		
	case TDCO_NOENCRYPTIONDLL:
		sMessage.Format(IDS_NOENCRYPTIONDLL, szTaskList);
		break;
		
	case TDCO_UNKNOWNENCRYPTION:
		sMessage.Format(IDS_UNKNOWNENCRYPTION, szTaskList);
		break;
		
	default: // all the other errors
		sMessage.Format(IDS_UNKNOWNOPENERROR, szTaskList, nErr - (int)TDCO_OTHER);
		break;
	}
	
	if (!sMessage.IsEmpty())
		MessageBox(sMessage, sTitle, MB_OK);
}

void CToDoListWnd::SaveSettings()
{
	// pos
	WINDOWPLACEMENT wp;
	GetWindowPlacement(&wp);
	
	AfxGetApp()->WriteProfileInt("Pos", "TopLeft", MAKELPARAM(wp.rcNormalPosition.left, wp.rcNormalPosition.top));
	AfxGetApp()->WriteProfileInt("Pos", "BottomRight", MAKELPARAM(wp.rcNormalPosition.right, wp.rcNormalPosition.bottom));
	AfxGetApp()->WriteProfileInt("Pos", "Hidden", !m_bVisible);
	AfxGetApp()->WriteProfileInt("Pos", "Maximized", IsZoomed());
	
	// version
	AfxGetApp()->WriteProfileInt("Version", "Version", GetVersion());
	
	// last open files
	int nTDC = GetTDCCount();
	int nSel = GetSelToDoCtrl(); // and last active file
	
	if (nTDC) // but don't overwrite files saved in OnQueryEndSession() or OnClose()
	{
		AfxGetApp()->WriteProfileInt("Settings", "NumLastFiles", nTDC);
		
		while (nTDC--)
		{
			CEnString sKey("LastFile%d", nTDC);
			CString sFilePath = m_mgrToDoCtrls.GetFilePath(nTDC);
			TDCM_PATHTYPE nType = m_mgrToDoCtrls.GetFilePathType(nTDC);

			// if the tasklist is on a removable drive then remove the drive letter
			if (nType == TDCM_REMOVABLE)
				sFilePath = sFilePath.Mid(1);

			AfxGetApp()->WriteProfileString("Settings", sKey, sFilePath);
			
			if (nSel == nTDC)
				AfxGetApp()->WriteProfileString("Settings", "LastActiveFile", sFilePath);
		}
	}
	
	// other settings
	AfxGetApp()->WriteProfileInt("Settings", "SimpleMode", m_bMaxTasklists);
	AfxGetApp()->WriteProfileInt("Settings", "ShowFilterBar", m_bShowFilterBar);
	AfxGetApp()->WriteProfileInt("Settings", "ToolbarOption", m_bShowToolbar ? TB_TOOLBARANDMENU : TB_TOOLBARHIDDEN);
	AfxGetApp()->WriteProfileInt("Settings", "ShowProjectName", m_bShowProjectName);
	AfxGetApp()->WriteProfileInt("Settings", "ShowStatusBar", m_bShowStatusBar);

	if (m_findDlg.GetSafeHwnd())
		AfxGetApp()->WriteProfileInt("Settings", "FindTasksVisible", m_bFindShowing && m_findDlg.IsWindowVisible());
	
	if (Prefs().GetAddFilesToMRU())
		m_mruList.WriteList();
}

LRESULT CToDoListWnd::OnWebUpdateWizard(WPARAM /*wp*/, LPARAM /*lp*/)
{
	ASSERT (Prefs().GetAutoCheckForUpdates());

	CheckForUpdates(FALSE);
	return 0L;
}

LRESULT CToDoListWnd::OnAddToolbarTools(WPARAM /*wp*/, LPARAM /*lp*/)
{
	Misc::ProcessMsgLoop();
	AppendTools2Toolbar();
	return 0L;
}

void CToDoListWnd::PrepareOpenFilePath(CString& sFilePath)
{
	// if it starts with a colon then we need to find the 
	// removable drive it's stored on
	if (!sFilePath.IsEmpty() && sFilePath[0] == ':')
	{
		for (int nDrive = 4; nDrive <= 26; nDrive++) // from D: upwards
		{
			if (CDriveInfo::GetType(nDrive) == DRIVE_REMOVABLE)
			{
				CString sTryPath = CDriveInfo::GetLetter(nDrive) + sFilePath;

				if (GetFileAttributes(sTryPath) != 0xffffffff)
				{
					sFilePath = sTryPath;
					break; // finished
				}
			}
		}
	}
}

LRESULT CToDoListWnd::OnPostOnCreate(WPARAM /*wp*/, LPARAM /*lp*/)
{
	CAutoFlag af(m_bReloading, TRUE);
	CPreferencesDlg& prefs = Prefs();

	RestoreVisibility();
	LockWindowUpdate();
	
	// open cmdline tasklist
	int nTDCCount = AfxGetApp()->GetProfileInt("Settings", "NumLastFiles", 0);

	// if we have a file on the commandline or any previous tasklists
	// set the prompt of the initial tasklist to something appropriate
	if (!m_startupOptions.sFilePath.IsEmpty() || nTDCCount)
	{
		//	TODO
	}
	
	BOOL bOpen = FALSE;

	if (!m_startupOptions.sFilePath.IsEmpty())
	{
		PrepareOpenFilePath(m_startupOptions.sFilePath);

		bOpen = (TDCO_SUCCESS == OpenTaskList(m_startupOptions.sFilePath));

		// other startup options
		if (bOpen)
		{
			CFilteredToDoCtrl& tdc = GetToDoCtrl();

			if (m_startupOptions.dwIDSel)
				tdc.SelectTask(m_startupOptions.dwIDSel);

			else if (!m_startupOptions.sNewTask.IsEmpty())
			{
				tdc.NewTask(m_startupOptions.sNewTask, TDC_INSERTATTOP, TRUE, FALSE);

				if (!m_startupOptions.sComments.IsEmpty())
					tdc.SetSelectedTaskComments(m_startupOptions.sComments, m_startupOptions.sComments);
			}
		}
	}
	
	m_startupOptions.Clear(); // always
	
	// load last files
	if (!bOpen)
	{
		CString sLastActiveFile = AfxGetApp()->GetProfileString("Settings", "LastActiveFile");
		PrepareOpenFilePath(sLastActiveFile);
		
		for (int nTDC = 0; nTDC < nTDCCount; nTDC++)
		{
			CEnString sKey("LastFile%d", nTDC);
			CString sLastFile = AfxGetApp()->GetProfileString("Settings", sKey);
			
			// delay open all but the non-active tasklist
			if (!sLastFile.IsEmpty())
			{
				PrepareOpenFilePath(sLastFile);

				if (sLastFile != sLastActiveFile && prefs.GetEnableDelayedLoading())
					DelayOpenTaskList(sLastFile);
				else
					OpenTaskList(sLastFile, FALSE);
			}

			// process all pending messages
			Misc::ProcessMsgLoop();
		}

		// select
		if (GetTDCCount())
		{
			if (!SelectToDoCtrl(sLastActiveFile, FALSE))
				SelectToDoCtrl(0, FALSE); // the first one

			ResizeDlg();
		}
	}

	// if there's only one tasklist open and it's pristine then 
	// it's the original one so add a sample task
	if (m_mgrToDoCtrls.IsPristine(0))
	{
		ASSERT (GetTDCCount() == 1);

		CToDoCtrl& tdc = GetToDoCtrl();
		ASSERT (tdc.GetTaskCount() == 0);

		tdc.NewTask(CEnString(IDS_SAMPLETASK), TDC_INSERTATTOP, TRUE, FALSE);
		tdc.SetModified(FALSE);
		
		m_mgrToDoCtrls.SetModifiedStatus(0, FALSE);

		UpdateCaption();
	}
	else // due task notifications
	{
		int nDueBy = prefs.GetNotifyDueByOnLoad();
		
		if (nDueBy != PFP_DONTNOTIFY)
		{
			UpdateWindow();
			m_tabCtrl.UpdateWindow();
			
			int nCtrls = GetTDCCount();
			
			for (int nCtrl = 0; nCtrl < nCtrls; nCtrl++)
			{
				if (m_mgrToDoCtrls.IsLoaded(nCtrl))
					DoDueTaskNotification(&GetToDoCtrl(nCtrl), nDueBy);
			}
		}
	}

	UnlockWindowUpdate();
	
	// find tasks dialog
	if (AfxGetApp()->GetProfileInt("Settings", "FindTasksVisible", 0))
	{
		OnFindTasks();
		
		if (prefs.GetRefreshFindOnLoad())
			m_findDlg.RefreshSearch();
	}

	// refresh toolbar 'tools' buttons unless minimized because
	// we must handle it when we're first shown
	if (m_bShowToolbar && AfxGetApp()->m_nCmdShow != SW_SHOWMINIMIZED)
		AppendTools2Toolbar();

	InitMenuIconManager();
	
	// web update
	if (Prefs().GetAutoCheckForUpdates())
		PostMessage(WM_WEBUPDATEWIZARD);

	return 0L;
}

void CToDoListWnd::CheckForUpdates(BOOL bManual)
{
	// only check once a day
	int nLastUpdate = AfxGetApp()->GetProfileInt("Updates", "LastUpdate", 0);

	if (!bManual && nLastUpdate >= (int)COleDateTime::GetCurrentTime().m_dt)
		return;

	AfxGetApp()->WriteProfileInt("Updates", "LastUpdate", (int)COleDateTime::GetCurrentTime().m_dt);

	// get the app wuw path
	CString sFolder, sDrive;
	CString sWuwPath = FileMisc::GetModuleFileName();

	FileMisc::SplitPath(sWuwPath, &sDrive, &sFolder);
	FileMisc::MakePath(sWuwPath, sDrive, sFolder, "WebUpdateSvc.exe");

	// check for existence if manual
	if (bManual && !FileMisc::FileExists(sWuwPath))
	{
		const LPCTSTR DOWNLOAD_WUW_PATH = "http://www.abstractspoon.com/todolist_wuw.zip";

		if (AfxMessageBox(IDS_NOWUW, MB_YESNO) == IDYES)
			ShellExecute(NULL, "open", DOWNLOAD_WUW_PATH, NULL, NULL, SW_HIDE);
		else
			return;
	}

	const LPCTSTR UPDATE_SCRIPT_PATH = "http://www.abstractspoon.com/todolist_update.txt";
	const LPCTSTR UPDATE_SCRIPT_PATH_MANUAL = "http://www.abstractspoon.com/todolist_update_manual.txt";

	if (bManual)
		ShellExecute(NULL, "open", sWuwPath, UPDATE_SCRIPT_PATH_MANUAL, NULL, SW_HIDE);
	else
		ShellExecute(NULL, "open", sWuwPath, UPDATE_SCRIPT_PATH, NULL, SW_HIDE);
} 
	
void CToDoListWnd::LoadSettings()
{
	CPreferencesDlg& prefs = Prefs();
	
	// MRU
	if (prefs.GetAddFilesToMRU())
		m_mruList.ReadList();
	
	// settings
	m_bMaxTasklists = AfxGetApp()->GetProfileInt("Settings", "SimpleMode", m_bMaxTasklists);
	m_bShowFilterBar = AfxGetApp()->GetProfileInt("Settings", "ShowFilterBar", m_bShowFilterBar);
	m_bShowProjectName = AfxGetApp()->GetProfileInt("Settings", "ShowProjectName", m_bShowProjectName);

	m_bShowStatusBar = AfxGetApp()->GetProfileInt("Settings", "ShowStatusBar", m_bShowStatusBar);
	m_statusBar.ShowWindow(m_bShowStatusBar ? SW_SHOW : SW_HIDE);
	
	// toolbar
	m_bShowToolbar = (AfxGetApp()->GetProfileInt("Settings", "ToolbarOption", TB_TOOLBARANDMENU) != TB_TOOLBARHIDDEN);
	m_toolbar.ShowWindow(m_bShowToolbar ? SW_SHOW : SW_HIDE);
	m_toolbar.EnableWindow(m_bShowToolbar);

	RefreshFilterLabelAlignment();
	
	CXmlFile::EnableFormattedOutput(prefs.GetFormatXmlOutput());
	
	// pos
	RestorePosition();

	// note: we do not restore visibility until OnPostOnCreate
	
	// hotkey
	UpdateGlobalHotkey();
	
	// time periods
	CTimeHelper::SetHoursInOneDay(prefs.GetHoursInOneDay());
	CTimeHelper::SetDaysInOneWeek(prefs.GetDaysInOneWeek());

	// support for .tdl
	CFileRegister filereg("tdl", "tdl_Tasklist");
	
	if (prefs.GetEnableTDLExtension())
		filereg.RegisterFileType("Tasklist", 0);
	else
		filereg.UnRegisterFileType();

	// support for tdl protocol
	EnableTDLProtocol(prefs.GetEnableTDLProtocol());
}

void CToDoListWnd::EnableTDLProtocol(BOOL bEnable)
{
	if (bEnable)
	{
		CRegKey reg;

		if (reg.Open(HKEY_CLASSES_ROOT, "tdl") == ERROR_SUCCESS)
		{
			reg.Write("", "URL: ToDoList protocol");
			reg.Write("URL Protocol", "");

			// write exe name out
			CString sAppPath = FileMisc::GetModuleFileName() + " -l \"%1\"";

			reg.Close();

			if (reg.Open(HKEY_CLASSES_ROOT, "tdl\\shell\\open\\command") == ERROR_SUCCESS)
				reg.Write("", sAppPath);
		}
	}
	else
		CRegKey::Delete(HKEY_CLASSES_ROOT, "tdl");
}

void CToDoListWnd::RestoreVisibility()
{
	CPreferencesDlg& prefs = Prefs();

	int nDefShowState = AfxGetApp()->m_nCmdShow;
	BOOL bShowOnStartup = prefs.GetShowOnStartup();

	BOOL bMaximized = AfxGetApp()->GetProfileInt("Pos", "Maximized", FALSE) || (nDefShowState == SW_SHOWMAXIMIZED);
	BOOL bMinimized = !bShowOnStartup && (nDefShowState == SW_SHOWMINIMIZED || nDefShowState == SW_SHOWMINNOACTIVE);
	
	if (bMinimized)
	{
		bMaximized = FALSE; // can't be max-ed and min-ed
		m_bStartHidden = TRUE;
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?