GME  13
RecentConnStrList.cpp
Go to the documentation of this file.
00001 
00002 #include "stdafx.h"
00003 
00004 #include "RecentConnStrList.h"
00005 
00007 // CRecentConnStrList
00008 
00009 CRecentConnStrList::CRecentConnStrList(UINT nStart, LPCTSTR lpszSection,
00010         LPCTSTR lpszEntryFormat, int nSize, int nMaxDispLen)
00011 {
00012         ASSERT(nSize != 0);
00013         m_arrNames.resize(nSize);
00014 
00015         m_nStart = nStart;
00016         m_strSectionName = lpszSection;
00017         m_strEntryFormat = lpszEntryFormat;
00018         m_nMaxDisplayLength = nMaxDispLen;
00019 }
00020 
00021 CRecentConnStrList::~CRecentConnStrList()
00022 {
00023 }
00024 
00025 // Operations
00026 void CRecentConnStrList::Add(LPCTSTR lpszConnName)
00027 {
00028         ASSERT(m_arrNames.size() > 0);
00029         ASSERT(lpszConnName != NULL);
00030         ASSERT(AfxIsValidString(lpszConnName));
00031 
00032         // update the MRU list, if an existing MRU string matches conn name
00033         int iMRU;
00034         for (iMRU = 0; iMRU < (int)m_arrNames.size() - 1; iMRU++)
00035         {
00036                 if (m_arrNames[iMRU].Compare(lpszConnName) == 0)
00037                         break;      // iMRU will point to matching entry
00038         }
00039         // move MRU strings before this one down
00040         for (; iMRU > 0; iMRU--)
00041         {
00042                 ASSERT(iMRU > 0);
00043                 ASSERT(iMRU < (int)m_arrNames.size());
00044                 m_arrNames[iMRU] = m_arrNames[iMRU - 1];
00045         }
00046         // place this one at the beginning
00047         m_arrNames[0] = lpszConnName;
00048 }
00049 
00050 void CRecentConnStrList::Remove(int nIndex)
00051 {
00052         ASSERT(nIndex >= 0);
00053         ASSERT(nIndex < (int)m_arrNames.size());
00054 
00055         m_arrNames[nIndex].Empty();
00056         int iMRU;
00057         for (iMRU = nIndex; iMRU < (int)m_arrNames.size() - 1; iMRU++)
00058                 m_arrNames[iMRU] = m_arrNames[iMRU + 1];
00059 
00060         ASSERT(iMRU < (int)m_arrNames.size());
00061         m_arrNames[iMRU].Empty();
00062 }
00063 
00064 BOOL CRecentConnStrList::GetDisplayName(CString& strName, int nIndex) const
00065 {
00066         ASSERT(m_arrNames.size() > 0);
00067         ASSERT(nIndex < (int)m_arrNames.size());
00068         if (m_arrNames[nIndex].IsEmpty())
00069                 return FALSE;
00070 
00071         strName = m_arrNames[nIndex];
00072         return TRUE;
00073 }
00074 
00075 void CRecentConnStrList::UpdateMenu(CCmdUI* pCmdUI, bool enable)
00076 {
00077         ASSERT(m_arrNames.size() > 0);
00078 
00079         CMenu* pMenu = pCmdUI->m_pMenu;
00080         if (m_strOriginal.IsEmpty() && pMenu != NULL)
00081                 pMenu->GetMenuString(pCmdUI->m_nID, m_strOriginal, MF_BYCOMMAND);
00082 
00083         if (m_arrNames[0].IsEmpty())
00084         {
00085                 // no MRU files
00086                 if (!m_strOriginal.IsEmpty())
00087                         pCmdUI->SetText(m_strOriginal);
00088                 pCmdUI->Enable(FALSE);
00089                 return;
00090         }
00091 
00092         if (pCmdUI->m_pMenu == NULL)
00093                 return;
00094 
00095         for (unsigned int iMRU = 0; iMRU < m_arrNames.size(); iMRU++)
00096                 pCmdUI->m_pMenu->DeleteMenu(pCmdUI->m_nID + iMRU, MF_BYCOMMAND);
00097 
00098         if (!enable) {
00099                 pCmdUI->Enable(FALSE);
00100                 return;
00101         }
00102 
00103         CString strName;
00104         for (int iMRU = 0; iMRU < (int)m_arrNames.size(); iMRU++)
00105         {
00106                 if (!GetDisplayName(strName, iMRU))
00107                         break;
00108 
00109                 // double up any '&' characters so they are not underlined
00110                 strName.Replace(_T("&"), _T("&&"));
00111 
00112                 // insert mnemonic + the file name
00113                 CString buf;
00114                 if (iMRU < 10)  // Do not overlap mnemonic (in case of more than 10 (actually 9) MRU item)
00115                         buf.Format(_T("&%ld "), (iMRU + 1 + m_nStart) % 10);
00116                 pCmdUI->m_pMenu->InsertMenu(pCmdUI->m_nIndex,
00117                         MF_STRING | MF_BYPOSITION, pCmdUI->m_nID,
00118                         buf + strName);
00119                 // This causes error message with new MFC when we try to disable the last recent item
00120                 // So instead we don't add any disabled recent item at all (see exit above the iteration).
00121                 // Disabled recent list would occur when there's an open project without any open model,
00122                 // in case of this MFC automatically switches back from the IDR_GMETYPE menu to the IDR_MAINFRAME
00123 //              if (!enable)
00124 //                      pCmdUI->Enable(FALSE);
00125                 pCmdUI->m_nIndex++;
00126                 pCmdUI->m_nID++;
00127         }
00128 
00129         // update end menu count
00130         pCmdUI->m_nIndex--; // point to last menu added
00131         pCmdUI->m_nIndexMax = pCmdUI->m_pMenu->GetMenuItemCount();
00132 
00133         pCmdUI->m_bEnableChanged = TRUE;    // all the added items are enabled
00134 }
00135 
00136 void CRecentConnStrList::AddAndWriteList(LPCTSTR lpszConnName)
00137 {
00138         ReadList();
00139         Add(lpszConnName);
00140         WriteList();
00141 }
00142 
00143 void CRecentConnStrList::WriteList()
00144 {
00145         ASSERT(m_arrNames.size() > 0);
00146         ASSERT(!m_strSectionName.IsEmpty());
00147         ASSERT(!m_strEntryFormat.IsEmpty());
00148         CString strEntry;
00149         CWinApp* pApp = AfxGetApp();
00150         pApp->WriteProfileString(m_strSectionName, NULL, NULL);
00151         for (int iMRU = 0; iMRU < (int)m_arrNames.size(); iMRU++)
00152         {
00153                 strEntry.Format(m_strEntryFormat, iMRU + 1);
00154                 if (!m_arrNames[iMRU].IsEmpty())
00155                 {
00156                         pApp->WriteProfileString(m_strSectionName, strEntry,
00157                                 m_arrNames[iMRU]);
00158                 }
00159         }
00160 }
00161 
00162 void CRecentConnStrList::ReadList()
00163 {
00164         ASSERT(m_arrNames.size() > 0);
00165         ASSERT(!m_strSectionName.IsEmpty());
00166         ASSERT(!m_strEntryFormat.IsEmpty());
00167         CString strEntry;
00168         CWinApp* pApp = AfxGetApp();
00169         for (int iMRU = 0; iMRU < (int)m_arrNames.size(); iMRU++)
00170         {
00171                 strEntry.Format(m_strEntryFormat, iMRU + 1);
00172                 m_arrNames[iMRU] = pApp->GetProfileString(
00173                         m_strSectionName, strEntry, NULL);// '&afxChNil' previously
00174         }
00175 }
00176