home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*\
- | UIMru.cpp - DevUI(tm) library MRU list class |
- | From Developing User Interfaces for Microsoft Windows |
- | Copyright (c) 1999, Everett N. McKay |
- \*---------------------------------------------------------------------------*/
-
- #include "stdafx.h"
- #include "DevUI.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
- // class CMcMRUList
-
- // effect: (maxSize) is the maximum size of the list (-1 if no maximum) and
- // (isCaseSensitive) determines if the list is case sensitive.
- CMcMRUList::CMcMRUList (int maxSize, BOOL isCaseSensitive)
- {
- m_MaxSize = maxSize;
- m_IsCaseSensitive = isCaseSensitive;
- }
-
- // effect: Adds the most recently used string to list.
- void CMcMRUList::AddString (LPCTSTR newString)
- {
- POSITION pos;
- BOOL exactMatch;
-
- // don't add blank strings
- ASSERT(newString != 0);
- if (newString == 0 || *newString == _T('\0'))
- return;
-
- // check if string is already in list
- GetClosestMatchOffset(newString, pos, exactMatch);
- if (exactMatch)
- {
- // remove from current position
- RemoveAt(pos);
- }
-
- // add to head
- AddHead(newString);
-
- // enforce maximum size by removing excess from tail
- while (GetCount() > m_MaxSize)
- RemoveTail();
- }
-
- // effect: Sets (match) the closest matching string for (text). Returns TRUE if
- // match is exact, otherwise returns FALSE.
- BOOL CMcMRUList::GetClosestMatch (LPCTSTR text, LPTSTR match, int matchBufSize)
- {
- POSITION pos;
- CString s;
- BOOL exactMatch;
-
- ASSERT(text != 0);
- ASSERT(match != 0);
-
- // init
- *match = _T('\0');
-
- // check if string is not in list
- if (!GetClosestMatchOffset(text, pos, exactMatch))
- {
- if (*text == _T('\0'))
- return TRUE;
- else
- return FALSE;
- }
-
- // get match
- s = GetAt(pos);
-
- // copy string
- if (s.GetLength() <= matchBufSize - 1)
- {
- // copy string
- _tcscpy(match, (LPCTSTR)s);
- return exactMatch;
- }
- else
- {
- // truncate string
- _tcsncpy(match, (LPCTSTR)s, matchBufSize - 1);
- match[matchBufSize - 1] = _T('\0');
- return FALSE;
- }
- }
-
- // effect: Checks for a match, returns TRUE if found, otherwise returns FALSE. If
- // a match is found, also sets (pos) and (exactMatch).
- BOOL CMcMRUList::GetClosestMatchOffset (LPCTSTR text, POSITION &pos, BOOL &exactMatch)
- {
- CString listText;
- POSITION closestMatchPos = 0;
- int textLength = _tcslen(text);
-
- ASSERT(text != 0);
-
- // init
- exactMatch = FALSE;
- pos = 0;
- if (textLength == 0)
- return FALSE;
-
- for (pos = GetHeadPosition(); pos != 0; GetNext(pos))
- {
- // get list text
- listText = GetAt(pos);
-
- // check for exact match
- if (( m_IsCaseSensitive && _tcscmp (text, listText) == 0) ||
- (!m_IsCaseSensitive && _tcsicmp(text, listText) == 0))
- {
- exactMatch = TRUE;
- return TRUE;
- }
-
- // check for closest match - use first in MRU list
- if (closestMatchPos == 0)
- {
- if (( m_IsCaseSensitive && _tcsncmp (text, listText, textLength) == 0) ||
- (!m_IsCaseSensitive && _tcsnicmp(text, listText, textLength) == 0))
- {
- closestMatchPos = pos;
- }
- }
- }
-
- // set closest match, if any
- if (closestMatchPos > 0)
- {
- pos = closestMatchPos;
- return TRUE;
- }
- return FALSE;
- }
-
- // effect: Saves MRU to registry, entry names are based on (entryPrefix).
- HRESULT CMcMRUList::SetProfile (LPCTSTR section, LPCTSTR entryPrefix)
- {
- return McSetProfileStringList(section, entryPrefix, *this);
- }
-
- // effect: Restores MRU from registry, entry names are based on (entryPrefix).
- HRESULT CMcMRUList::GetProfile (LPCTSTR section, LPCTSTR entryPrefix)
- {
- return McGetProfileStringList(section, entryPrefix, *this);
- }
-