HistoryCombo.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:7k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////
- #include "stdafx.h"
- #include "HistoryCombo.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define MAX_HISTORY_ITEMS 50
- /////////////////////////////////////////////////////////////////////////////
- // CHistoryCombo
- CHistoryCombo::CHistoryCombo(BOOL bAllowSortStyle/*=FALSE*/)
- {
- m_nMaxHistoryItems = MAX_HISTORY_ITEMS;
- m_bSaveRestoreLastCurrent = TRUE;
- m_bAllowSortStyle = bAllowSortStyle;
- }
- CHistoryCombo::~CHistoryCombo()
- {
- }
- BOOL CHistoryCombo::PreCreateWindow(CREATESTRUCT& cs)
- {
- if (! m_bAllowSortStyle) // turn off CBS_SORT style
- cs.style &= ~CBS_SORT;
- return CComboBox::PreCreateWindow(cs);
- }
- void CHistoryCombo::PreSubclassWindow()
- {
- // warn if creating with CBS_SORT style
- // (unfortunately we can't turn it off)
- if (! m_bAllowSortStyle && GetStyle() & CBS_SORT)
- TRACE(_T("WARNING: Creating History combo with CBS_SORT stylen"));
- CComboBox::PreSubclassWindow();
- }
- BEGIN_MESSAGE_MAP(CHistoryCombo, CComboBox)
- //{{AFX_MSG_MAP(CHistoryCombo)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- int CHistoryCombo::AddString(LPCTSTR lpszString)
- {
- if (m_sSection.IsEmpty() || m_sKeyPrefix.IsEmpty())
- return CComboBox::AddString(lpszString);
- int nRet = -1;
- CString sString(lpszString);
- sString.TrimLeft(_T(" "));
- sString.TrimRight(_T(" "));
- nRet = CComboBox::InsertString(0, sString);
- int nIndex = FindStringExact(0, sString);
- if (nIndex != -1 && nIndex != 0)
- DeleteString(nIndex);
- int nNumItems = GetCount();
- for (int n = m_nMaxHistoryItems; n < nNumItems; n++)
- DeleteString(m_nMaxHistoryItems);
- SetCurSel(nRet);
- return nRet;
- }
- CString CHistoryCombo::LoadHistory(CRecentFileList* pListMRU, BOOL bSelectMostRecent/*=TRUE*/)
- {
- if (pListMRU == NULL)
- return "";
- int nNumItems = pListMRU->GetSize();
- for (int n = 0; n < nNumItems; n++)
- CComboBox::AddString((*pListMRU)[n]);
- if (bSelectMostRecent)
- SetCurSel(0);
- CString sText;
- GetWindowText(sText);
- return sText;
- }
- CString CHistoryCombo::LoadHistory(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix,
- BOOL bSaveRestoreLastCurrent/*=TRUE*/,
- LPCTSTR lpszKeyCurItem/*=NULL*/)
- {
- if (lpszSection == NULL || lpszKeyPrefix == NULL || *lpszSection == ' ')
- return "";
- m_sSection = lpszSection;
- m_sKeyPrefix = lpszKeyPrefix;
- m_sKeyCurItem = lpszKeyCurItem == NULL ? _T("") : lpszKeyCurItem;
- m_bSaveRestoreLastCurrent = bSaveRestoreLastCurrent;
- CWinApp* pApp = AfxGetApp();
- int n = 0;
- CString sText;
- do
- {
- CString sKey;
- sKey.Format(_T("%s%d"), m_sKeyPrefix, n++);
- sText = pApp->GetProfileString(m_sSection, sKey);
- if (!sText.IsEmpty())
- CComboBox::AddString(sText);
- }while (!sText.IsEmpty() && n < m_nMaxHistoryItems);
- if (m_bSaveRestoreLastCurrent)
- {
- CString sKey;
- if (!m_sKeyCurItem.IsEmpty())
- sKey = m_sKeyCurItem;
- else if (m_sKeyPrefix.IsEmpty())
- sKey = "Last";
- else
- sKey = m_sKeyPrefix;
- sText = pApp->GetProfileString(m_sSection, sKey);
- if (!sText.IsEmpty())
- {
- int nIndex = FindStringExact(-1, sText);
- if (nIndex != -1)
- SetCurSel(nIndex);
- else if (GetStyle() & CBS_DROPDOWN)
- SetWindowText(sText);
- }
- }
- return sText;
- }
- void CHistoryCombo::SaveHistory(BOOL bAddCurrentItemToHistory/*=TRUE*/)
- {
- if (m_sSection.IsEmpty())
- return;
- CWinApp* pApp = AfxGetApp();
- ASSERT(pApp);
- if (bAddCurrentItemToHistory)
- {
- CString sCurItem;
- GetWindowText(sCurItem);
- // trim it, so we items which differ only by a leading/trailing space
- sCurItem.TrimLeft();
- sCurItem.TrimRight();
- if (! sCurItem.IsEmpty())
- AddString(sCurItem);
- }
- // save history to info cached earlier
- int nMax = min(GetCount(), m_nMaxHistoryItems + 1);
- for (int n = 0; n < nMax; n++)
- {
- CString sKey;
- sKey.Format(_T("%s%d"), m_sKeyPrefix, n);
- CString sText;
- GetLBText(n, sText);
- pApp->WriteProfileString(m_sSection, sKey, sText);
- }
- for (n = nMax; n < 1000/* prevent runaway*/; n++)
- {
- CString sKey;
- sKey.Format(_T("%s%d"), m_sKeyPrefix, n);
- CString sText = pApp->GetProfileString(m_sSection, sKey);
- if (sText.IsEmpty())
- break;
- pApp->WriteProfileString(m_sSection, sKey, NULL); // remove entry
- }
- if (m_bSaveRestoreLastCurrent)
- {
- CString sText;
- GetWindowText(sText);
- CString sKey;
- if (!m_sKeyCurItem.IsEmpty())
- sKey = m_sKeyCurItem;
- else if (m_sKeyPrefix.IsEmpty())
- sKey = "Last";
- else
- sKey = m_sKeyPrefix;
- pApp->WriteProfileString(m_sSection, sKey, sText);
- }
- }
- void CHistoryCombo::ClearHistory(BOOL bDeleteRegistryEntries/*=TRUE*/)
- {
- ResetContent();
- if (! m_sSection.IsEmpty() && bDeleteRegistryEntries)
- {
- // remove profile entries
- CWinApp* pApp = AfxGetApp();
- ASSERT(pApp);
- CString sKey;
- for (int n = 0; n < 1000/* prevent runaway*/; n++)
- {
- sKey.Format(_T("%s%d"), m_sKeyPrefix, n);
- CString sText = pApp->GetProfileString(m_sSection, sKey);
- if (sText.IsEmpty())
- break;
- pApp->WriteProfileString(m_sSection, sKey, NULL); // remove entry
- }
- if (! m_sKeyCurItem.IsEmpty())
- sKey = m_sKeyCurItem;
- else if (m_sKeyPrefix.IsEmpty())
- sKey = "Last";
- else
- sKey = m_sKeyPrefix;
- pApp->WriteProfileString(m_sSection, sKey, NULL);
- }
- }
- void CHistoryCombo::SetMaxHistoryItems(int nMaxItems)
- {
- m_nMaxHistoryItems = nMaxItems;
- int nNumItems = GetCount();
- for (int n = m_nMaxHistoryItems; n < nNumItems; n++)
- DeleteString(m_nMaxHistoryItems);
- }
- void CHistoryCombo::StoreValue(BOOL bIgnoreIfEmpty/*=TRUE*/)
- {
- CString sValue;
- GetWindowText(sValue);
- if (bIgnoreIfEmpty && sValue.IsEmpty())
- return;
- AddString(sValue);
- }