home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / ctlhtmlc / uimru.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-23  |  4.1 KB  |  156 lines

  1. /*---------------------------------------------------------------------------*\
  2.  | UIMru.cpp    - DevUI(tm) library MRU list class                           |
  3.  |                From Developing User Interfaces for Microsoft Windows      |
  4.  |                Copyright (c) 1999, Everett N. McKay                       |
  5. \*---------------------------------------------------------------------------*/
  6.  
  7. #include "stdafx.h"
  8. #include "DevUI.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16.  
  17. // class CMcMRUList
  18.  
  19. // effect:  (maxSize) is the maximum size of the list (-1 if no maximum) and  
  20. //          (isCaseSensitive) determines if the list is case sensitive.
  21. CMcMRUList::CMcMRUList (int maxSize, BOOL isCaseSensitive)
  22. {
  23.    m_MaxSize = maxSize;
  24.    m_IsCaseSensitive = isCaseSensitive;
  25. }
  26.  
  27. // effect:  Adds the most recently used string to list.
  28. void CMcMRUList::AddString (LPCTSTR newString)
  29. {
  30.    POSITION pos;
  31.    BOOL     exactMatch;
  32.  
  33.    // don't add blank strings
  34.    ASSERT(newString != 0);
  35.    if (newString == 0 || *newString == _T('\0'))
  36.       return;
  37.  
  38.    // check if string is already in list
  39.    GetClosestMatchOffset(newString, pos, exactMatch);
  40.    if (exactMatch)
  41.    {
  42.       // remove from current position
  43.       RemoveAt(pos);
  44.    }
  45.  
  46.    // add to head
  47.    AddHead(newString);
  48.  
  49.    // enforce maximum size by removing excess from tail
  50.    while (GetCount() > m_MaxSize)
  51.       RemoveTail();
  52. }
  53.  
  54. // effect:  Sets (match) the closest matching string for (text). Returns TRUE if 
  55. //          match is exact, otherwise returns FALSE.
  56. BOOL CMcMRUList::GetClosestMatch (LPCTSTR text, LPTSTR match, int matchBufSize)
  57. {
  58.    POSITION pos;
  59.    CString  s;
  60.    BOOL     exactMatch;
  61.  
  62.    ASSERT(text != 0);
  63.    ASSERT(match != 0);
  64.  
  65.    // init
  66.    *match = _T('\0');
  67.  
  68.    // check if string is not in list
  69.    if (!GetClosestMatchOffset(text, pos, exactMatch))
  70.    {
  71.       if (*text == _T('\0'))
  72.          return TRUE;
  73.       else
  74.          return FALSE;
  75.    }
  76.  
  77.    // get match
  78.    s = GetAt(pos);
  79.  
  80.    // copy string
  81.    if (s.GetLength() <= matchBufSize - 1)
  82.    {
  83.       // copy string
  84.       _tcscpy(match, (LPCTSTR)s);
  85.       return exactMatch;
  86.    }
  87.    else
  88.    {
  89.       // truncate string
  90.       _tcsncpy(match, (LPCTSTR)s, matchBufSize - 1);
  91.       match[matchBufSize - 1] = _T('\0');
  92.       return FALSE;
  93.    }
  94. }
  95.  
  96. // effect:  Checks for a match, returns TRUE if found, otherwise returns FALSE. If 
  97. //          a match is found, also sets (pos) and (exactMatch).
  98. BOOL CMcMRUList::GetClosestMatchOffset (LPCTSTR text, POSITION &pos, BOOL &exactMatch)
  99. {
  100.    CString  listText;
  101.    POSITION closestMatchPos = 0;
  102.    int      textLength = _tcslen(text);
  103.  
  104.    ASSERT(text != 0);
  105.  
  106.    // init
  107.    exactMatch = FALSE;
  108.    pos = 0;
  109.    if (textLength == 0)
  110.       return FALSE;
  111.  
  112.    for (pos = GetHeadPosition(); pos != 0; GetNext(pos))
  113.    {
  114.       // get list text
  115.       listText = GetAt(pos);
  116.  
  117.       // check for exact match
  118.       if (( m_IsCaseSensitive && _tcscmp (text, listText) == 0) ||
  119.           (!m_IsCaseSensitive && _tcsicmp(text, listText) == 0))
  120.       {
  121.          exactMatch = TRUE;
  122.          return TRUE;
  123.       }
  124.  
  125.       // check for closest match - use first in MRU list
  126.       if (closestMatchPos == 0)
  127.       {
  128.          if (( m_IsCaseSensitive && _tcsncmp (text, listText, textLength) == 0) ||
  129.              (!m_IsCaseSensitive && _tcsnicmp(text, listText, textLength) == 0))
  130.          {
  131.             closestMatchPos = pos;
  132.          }
  133.       }
  134.    }
  135.  
  136.    // set closest match, if any
  137.    if (closestMatchPos > 0)
  138.    {
  139.       pos = closestMatchPos;
  140.       return TRUE;
  141.    }
  142.    return FALSE;
  143. }
  144.  
  145. // effect:  Saves MRU to registry, entry names are based on (entryPrefix).
  146. HRESULT CMcMRUList::SetProfile (LPCTSTR section, LPCTSTR entryPrefix)
  147. {
  148.    return McSetProfileStringList(section, entryPrefix, *this);
  149. }
  150.  
  151. // effect:  Restores MRU from registry, entry names are based on (entryPrefix).
  152. HRESULT CMcMRUList::GetProfile (LPCTSTR section, LPCTSTR entryPrefix)
  153. {
  154.    return McGetProfileStringList(section, entryPrefix, *this); 
  155. }
  156.