home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / ComboAutoComplete.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-20  |  4.2 KB  |  126 lines

  1. // ComboAutoComplete.cpp: implementation of the CComboAutoComplete class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "ComboAutoComplete.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CComboAutoComplete::CComboAutoComplete()
  41. {
  42.     m_nLastTextLength = 0;
  43.     m_nLastEditPos = 0;
  44. }
  45.  
  46. CComboAutoComplete::~CComboAutoComplete()
  47. {
  48.  
  49. }
  50.  
  51. LRESULT CComboAutoComplete::WndProc(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
  52. {// begin WndProc
  53.     switch(nMessage)
  54.     {// begin nMessage switch
  55.     case WM_COMMAND:
  56.         if(LOWORD(wParam) == 1001)
  57.         {
  58.             switch(HIWORD(wParam))
  59.             {
  60.             case EN_CHANGE:
  61.                 OnChange(wParam,lParam);
  62.                 break;
  63.             case EN_SETFOCUS:
  64.                 OnSetFocus();
  65.                 break;
  66.             }    
  67.         }
  68.         break;
  69.     }// end nMessage swtich
  70.     return CMyComboBox::WndProc(hWnd, nMessage, wParam, lParam);
  71. }// end WndProc
  72.  
  73. void CComboAutoComplete::OnChange(WPARAM wParam,LPARAM lParam)
  74. {// begin OnChange
  75.     char *pString = NULL;
  76.     char *pFoundString = NULL;
  77.     int nLength = GetWindowTextLength(m_hWnd);
  78.     unsigned long int nEditPos = GetEditPos();
  79.     pString = new char[nLength+1];
  80.      GetWindowText(m_hWnd,pString,nLength+1);
  81.     int nIndexFound = 0;
  82.     // check if the current text length is longer what was there
  83.     // during the last OnChange
  84.     if(nLength > 0 && nLength > m_nLastTextLength)
  85.     {// begin autocomplete
  86.         // find the string in the combo box list
  87.         nIndexFound = FindString(-1, pString);
  88.         // check for a partial match of the contents of the edit box to
  89.         // the contents of the listbox
  90.         if(nIndexFound != CB_ERR)
  91.         {// begin partial match found
  92.             int nFoundLength = GetLBTextLen(nIndexFound);
  93.             // as long as there was no error
  94.             if(nFoundLength != CB_ERR && nLength >= nEditPos)
  95.             {// begin no error
  96.                 pFoundString = new char[nFoundLength+1];
  97.                 GetLBText(nIndexFound, pFoundString);
  98.                 SelectString(nIndexFound, pFoundString);
  99.                 SetEditSel(nLength, -1);
  100.                 if(GetDroppedState())
  101.                     ShowDropDown(false);
  102.             }// end no error
  103.         }// end partial match found
  104.         else
  105.             ShowDropDown(true);
  106.     }// end autocomplete
  107.     else
  108.     {// begin close dropdown
  109.         if(GetDroppedState())
  110.             ShowDropDown(false);
  111.         SetEditSel(nEditPos, -1);
  112.     }// end close dropdown
  113.     m_nLastTextLength = nLength;
  114.     m_nLastEditPos = nEditPos;
  115.     if(pString)
  116.         delete []pString;
  117.     if(pFoundString)
  118.         delete []pFoundString;
  119. }// end OnChange
  120.  
  121. void CComboAutoComplete::OnSetFocus()
  122. {// begin OnSetFocus
  123.     if(m_nLastEditPos > 0)
  124.         SetEditSel(m_nLastEditPos, -1);
  125. }// end OnSetFocus
  126.