AutoCompl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ////////////////////////////////////////////////////////////////
  2. // VCKBASE -- August 2000
  3. // Compiles with Visual C++ 6.0, runs on Windows 98 and probably NT too.
  4. //
  5. #include "stdafx.h"
  6. #include "autocompl.h"
  7. #include "LYFZIPManage.h"
  8. //////////////////
  9. // ctor: initialize stuff to zero
  10. //
  11. CAutoComplete::CAutoComplete()
  12. {
  13. m_bIgnoreChangeMsg=0;
  14. m_iType = 0;
  15. m_idMyControl = 0;
  16. m_iCurString = 0;
  17. }
  18. CAutoComplete::~CAutoComplete()
  19. {
  20. }
  21. //////////////////
  22. // Install hook. Initialize control ID and type of control based on
  23. // classname
  24. //
  25. void CAutoComplete::Init(CWnd* pWnd)
  26. {
  27. CSubclassWnd::HookWindow(pWnd->GetParent());
  28. CString sClassName;
  29. ::GetClassName(pWnd->GetSafeHwnd(), sClassName.GetBuffer(32), 32);
  30. sClassName.ReleaseBuffer();
  31. if (sClassName=="Edit") {
  32. m_iType = Edit;
  33. } else if (sClassName=="ComboBox") {
  34. m_iType = ComboBox;
  35. }
  36. m_idMyControl = pWnd->GetDlgCtrlID();
  37. }
  38. //////////////////
  39. // Scan string array for strings that match text, and add the matches
  40. // to a new array. Returns number of matches. For edit controls, only
  41. // need to find the first match, so to be efficient BOOL arg tells me
  42. // that.
  43. //
  44. UINT CAutoComplete::GetMatches(LPCTSTR pszText, CStringArray& arMatches,
  45. BOOL bFirstOnly)
  46. {
  47. arMatches.RemoveAll();
  48. int nMatch = 0;
  49. CString s=pszText;
  50. if (s.GetLength()>0) {
  51. OnFirstString();
  52. CString sMatch;
  53. while (OnNextString(sMatch)) {
  54. if (OnMatchString(s, sMatch)) {
  55. TRACE("Add %s\n",(LPCTSTR)sMatch);
  56. arMatches.Add(sMatch);
  57. nMatch++;
  58. if (bFirstOnly)
  59. break;
  60. }
  61. }
  62. }
  63. return nMatch;
  64. }
  65. //////////////////
  66. // This virtual function takes the string entered and a potential match
  67. // and returns TRUE if the strings match. Default implementation does a
  68. // normal prefix compare--but you could override, for example to ignore
  69. // 'www' at the start of either string.
  70. //
  71. BOOL CAutoComplete::OnMatchString(const CString& s, const CString& sMatch)
  72. {
  73. return s==sMatch.Left(s.GetLength());
  74. }
  75. void CAutoComplete::OnFirstString()
  76. {
  77. m_iCurString=0;
  78. }
  79. BOOL CAutoComplete::OnNextString(CString& sNext)
  80. {
  81. if (m_iCurString < m_arStrings.GetSize()) {
  82. sNext = m_arStrings[m_iCurString++];
  83. return TRUE;
  84. }
  85. sNext = (LPCTSTR)NULL;
  86. return FALSE;
  87. }
  88. //////////////////
  89. // "hook" function traps messages sent to edit control/combobox. I am
  90. // interested in EN_CHANGE or CBN_EDITCHANGE: contents of edit control
  91. // have changed.
  92. //
  93. LRESULT CAutoComplete::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
  94. {
  95. if ((msg==WM_COMMAND && LOWORD(wp)==m_idMyControl) &&
  96. ((m_iType==Edit && HIWORD(wp)==EN_CHANGE) ||
  97. (m_iType==ComboBox && HIWORD(wp)==CBN_EDITCHANGE))) {
  98. // since I will be changing the contents of the control, which
  99. // will trigger more EN_CHANGE messages, turn off processing
  100. // while I have control, using m_bIgnoreChangeMsg.
  101. if (!m_bIgnoreChangeMsg++) {
  102. CString s;
  103. CWnd* pWnd = CWnd::FromHandle((HWND)lp);
  104. pWnd->GetWindowText(s);
  105. OnComplete(pWnd, s);
  106. }
  107. m_bIgnoreChangeMsg--;
  108. }
  109. return CSubclassWnd::WindowProc(msg, wp, lp);
  110. }
  111. //////////////////
  112. // This is the main function that does the completion.
  113. //
  114. void CAutoComplete::OnComplete(CWnd* pWnd, CString m_filter)
  115. {
  116. CStringArray arMatches; // strings that match
  117. // if (s.GetLength()>0 && GetMatches(s, arMatches, m_iType==Edit)>0) {
  118. // DoCompletion(pWnd, s, arMatches);
  119. // }
  120. m_filter.TrimLeft ();
  121. int ii;
  122. if (m_filter.GetLength()>0)
  123. {
  124. m_posarray.RemoveAll ();
  125. int type=GetType(m_filter);
  126. if(type==1)//µç»°
  127. {
  128. for(ii=0; ii<m_pArray->GetSize (); ii++)
  129. {
  130. if(m_pArray->ElementAt (ii).ElementAt (12).Find (m_filter)!=-1 || \
  131. m_pArray->ElementAt (ii).ElementAt (13).Find (m_filter)!=-1)
  132. {
  133. m_posarray.Add (ii);
  134. arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
  135. }
  136. }
  137. }
  138. else if(type==2)//Æ´Òô
  139. {
  140. m_filter.MakeUpper ();
  141. for(ii=0; ii<m_pArray->GetSize (); ii++)
  142. {
  143. if(m_pArray->ElementAt (ii).ElementAt (14).Find (m_filter)!=-1 || \
  144. m_pArray->ElementAt (ii).ElementAt (15).Find (m_filter)!=-1)
  145. {
  146. m_posarray.Add (ii);
  147. arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
  148. }
  149. }
  150. }
  151. else
  152. {
  153. for(ii=0; ii<m_pArray->GetSize (); ii++)
  154. {
  155. if(m_pArray->ElementAt (ii).ElementAt (0).Find (m_filter)!=-1 ||\
  156. m_pArray->ElementAt (ii).ElementAt (1).Find (m_filter)!=-1 || \
  157. m_pArray->ElementAt (ii).ElementAt (2).Find (m_filter)!=-1 || \
  158. m_pArray->ElementAt (ii).ElementAt (7).Find (m_filter)!=-1 || \
  159. m_pArray->ElementAt (ii).ElementAt (8).Find (m_filter)!=-1 || \
  160. m_pArray->ElementAt (ii).ElementAt (9).Find (m_filter)!=-1|| \
  161. m_pArray->ElementAt (ii).ElementAt (10).Find (m_filter)!=-1|| \
  162. m_pArray->ElementAt (ii).ElementAt (12).Find (m_filter)!=-1 || \
  163. m_pArray->ElementAt (ii).ElementAt (13).Find (m_filter)!=-1 )
  164. {
  165. m_posarray.Add (ii);
  166. arMatches.Add (m_pArray->ElementAt (ii).ElementAt (1)+m_pArray->ElementAt (ii).ElementAt (2));
  167. }
  168. }
  169. }
  170. DoCompletion(pWnd, m_filter, arMatches);
  171. }
  172. m_sPrevious=m_filter; // remember current string
  173. }
  174. void CAutoComplete::DoCompletion(CWnd* pWnd, CString s, //m_pArray
  175. const CStringArray& arMatches)
  176. {
  177. if (m_iType==ComboBox) {
  178. // This cast is technically incorrect, but a standard MFC trick.
  179. CComboBox* pComboBox = (CComboBox*)pWnd;
  180. // update dropdown to reflect possible matches
  181. pComboBox->ResetContent();
  182. for (int i=0; i<arMatches.GetSize(); i++) {
  183. pComboBox->AddString(arMatches[i]);
  184. }
  185. // user arrow cursor so user can select
  186. ::SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
  187. // show dropdown
  188. pComboBox->ShowDropDown();
  189. // pComboBox->SetWindowText(IgnoreCompletion(s) ? s : arMatches[0]);
  190. pComboBox->SetWindowText(s);
  191. pComboBox->SetEditSel(s.GetLength(),-1);
  192. } else if (m_iType==Edit && !IgnoreCompletion(s)) {
  193. // This cast is technically incorrect, but a standard MFC trick.
  194. CEdit* pEdit = (CEdit*)pWnd;
  195. pEdit->SetWindowText(arMatches[0]);
  196. pEdit->SetSel(s.GetLength(),-1);
  197. }
  198. }
  199. //////////////////
  200. // This function is used to turn off the completion feature when the
  201. // user presses Backspace to delete a character typed. In that case,
  202. // the current string (s) will match the last (previously) entered
  203. // string. If this is the case, don't do the completion. For example,
  204. // if user types 'foo' and this causes me to complete to 'foobar', with
  205. // 'bar' highlighted, I don't want to complete to foobar again if the
  206. // user presses Backspace or Delete to delete 'bar'. Instead, 'foo'
  207. // should remain--and likewise if the user keeps pressing backspace.
  208. // This is one of the only functions I have ever written where the
  209. // explanation is longer than the code itself!
  210. //
  211. BOOL CAutoComplete::IgnoreCompletion(CString s)
  212. {
  213. return s==m_sPrevious.Left(s.GetLength());
  214. }