Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4233 lines
135 KiB

  1. // Copyright 1996-98 Microsoft
  2. #include "priv.h"
  3. #include "sccls.h"
  4. #include "autocomp.h"
  5. #include "itbar.h"
  6. #include "address.h"
  7. #include "addrlist.h"
  8. #include "resource.h"
  9. #include "mluisupp.h"
  10. #include "apithk.h"
  11. extern HRESULT CACLMRU_CreateInstance(IUnknown *punkOuter, IUnknown **ppunk, LPCOBJECTINFO poi, LPCTSTR pszMRU);
  12. #define WZ_REGKEY_QUICKCOMPLETE L"Software\\Microsoft\\Internet Explorer\\Toolbar\\QuickComplete"
  13. #define WZ_DEFAULTQUICKCOMPLETE L"http://www.%s.com"
  14. // Statics
  15. const static TCHAR c_szAutoDefQuickComp[] = TEXT("%s");
  16. const static TCHAR c_szAutoCompleteProp[] = TEXT("CAutoComplete_This");
  17. const static TCHAR c_szParentWindowProp[] = TEXT("CParentWindow_This");
  18. const static TCHAR c_szAutoSuggest[] = TEXT("AutoSuggest Drop-Down");
  19. const static TCHAR c_szAutoSuggestTitle[] = TEXT("Internet Explorer");
  20. BOOL CAutoComplete::s_fNoActivate = FALSE;
  21. HWND CAutoComplete::s_hwndDropDown = NULL;
  22. HHOOK CAutoComplete::s_hhookMouse = NULL;
  23. #define MAX_QUICK_COMPLETE_STRING 64
  24. #define LISTVIEW_COLUMN_WIDTH 30000
  25. //
  26. // FLAGS for dwFlags
  27. //
  28. #define ACF_RESET 0x00000000
  29. #define ACF_IGNOREUPDOWN 0x00000004
  30. #define URL_SEPARATOR_CHAR TEXT('/')
  31. #define DIR_SEPARATOR_CHAR TEXT('\\')
  32. #define DIR_SEPARATOR_STRING TEXT("\\")
  33. /////////////////////////////////////////////////////////////////////////////
  34. // Line Break Character Table
  35. //
  36. // This was swipped from mlang. Special break characters added for URLs
  37. // have an "IE:" in the comment. Note that this table must be sorted!
  38. const WCHAR g_szBreakChars[] = {
  39. 0x0009, // TAB
  40. 0x0020, // SPACE
  41. 0x0021, // IE: !
  42. 0x0022, // IE: "
  43. 0x0023, // IE: #
  44. 0x0024, // IE: $
  45. 0x0025, // IE: %
  46. 0x0026, // IE: &
  47. 0x0027, // IE: '
  48. 0x0028, // LEFT PARENTHESIS
  49. 0x0029, // RIGHT PARENTHESIS
  50. 0x002A, // IE: *
  51. 0x002B, // IE: +
  52. 0x002C, // IE: ,
  53. 0x002D, // HYPHEN
  54. 0x002E, // IE: .
  55. 0x002F, // IE: /
  56. 0x003A, // IE: :
  57. 0x003B, // IE: ;
  58. 0x003C, // IE: <
  59. 0x003D, // IE: =
  60. 0x003E, // IE: >
  61. 0x003F, // IE: ?
  62. 0x0040, // IE: @
  63. 0x005B, // LEFT SQUARE BRACKET
  64. 0x005C, // IE: '\'
  65. 0x005D, // RIGHT SQUARE BRACKET
  66. 0x005E, // IE: ^
  67. 0x005F, // IE: _
  68. 0x0060, // IE:`
  69. 0x007B, // LEFT CURLY BRACKET
  70. 0x007C, // IE: |
  71. 0x007D, // RIGHT CURLY BRACKET
  72. 0x007E, // IE: ~
  73. 0x00AB, // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
  74. 0x00AD, // OPTIONAL HYPHEN
  75. 0x00BB, // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
  76. 0x02C7, // CARON
  77. 0x02C9, // MODIFIER LETTER MACRON
  78. 0x055D, // ARMENIAN COMMA
  79. 0x060C, // ARABIC COMMA
  80. 0x2002, // EN SPACE
  81. 0x2003, // EM SPACE
  82. 0x2004, // THREE-PER-EM SPACE
  83. 0x2005, // FOUR-PER-EM SPACE
  84. 0x2006, // SIX-PER-EM SPACE
  85. 0x2007, // FIGURE SPACE
  86. 0x2008, // PUNCTUATION SPACE
  87. 0x2009, // THIN SPACE
  88. 0x200A, // HAIR SPACE
  89. 0x200B, // ZERO WIDTH SPACE
  90. 0x2013, // EN DASH
  91. 0x2014, // EM DASH
  92. 0x2016, // DOUBLE VERTICAL LINE
  93. 0x2018, // LEFT SINGLE QUOTATION MARK
  94. 0x201C, // LEFT DOUBLE QUOTATION MARK
  95. 0x201D, // RIGHT DOUBLE QUOTATION MARK
  96. 0x2022, // BULLET
  97. 0x2025, // TWO DOT LEADER
  98. 0x2026, // HORIZONTAL ELLIPSIS
  99. 0x2027, // HYPHENATION POINT
  100. 0x2039, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
  101. 0x203A, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
  102. 0x2045, // LEFT SQUARE BRACKET WITH QUILL
  103. 0x2046, // RIGHT SQUARE BRACKET WITH QUILL
  104. 0x207D, // SUPERSCRIPT LEFT PARENTHESIS
  105. 0x207E, // SUPERSCRIPT RIGHT PARENTHESIS
  106. 0x208D, // SUBSCRIPT LEFT PARENTHESIS
  107. 0x208E, // SUBSCRIPT RIGHT PARENTHESIS
  108. 0x226A, // MUCH LESS THAN
  109. 0x226B, // MUCH GREATER THAN
  110. 0x2574, // BOX DRAWINGS LIGHT LEFT
  111. 0x3001, // IDEOGRAPHIC COMMA
  112. 0x3002, // IDEOGRAPHIC FULL STOP
  113. 0x3003, // DITTO MARK
  114. 0x3005, // IDEOGRAPHIC ITERATION MARK
  115. 0x3008, // LEFT ANGLE BRACKET
  116. 0x3009, // RIGHT ANGLE BRACKET
  117. 0x300A, // LEFT DOUBLE ANGLE BRACKET
  118. 0x300B, // RIGHT DOUBLE ANGLE BRACKET
  119. 0x300C, // LEFT CORNER BRACKET
  120. 0x300D, // RIGHT CORNER BRACKET
  121. 0x300E, // LEFT WHITE CORNER BRACKET
  122. 0x300F, // RIGHT WHITE CORNER BRACKET
  123. 0x3010, // LEFT BLACK LENTICULAR BRACKET
  124. 0x3011, // RIGHT BLACK LENTICULAR BRACKET
  125. 0x3014, // LEFT TORTOISE SHELL BRACKET
  126. 0x3015, // RIGHT TORTOISE SHELL BRACKET
  127. 0x3016, // LEFT WHITE LENTICULAR BRACKET
  128. 0x3017, // RIGHT WHITE LENTICULAR BRACKET
  129. 0x3018, // LEFT WHITE TORTOISE SHELL BRACKET
  130. 0x3019, // RIGHT WHITE TORTOISE SHELL BRACKET
  131. 0x301A, // LEFT WHITE SQUARE BRACKET
  132. 0x301B, // RIGHT WHITE SQUARE BRACKET
  133. 0x301D, // REVERSED DOUBLE PRIME QUOTATION MARK
  134. 0x301E, // DOUBLE PRIME QUOTATION MARK
  135. 0x3041, // HIRAGANA LETTER SMALL A
  136. 0x3043, // HIRAGANA LETTER SMALL I
  137. 0x3045, // HIRAGANA LETTER SMALL U
  138. 0x3047, // HIRAGANA LETTER SMALL E
  139. 0x3049, // HIRAGANA LETTER SMALL O
  140. 0x3063, // HIRAGANA LETTER SMALL TU
  141. 0x3083, // HIRAGANA LETTER SMALL YA
  142. 0x3085, // HIRAGANA LETTER SMALL YU
  143. 0x3087, // HIRAGANA LETTER SMALL YO
  144. 0x308E, // HIRAGANA LETTER SMALL WA
  145. 0x309B, // KATAKANA-HIRAGANA VOICED SOUND MARK
  146. 0x309C, // KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
  147. 0x309D, // HIRAGANA ITERATION MARK
  148. 0x309E, // HIRAGANA VOICED ITERATION MARK
  149. 0x30A1, // KATAKANA LETTER SMALL A
  150. 0x30A3, // KATAKANA LETTER SMALL I
  151. 0x30A5, // KATAKANA LETTER SMALL U
  152. 0x30A7, // KATAKANA LETTER SMALL E
  153. 0x30A9, // KATAKANA LETTER SMALL O
  154. 0x30C3, // KATAKANA LETTER SMALL TU
  155. 0x30E3, // KATAKANA LETTER SMALL YA
  156. 0x30E5, // KATAKANA LETTER SMALL YU
  157. 0x30E7, // KATAKANA LETTER SMALL YO
  158. 0x30EE, // KATAKANA LETTER SMALL WA
  159. 0x30F5, // KATAKANA LETTER SMALL KA
  160. 0x30F6, // KATAKANA LETTER SMALL KE
  161. 0x30FC, // KATAKANA-HIRAGANA PROLONGED SOUND MARK
  162. 0x30FD, // KATAKANA ITERATION MARK
  163. 0x30FE, // KATAKANA VOICED ITERATION MARK
  164. 0xFD3E, // ORNATE LEFT PARENTHESIS
  165. 0xFD3F, // ORNATE RIGHT PARENTHESIS
  166. 0xFE30, // VERTICAL TWO DOT LEADER
  167. 0xFE31, // VERTICAL EM DASH
  168. 0xFE33, // VERTICAL LOW LINE
  169. 0xFE34, // VERTICAL WAVY LOW LINE
  170. 0xFE35, // PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS
  171. 0xFE36, // PRESENTATION FORM FOR VERTICAL RIGHT PARENTHESIS
  172. 0xFE37, // PRESENTATION FORM FOR VERTICAL LEFT CURLY BRACKET
  173. 0xFE38, // PRESENTATION FORM FOR VERTICAL RIGHT CURLY BRACKET
  174. 0xFE39, // PRESENTATION FORM FOR VERTICAL LEFT TORTOISE SHELL BRACKET
  175. 0xFE3A, // PRESENTATION FORM FOR VERTICAL RIGHT TORTOISE SHELL BRACKET
  176. 0xFE3B, // PRESENTATION FORM FOR VERTICAL LEFT BLACK LENTICULAR BRACKET
  177. 0xFE3C, // PRESENTATION FORM FOR VERTICAL RIGHT BLACK LENTICULAR BRACKET
  178. 0xFE3D, // PRESENTATION FORM FOR VERTICAL LEFT DOUBLE ANGLE BRACKET
  179. 0xFE3E, // PRESENTATION FORM FOR VERTICAL RIGHT DOUBLE ANGLE BRACKET
  180. 0xFE3F, // PRESENTATION FORM FOR VERTICAL LEFT ANGLE BRACKET
  181. 0xFE40, // PRESENTATION FORM FOR VERTICAL RIGHT ANGLE BRACKET
  182. 0xFE41, // PRESENTATION FORM FOR VERTICAL LEFT CORNER BRACKET
  183. 0xFE42, // PRESENTATION FORM FOR VERTICAL RIGHT CORNER BRACKET
  184. 0xFE43, // PRESENTATION FORM FOR VERTICAL LEFT WHITE CORNER BRACKET
  185. 0xFE44, // PRESENTATION FORM FOR VERTICAL RIGHT WHITE CORNER BRACKET
  186. 0xFE4F, // WAVY LOW LINE
  187. 0xFE50, // SMALL COMMA
  188. 0xFE51, // SMALL IDEOGRAPHIC COMMA
  189. 0xFE59, // SMALL LEFT PARENTHESIS
  190. 0xFE5A, // SMALL RIGHT PARENTHESIS
  191. 0xFE5B, // SMALL LEFT CURLY BRACKET
  192. 0xFE5C, // SMALL RIGHT CURLY BRACKET
  193. 0xFE5D, // SMALL LEFT TORTOISE SHELL BRACKET
  194. 0xFE5E, // SMALL RIGHT TORTOISE SHELL BRACKET
  195. 0xFF08, // FULLWIDTH LEFT PARENTHESIS
  196. 0xFF09, // FULLWIDTH RIGHT PARENTHESIS
  197. 0xFF0C, // FULLWIDTH COMMA
  198. 0xFF0E, // FULLWIDTH FULL STOP
  199. 0xFF1C, // FULLWIDTH LESS-THAN SIGN
  200. 0xFF1E, // FULLWIDTH GREATER-THAN SIGN
  201. 0xFF3B, // FULLWIDTH LEFT SQUARE BRACKET
  202. 0xFF3D, // FULLWIDTH RIGHT SQUARE BRACKET
  203. 0xFF40, // FULLWIDTH GRAVE ACCENT
  204. 0xFF5B, // FULLWIDTH LEFT CURLY BRACKET
  205. 0xFF5C, // FULLWIDTH VERTICAL LINE
  206. 0xFF5D, // FULLWIDTH RIGHT CURLY BRACKET
  207. 0xFF5E, // FULLWIDTH TILDE
  208. 0xFF61, // HALFWIDTH IDEOGRAPHIC FULL STOP
  209. 0xFF62, // HALFWIDTH LEFT CORNER BRACKET
  210. 0xFF63, // HALFWIDTH RIGHT CORNER BRACKET
  211. 0xFF64, // HALFWIDTH IDEOGRAPHIC COMMA
  212. 0xFF67, // HALFWIDTH KATAKANA LETTER SMALL A
  213. 0xFF68, // HALFWIDTH KATAKANA LETTER SMALL I
  214. 0xFF69, // HALFWIDTH KATAKANA LETTER SMALL U
  215. 0xFF6A, // HALFWIDTH KATAKANA LETTER SMALL E
  216. 0xFF6B, // HALFWIDTH KATAKANA LETTER SMALL O
  217. 0xFF6C, // HALFWIDTH KATAKANA LETTER SMALL YA
  218. 0xFF6D, // HALFWIDTH KATAKANA LETTER SMALL YU
  219. 0xFF6E, // HALFWIDTH KATAKANA LETTER SMALL YO
  220. 0xFF6F, // HALFWIDTH KATAKANA LETTER SMALL TU
  221. 0xFF70, // HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK
  222. 0xFF9E, // HALFWIDTH KATAKANA VOICED SOUND MARK
  223. 0xFF9F, // HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
  224. 0xFFE9, // HALFWIDTH LEFTWARDS ARROW
  225. 0xFFEB, // HALFWIDTH RIGHTWARDS ARROW
  226. };
  227. /*
  228. //
  229. // AutoComplete Common Functions / Structures
  230. //
  231. const struct {
  232. UINT idMenu;
  233. UINT idCmd;
  234. } MenuToMessageId[] = {
  235. { IDM_AC_UNDO, WM_UNDO },
  236. { IDM_AC_CUT, WM_CUT },
  237. { IDM_AC_COPY, WM_COPY },
  238. { IDM_AC_PASTE, WM_PASTE }
  239. };
  240. */
  241. //+-------------------------------------------------------------------------
  242. // IUnknown methods
  243. //--------------------------------------------------------------------------
  244. HRESULT CAutoComplete::QueryInterface(REFIID riid, void **ppvObj)
  245. {
  246. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IAutoComplete) ||
  247. IsEqualIID(riid, IID_IAutoComplete2))
  248. {
  249. *ppvObj = SAFECAST(this, IAutoComplete2*);
  250. }
  251. else if (IsEqualIID(riid, IID_IAutoCompleteDropDown))
  252. {
  253. *ppvObj = SAFECAST(this, IAutoCompleteDropDown*);
  254. }
  255. else if (IsEqualIID(riid, IID_IEnumString))
  256. {
  257. *ppvObj = SAFECAST(this, IEnumString*);
  258. }
  259. else
  260. {
  261. return _DefQueryInterface(riid, ppvObj);
  262. }
  263. AddRef();
  264. return S_OK;
  265. }
  266. ULONG CAutoComplete::AddRef(void)
  267. {
  268. return InterlockedIncrement(&m_cRef);
  269. }
  270. ULONG CAutoComplete::Release(void)
  271. {
  272. ASSERT( 0 != m_cRef );
  273. ULONG cRef = InterlockedDecrement(&m_cRef);
  274. if ( 0 == cRef )
  275. {
  276. delete this;
  277. }
  278. TraceMsg(AC_GENERAL, "CAutoComplete::Release() --- cRef = %i", cRef);
  279. return cRef;
  280. }
  281. /* IAutoComplete methods */
  282. //+-------------------------------------------------------------------------
  283. // This object can be Inited in two ways. This function will init it in
  284. // the first way, which works as follows:
  285. //
  286. // 1. The caller called CoInitialize or OleInitialize() and the corresponding
  287. // uninit will not be called until the control we are subclassing and
  288. // our selfs are long gone.
  289. // 2. The caller calls us on their main thread and we create and destroy
  290. // the background thread as needed.
  291. //--------------------------------------------------------------------------
  292. HRESULT CAutoComplete::Init
  293. (
  294. HWND hwndEdit, // control to be subclassed
  295. IUnknown *punkACL, // autocomplete list
  296. LPCOLESTR pwszRegKeyPath, // reg location where ctrl-enter completion is stored stored
  297. LPCOLESTR pwszQuickComplete // default format string for ctrl-enter completion
  298. )
  299. {
  300. HRESULT hr = S_OK;
  301. TraceMsg(AC_GENERAL, "CAutoComplete::Init(hwndEdit=0x%x, punkACL = 0x%x, pwszRegKeyPath = 0x%x, pwszQuickComplete = 0x%x)",
  302. hwndEdit, punkACL, pwszRegKeyPath, pwszQuickComplete);
  303. #ifdef DEBUG
  304. // Ensure that the Line Break Character Table is ordered
  305. WCHAR c = g_szBreakChars[0];
  306. for (int i = 1; i < ARRAYSIZE(g_szBreakChars); ++i)
  307. {
  308. ASSERT(c < g_szBreakChars[i]);
  309. c = g_szBreakChars[i];
  310. }
  311. #endif
  312. if (m_hwndEdit != NULL)
  313. {
  314. // Can currently only be initialized once
  315. ASSERT(FALSE);
  316. return E_FAIL;
  317. }
  318. m_hwndEdit = hwndEdit;
  319. // Add our custom word-break callback so that we recognize URL delimitors when
  320. // ctrl-arrowing around.
  321. //
  322. // There is a bug with how USER handles WH_CALLWNDPROC global hooks in Win95 that
  323. // causes us to blow up if one is installed and a wordbreakproc is set. Thus,
  324. // if an app is running that has one of these hooks installed (intellipoint 1.1 etc.) then
  325. // if we install our wordbreakproc the app will fault when the proc is called. There
  326. // does not appear to be any way for us to work around it since USER's thunking code
  327. // trashes the stack so this API is disabled for Win95.
  328. //
  329. m_fEditControlUnicode = g_fRunningOnNT && IsWindowUnicode(m_hwndEdit);
  330. if (m_fEditControlUnicode)
  331. {
  332. m_oldEditWordBreakProc = (EDITWORDBREAKPROC)SendMessage(m_hwndEdit, EM_GETWORDBREAKPROC, 0, 0);
  333. SendMessage(m_hwndEdit, EM_SETWORDBREAKPROC, 0, (DWORD_PTR)EditWordBreakProcW);
  334. }
  335. //
  336. // bug 81414 : To avoid clashing with app messages used by the edit window, we
  337. // use registered messages.
  338. //
  339. m_uMsgSearchComplete = RegisterWindowMessageA("AC_SearchComplete");
  340. m_uMsgItemActivate = RegisterWindowMessageA("AC_ItemActivate");
  341. if (m_uMsgSearchComplete == 0)
  342. {
  343. m_uMsgSearchComplete = WM_APP + 300;
  344. }
  345. if (m_uMsgItemActivate == 0)
  346. {
  347. m_uMsgItemActivate = WM_APP + 301;
  348. }
  349. _SetQuickCompleteStrings(pwszRegKeyPath, pwszQuickComplete);
  350. // IEnumString required
  351. ASSERT(m_pes == NULL);
  352. EVAL(SUCCEEDED(punkACL->QueryInterface(IID_IEnumString, (void **)&m_pes)));
  353. // IACList optional
  354. ASSERT(m_pacl == NULL);
  355. punkACL->QueryInterface(IID_IACList, (void **)&m_pacl);
  356. AddRef(); // Hold on to a ref for our Subclass.
  357. // Initial creation should have failed if the thread object was not allocated!
  358. ASSERT(m_pThread);
  359. m_pThread->Init(m_pes, m_pacl);
  360. // subclass the edit window
  361. SetWindowSubclass(m_hwndEdit, &s_EditWndProc, 0, (DWORD_PTR)this);
  362. //#define TEST_SETFONT
  363. #ifdef TEST_SETFONT
  364. HFONT h = CreateFont(20, 5, 0, 0, FW_BOLD, TRUE, FALSE, FALSE, ANSI_CHARSET,
  365. OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  366. FF_ROMAN, NULL);
  367. SendMessage(m_hwndEdit, WM_SETFONT, (WPARAM)h, TRUE);
  368. #endif
  369. // See what autocomplete features are enabled
  370. _SeeWhatsEnabled();
  371. // See if hwndEdit is part of a combobox
  372. HWND hwndParent = GetParent(m_hwndEdit);
  373. WCHAR szClass[30];
  374. int nLen = GetClassName(hwndParent, szClass, ARRAYSIZE(szClass));
  375. if (nLen != 0 &&
  376. (StrCmpI(szClass, L"combobox") == 0 || StrCmpI(szClass, L"comboboxex") == 0))
  377. {
  378. m_hwndCombo = hwndParent;
  379. }
  380. // If we've already got focus, then we need to call GotFocus...
  381. if (GetFocus() == hwndEdit)
  382. {
  383. m_pThread->GotFocus();
  384. }
  385. return hr;
  386. }
  387. //+-------------------------------------------------------------------------
  388. // Checks to see if autoappend or autosuggest freatures are enabled
  389. //--------------------------------------------------------------------------
  390. void CAutoComplete::_SeeWhatsEnabled()
  391. {
  392. #ifdef ALLOW_ALWAYS_DROP_UP
  393. m_fAlwaysDropUp = SHRegGetBoolUSValue(REGSTR_PATH_AUTOCOMPLETE,
  394. TEXT("AlwaysDropUp"), FALSE, /*default:*/FALSE);
  395. #endif
  396. // If autosuggest was just enabled, create the dropdown window
  397. if (_IsAutoSuggestEnabled() && NULL == m_hwndDropDown)
  398. {
  399. // Create the dropdown Window
  400. WNDCLASS wc = {0};
  401. wc.lpfnWndProc = s_DropDownWndProc;
  402. wc.cbWndExtra = SIZEOF(CAutoComplete*);
  403. wc.hInstance = HINST_THISDLL;
  404. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  405. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  406. wc.lpszClassName = c_szAutoSuggestClass;
  407. SHRegisterClass(&wc);
  408. DWORD dwExStyle = WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOPARENTNOTIFY;
  409. if(_IsRTLReadingEnabled())
  410. {
  411. dwExStyle |= WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR;
  412. }
  413. #ifdef AC_TRANSLUCENCY
  414. if (g_bRunOnNT5 && g_fIE)
  415. {
  416. dwExStyle |= WS_EX_LAYERED;
  417. }
  418. #endif
  419. // The dropdown holds a ref on this object
  420. AddRef();
  421. m_hwndDropDown = CreateWindowEx(dwExStyle,
  422. c_szAutoSuggestClass,
  423. c_szAutoSuggestTitle, // GPF dialog is picking up this name!
  424. WS_POPUP | WS_BORDER | WS_CLIPCHILDREN,
  425. 0, 0, 100, 100,
  426. NULL, NULL, HINST_THISDLL, this);
  427. if (m_hwndDropDown)
  428. {
  429. #ifdef AC_TRANSLUCENCY
  430. if (g_fIE)
  431. {
  432. SetLayeredWindowAttributes(m_hwndDropDown,
  433. 0,
  434. 230,
  435. LWA_ALPHA);
  436. }
  437. #endif
  438. m_fDropDownResized = FALSE;
  439. }
  440. else
  441. {
  442. Release();
  443. }
  444. }
  445. else if (!_IsAutoSuggestEnabled() && NULL != m_hwndDropDown)
  446. {
  447. // We don't need the dropdown Window.
  448. if (m_hwndList)
  449. {
  450. DestroyWindow(m_hwndList);
  451. }
  452. DestroyWindow(m_hwndDropDown);
  453. }
  454. }
  455. //+-------------------------------------------------------------------------
  456. // Returns TRUE if autocomplete is currently enabled
  457. //--------------------------------------------------------------------------
  458. BOOL CAutoComplete::IsEnabled()
  459. {
  460. BOOL fRet;
  461. //
  462. // If we have not used the new IAutoComplete2 interface, we revert
  463. // to the old IE4 global registry setting
  464. //
  465. if (m_dwOptions & ACO_UNINITIALIZED)
  466. {
  467. fRet = SHRegGetBoolUSValue(REGSTR_PATH_AUTOCOMPLETE,
  468. REGSTR_VAL_USEAUTOCOMPLETE, FALSE, TRUE);
  469. }
  470. else
  471. {
  472. fRet = (m_dwOptions & (ACO_AUTOAPPEND | ACO_AUTOSUGGEST));
  473. }
  474. return fRet;
  475. }
  476. //+-------------------------------------------------------------------------
  477. // Enables/disables the up down arrow for autocomplete. Used by comboboxes
  478. // to disable arrow keys when the combo box is dropped. (This function is
  479. // now redundent because we check to see of the combo is dropped.)
  480. //--------------------------------------------------------------------------
  481. HRESULT CAutoComplete::Enable(BOOL fEnable)
  482. {
  483. TraceMsg(AC_GENERAL, "CAutoComplete::Enable(0x%x)", fEnable);
  484. HRESULT hr = (m_dwFlags & ACF_IGNOREUPDOWN) ? S_FALSE : S_OK;
  485. if (fEnable)
  486. m_dwFlags &= ~ACF_IGNOREUPDOWN;
  487. else
  488. m_dwFlags |= ACF_IGNOREUPDOWN;
  489. return hr;
  490. }
  491. /* IAutocomplete2 methods */
  492. //+-------------------------------------------------------------------------
  493. // Enables/disables various autocomplete features (see ACO_* flags)
  494. //--------------------------------------------------------------------------
  495. HRESULT CAutoComplete::SetOptions(DWORD dwOptions)
  496. {
  497. m_dwOptions = dwOptions;
  498. _SeeWhatsEnabled();
  499. return S_OK;
  500. }
  501. //+-------------------------------------------------------------------------
  502. // Returns the current option settings
  503. //--------------------------------------------------------------------------
  504. HRESULT CAutoComplete::GetOptions(DWORD* pdwOptions)
  505. {
  506. HRESULT hr = E_INVALIDARG;
  507. if (pdwOptions)
  508. {
  509. *pdwOptions = m_dwOptions;
  510. hr = S_OK;
  511. }
  512. return hr;
  513. }
  514. /* IAutocompleteDropDown methods */
  515. //+-------------------------------------------------------------------------
  516. // Returns the current dropdown status
  517. //--------------------------------------------------------------------------
  518. HRESULT CAutoComplete::GetDropDownStatus(DWORD *pdwFlags, LPWSTR *ppwszString)
  519. {
  520. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  521. {
  522. if (pdwFlags)
  523. {
  524. *pdwFlags = ACDD_VISIBLE;
  525. }
  526. if (ppwszString)
  527. {
  528. *ppwszString=NULL;
  529. if (m_hwndList)
  530. {
  531. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  532. if (iCurSel != -1)
  533. {
  534. WCHAR szBuf[MAX_URL_STRING];
  535. _GetItem(iCurSel, szBuf, ARRAYSIZE(szBuf), FALSE);
  536. *ppwszString = (LPWSTR) CoTaskMemAlloc((lstrlenW(szBuf)+1)*sizeof(WCHAR));
  537. if (*ppwszString)
  538. {
  539. StringCchCopy(*ppwszString, lstrlenW(szBuf) + 1, szBuf);
  540. }
  541. }
  542. }
  543. }
  544. }
  545. else
  546. {
  547. if (pdwFlags)
  548. {
  549. *pdwFlags = 0;
  550. }
  551. if (ppwszString)
  552. {
  553. *ppwszString = NULL;
  554. }
  555. }
  556. return S_OK;
  557. }
  558. HRESULT CAutoComplete::ResetEnumerator()
  559. {
  560. _StopSearch();
  561. _ResetSearch();
  562. _FreeDPAPtrs(m_hdpa);
  563. m_hdpa = NULL;
  564. // If the dropdown is currently visible, re-search the IEnumString
  565. // and show the dropdown. Otherwise wait for user input.
  566. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  567. {
  568. _StartCompletion(FALSE, TRUE);
  569. }
  570. return S_OK;
  571. }
  572. /* IEnumString methods */
  573. //+-------------------------------------------------------------------------
  574. // Resets the IEnumString functionality exposed for external users.
  575. //--------------------------------------------------------------------------
  576. HRESULT CAutoComplete::Reset()
  577. {
  578. HRESULT hr = E_FAIL;
  579. if (!m_szEnumString) // If we needed it once, we will most likely continue to need it.
  580. m_szEnumString = (LPTSTR) LocalAlloc(LPTR, MAX_URL_STRING * SIZEOF(TCHAR));
  581. if (!m_szEnumString)
  582. return E_OUTOFMEMORY;
  583. GetWindowText(m_hwndEdit, m_szEnumString, MAX_URL_STRING);
  584. if (m_pesExtern)
  585. hr = m_pesExtern->Reset();
  586. else
  587. {
  588. hr = m_pes->Clone(&m_pesExtern);
  589. if (SUCCEEDED(hr))
  590. hr = m_pesExtern->Reset();
  591. }
  592. return hr;
  593. }
  594. //+-------------------------------------------------------------------------
  595. // Returns the next BSTR from the autocomplete enumeration.
  596. //
  597. // For consistant results, the caller should not allow the AutoComplete text
  598. // to change between one call to Next() and another call to Next().
  599. // AutoComplete text should change only before Reset() is called.
  600. //--------------------------------------------------------------------------
  601. HRESULT CAutoComplete::Next
  602. (
  603. ULONG celt, // number items to fetch, needs to be 1
  604. LPOLESTR *rgelt, // returned BSTR, caller must free
  605. ULONG *pceltFetched // number of items returned
  606. )
  607. {
  608. HRESULT hr = S_FALSE;
  609. LPOLESTR pwszUrl;
  610. ULONG cFetched;
  611. // Pre-init in case of error
  612. if (rgelt)
  613. *rgelt = NULL;
  614. if (pceltFetched)
  615. *pceltFetched = 0;
  616. if (!EVAL(rgelt) || (!EVAL(pceltFetched)) || (!EVAL(1 == celt)) || !EVAL(m_pesExtern))
  617. return E_INVALIDARG;
  618. while (S_OK == (hr = m_pesExtern->Next(1, &pwszUrl, &cFetched)))
  619. {
  620. if (!StrCmpNI(m_szEnumString, pwszUrl, lstrlen(m_szEnumString)))
  621. {
  622. TraceMsg(TF_BAND|TF_GENERAL, "CAutoComplete: Next(). AutoSearch Failed URL=%s.", pwszUrl);
  623. break;
  624. }
  625. else
  626. {
  627. // If the string can't be added to our list, we will free it.
  628. TraceMsg(TF_BAND|TF_GENERAL, "CAutoComplete: Next(). AutoSearch Match URL=%s.", pwszUrl);
  629. CoTaskMemFree(pwszUrl);
  630. }
  631. }
  632. if (S_OK == hr)
  633. {
  634. *rgelt = (LPOLESTR)pwszUrl;
  635. *pceltFetched = 1; // We will always only fetch one.
  636. }
  637. return hr;
  638. }
  639. CAutoComplete::CAutoComplete() : m_cRef(1)
  640. {
  641. DllAddRef();
  642. TraceMsg(AC_GENERAL, "CAutoComplete::CAutoComplete()");
  643. // This class requires that this COM object be allocated in Zero INITed
  644. // memory. If the asserts below go off, then this was violated.
  645. ASSERT(!m_dwFlags);
  646. ASSERT(!m_hwndEdit);
  647. ASSERT(!m_pszCurrent);
  648. ASSERT(!m_iCurrent);
  649. ASSERT(!m_dwLastSearchFlags);
  650. ASSERT(!m_pes);
  651. ASSERT(!m_pacl);
  652. ASSERT(!m_pesExtern);
  653. ASSERT(!m_szEnumString);
  654. ASSERT(!m_pThread);
  655. m_dwOptions = ACO_UNINITIALIZED;
  656. m_hfontListView = NULL;
  657. }
  658. CAutoComplete::~CAutoComplete()
  659. {
  660. TraceMsg(AC_GENERAL, "CAutoComplete::~CAutoComplete()");
  661. ASSERT(m_hwndDropDown == NULL)
  662. SAFERELEASE(m_pes);
  663. SAFERELEASE(m_pacl);
  664. SAFERELEASE(m_pesExtern);
  665. SetStr(&m_pszCurrent, NULL);
  666. if (m_szEnumString)
  667. LocalFree(m_szEnumString);
  668. if (m_hdpaSortIndex)
  669. {
  670. // Note that this list pointed to items in m_hdpa, so we don't need
  671. // to free the items pointed to by this list.
  672. DPA_Destroy(m_hdpaSortIndex);
  673. m_hdpaSortIndex = NULL;
  674. }
  675. _FreeDPAPtrs(m_hdpa);
  676. if (m_pThread)
  677. {
  678. m_pThread->SyncShutDownBGThread();
  679. SAFERELEASE(m_pThread);
  680. }
  681. DllRelease();
  682. }
  683. STDMETHODIMP CAutoComplete::get_accName(VARIANT varChild, BSTR *pszName)
  684. {
  685. HRESULT hr;
  686. if (varChild.vt == VT_I4)
  687. {
  688. if (varChild.lVal > 0)
  689. {
  690. WCHAR szBuf[MAX_URL_STRING];
  691. _GetItem(varChild.lVal - 1, szBuf, ARRAYSIZE(szBuf), TRUE);
  692. *pszName = SysAllocString(szBuf);
  693. }
  694. else
  695. {
  696. *pszName = NULL;
  697. }
  698. hr = S_OK;
  699. }
  700. else
  701. {
  702. hr = E_UNEXPECTED;
  703. }
  704. return hr;
  705. }
  706. //+-------------------------------------------------------------------------
  707. // Private initialization
  708. //--------------------------------------------------------------------------
  709. BOOL CAutoComplete::_Init()
  710. {
  711. m_pThread = new CACThread(*this);
  712. return (NULL != m_pThread);
  713. }
  714. //+-------------------------------------------------------------------------
  715. // Creates and instance of CAutoComplete
  716. //--------------------------------------------------------------------------
  717. HRESULT CAutoComplete_CreateInstance(IUnknown* pUnkOuter, IUnknown** ppunk, LPCOBJECTINFO poi)
  718. {
  719. // Note - Aggregation checking is handled in class factory
  720. *ppunk = NULL;
  721. CAutoComplete* p = new CAutoComplete();
  722. if (p)
  723. {
  724. if (p->_Init())
  725. {
  726. *ppunk = SAFECAST(p, IAutoComplete *);
  727. return S_OK;
  728. }
  729. delete p;
  730. }
  731. return E_OUTOFMEMORY;
  732. }
  733. //+-------------------------------------------------------------------------
  734. // Helper function to add default autocomplete functionality to and edit
  735. // window.
  736. //--------------------------------------------------------------------------
  737. HRESULT SHUseDefaultAutoComplete
  738. (
  739. HWND hwndEdit,
  740. IBrowserService * pbs, IN OPTIONAL
  741. IAutoComplete2 ** ppac, OUT OPTIONAL
  742. IShellService ** ppssACLISF, OUT OPTIONAL
  743. BOOL fUseCMDMRU
  744. )
  745. {
  746. HRESULT hr;
  747. IUnknown * punkACLMulti;
  748. if (ppac)
  749. *ppac = NULL;
  750. if (ppssACLISF)
  751. *ppssACLISF = NULL;
  752. hr = CoCreateInstance(CLSID_ACLMulti, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)&punkACLMulti);
  753. if (SUCCEEDED(hr))
  754. {
  755. IObjMgr * pomMulti;
  756. hr = punkACLMulti->QueryInterface(IID_IObjMgr, (LPVOID *)&pomMulti);
  757. if (SUCCEEDED(hr))
  758. {
  759. BOOL fReady = FALSE; // Fail only if all we are not able to create at least one list.
  760. // ADD The MRU List
  761. IUnknown * punkACLMRU;
  762. // MRU for run dialog no longer adds URL MRU automatically
  763. // so we have to add it ourselves
  764. if (fUseCMDMRU)
  765. {
  766. hr = CACLMRU_CreateInstance(NULL, &punkACLMRU, NULL, SZ_REGKEY_TYPEDCMDMRU);
  767. if (SUCCEEDED(hr))
  768. {
  769. pomMulti->Append(punkACLMRU);
  770. punkACLMRU->Release();
  771. fReady = TRUE;
  772. }
  773. }
  774. hr = CACLMRU_CreateInstance(NULL, &punkACLMRU, NULL, SZ_REGKEY_TYPEDURLMRU);
  775. if (SUCCEEDED(hr))
  776. {
  777. pomMulti->Append(punkACLMRU);
  778. punkACLMRU->Release();
  779. fReady = TRUE;
  780. }
  781. // ADD The History List
  782. IUnknown * punkACLHist;
  783. hr = CoCreateInstance(CLSID_ACLHistory, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)&punkACLHist);
  784. if (SUCCEEDED(hr))
  785. {
  786. pomMulti->Append(punkACLHist);
  787. punkACLHist->Release();
  788. fReady = TRUE;
  789. }
  790. // ADD The ISF List
  791. IUnknown * punkACLISF;
  792. hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)&punkACLISF);
  793. if (SUCCEEDED(hr))
  794. {
  795. // We need to give the ISF AutoComplete List a pointer to the IBrowserService
  796. // so it can retrieve the current browser location to AutoComplete correctly.
  797. IShellService * pss;
  798. hr = punkACLISF->QueryInterface(IID_IShellService, (LPVOID *)&pss);
  799. if (SUCCEEDED(hr))
  800. {
  801. if (pbs)
  802. pss->SetOwner(pbs);
  803. if (ppssACLISF)
  804. *ppssACLISF = pss;
  805. else
  806. pss->Release();
  807. }
  808. //
  809. // Set options
  810. //
  811. IACList2* pacl;
  812. if (SUCCEEDED(punkACLISF->QueryInterface(IID_IACList2, (LPVOID *)&pacl)))
  813. {
  814. // Specify directories to search
  815. pacl->SetOptions(ACLO_CURRENTDIR | ACLO_FAVORITES | ACLO_MYCOMPUTER | ACLO_DESKTOP);
  816. pacl->Release();
  817. }
  818. pomMulti->Append(punkACLISF);
  819. punkACLISF->Release();
  820. fReady = TRUE;
  821. }
  822. if (fReady)
  823. {
  824. IAutoComplete2 * pac;
  825. // Create the AutoComplete Object
  826. hr = CoCreateInstance(CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER, IID_IAutoComplete2, (void **)&pac);
  827. if (SUCCEEDED(hr))
  828. {
  829. // Load the quick complete string.
  830. WCHAR szQuickComplete[50]; // US string is 17 characters, 50 should be plenty without blowing the stack
  831. MLLoadString(IDS_QUICKCOMPLETE, szQuickComplete, ARRAYSIZE(szQuickComplete));
  832. hr = pac->Init(hwndEdit, punkACLMulti, WZ_REGKEY_QUICKCOMPLETE, szQuickComplete);
  833. if (ppac)
  834. *ppac = pac;
  835. else
  836. pac->Release();
  837. }
  838. }
  839. pomMulti->Release();
  840. }
  841. punkACLMulti->Release();
  842. }
  843. return hr;
  844. }
  845. /* Private functions */
  846. //+-------------------------------------------------------------------------
  847. // Removes anything that we appended to the edit text
  848. //--------------------------------------------------------------------------
  849. void CAutoComplete::_RemoveCompletion()
  850. {
  851. TraceMsg(AC_GENERAL, "CAutoComplete::_RemoveCompletion()");
  852. if (m_fAppended)
  853. {
  854. // Remove any highlighted text that we displayed
  855. Edit_ReplaceSel(m_hwndEdit, TEXT(""));
  856. m_fAppended = FALSE;
  857. }
  858. }
  859. //+-------------------------------------------------------------------------
  860. // Updates the text in the edit control
  861. //--------------------------------------------------------------------------
  862. void CAutoComplete::_SetEditText(LPCWSTR psz)
  863. {
  864. //
  865. // We set a flag so that we can distinguish between us setting the text
  866. // and someone else doing it. If someone else sets the text we hide our
  867. // dropdown.
  868. //
  869. m_fSettingText = TRUE;
  870. // Don't display our special wildcard search string
  871. if (psz[0] == CH_WILDCARD)
  872. {
  873. Edit_SetText(m_hwndEdit, L"");
  874. }
  875. else
  876. {
  877. Edit_SetText(m_hwndEdit, psz);
  878. }
  879. m_fSettingText = FALSE;
  880. }
  881. //+-------------------------------------------------------------------------
  882. // Removed anything that we appended to the edit text and then updates
  883. // m_pszCurrent with the current string.
  884. //--------------------------------------------------------------------------
  885. void CAutoComplete::_GetEditText(void)
  886. {
  887. TraceMsg(AC_GENERAL, "CAutoComplete::_GetEditText()");
  888. _RemoveCompletion(); // remove anything we added
  889. int iCurrent = GetWindowTextLength(m_hwndEdit);
  890. //
  891. // If the current buffer is too small, delete it.
  892. //
  893. if (m_pszCurrent &&
  894. LocalSize(m_pszCurrent) <= (UINT)(iCurrent + 1) * sizeof(TCHAR))
  895. {
  896. SetStr(&m_pszCurrent, NULL);
  897. }
  898. //
  899. // If there is no current buffer, try to allocate one
  900. // with some room to grow.
  901. //
  902. if (!m_pszCurrent)
  903. {
  904. m_pszCurrent = (LPTSTR)LocalAlloc(LPTR, (iCurrent + (MAX_URL_STRING / 2)) * SIZEOF(TCHAR));
  905. }
  906. //
  907. // If we have a current buffer, get the text.
  908. //
  909. if (m_pszCurrent)
  910. {
  911. if (!GetWindowText(m_hwndEdit, m_pszCurrent, iCurrent + 1))
  912. {
  913. *m_pszCurrent = L'\0';
  914. }
  915. // On win9x GetWindowTextLength can return more than the # of characters
  916. m_iCurrent = lstrlen(m_pszCurrent);
  917. }
  918. else
  919. {
  920. m_iCurrent = 0;
  921. }
  922. }
  923. //+-------------------------------------------------------------------------
  924. // Updates the text in the edit control
  925. //--------------------------------------------------------------------------
  926. void CAutoComplete::_UpdateText
  927. (
  928. int iStartSel, // start location for selected
  929. int iEndSel, // end location of selected text
  930. LPCTSTR pszCurrent, // unselected text
  931. LPCTSTR pszNew // autocompleted (selected) text
  932. )
  933. {
  934. TraceMsg(AC_GENERAL, "CAutoComplete::_UpdateText(iStart=%i; iEndSel = %i, pszCurrent=>%s<, pszNew=>%s<)",
  935. iStartSel, iEndSel, (pszCurrent ? pszCurrent : TEXT("(null)")), (pszNew ? pszNew : TEXT("(null)")));
  936. //
  937. // Restore the old text.
  938. //
  939. _SetEditText(pszCurrent);
  940. //
  941. // Put the cursor at the insertion point.
  942. //
  943. Edit_SetSel(m_hwndEdit, iStartSel, iStartSel);
  944. //
  945. // Insert the new text.
  946. //
  947. Edit_ReplaceSel(m_hwndEdit, pszNew);
  948. //
  949. // Select the newly added text.
  950. //
  951. Edit_SetSel(m_hwndEdit, iStartSel, iEndSel);
  952. }
  953. //+-------------------------------------------------------------------------
  954. // If pwszQuickComplete is NULL, we will use our internal default.
  955. // pwszRegKeyValue can be NULL indicating that there is not a key.
  956. //--------------------------------------------------------------------------
  957. BOOL CAutoComplete::_SetQuickCompleteStrings(LPCOLESTR pwszRegKeyPath, LPCOLESTR pwszQuickComplete)
  958. {
  959. TraceMsg(AC_GENERAL, "CAutoComplete::_SetQuickCompleteStrings(pwszRegKeyPath=0x%x, pwszQuickComplete = 0x%x)",
  960. pwszRegKeyPath, pwszQuickComplete);
  961. if (pwszRegKeyPath)
  962. {
  963. StringCchCopy(m_szRegKeyPath, ARRAYSIZE(m_szRegKeyPath), pwszRegKeyPath);
  964. }
  965. else
  966. {
  967. // can be empty
  968. m_szRegKeyPath[0] = TEXT('\0');
  969. }
  970. if (pwszQuickComplete)
  971. {
  972. StringCchCopy(m_szQuickComplete, ARRAYSIZE(m_szQuickComplete), pwszQuickComplete);
  973. }
  974. else
  975. {
  976. // use default value
  977. StringCchCopy(m_szQuickComplete, ARRAYSIZE(m_szQuickComplete), c_szAutoDefQuickComp);
  978. }
  979. return TRUE;
  980. }
  981. //+-------------------------------------------------------------------------
  982. // Formats the current contents of the edit box with the appropriate prefix
  983. // and endfix and returns the completed string.
  984. //--------------------------------------------------------------------------
  985. LPTSTR CAutoComplete::_QuickEnter()
  986. {
  987. //
  988. // If they shift-enter, then do the favorite pre/post-fix.
  989. //
  990. TCHAR szFormat[MAX_QUICK_COMPLETE_STRING];
  991. TCHAR szNewText[MAX_URL_STRING];
  992. int iLen;
  993. TraceMsg(AC_GENERAL, "CAutoComplete::_QuickEnter()");
  994. if (NULL == m_pszCurrent)
  995. {
  996. return NULL;
  997. }
  998. StringCchCopy(szFormat, ARRAYSIZE(szFormat), m_szQuickComplete);
  999. DWORD cb = sizeof(szFormat);
  1000. SHGetValue(HKEY_CURRENT_USER, m_szRegKeyPath, TEXT("QuickComplete"), NULL, &szFormat, &cb);
  1001. //
  1002. // Remove preceeding and trailing white space
  1003. //
  1004. PathRemoveBlanks(m_pszCurrent);
  1005. //
  1006. // Make sure we don't GPF.
  1007. //
  1008. iLen = lstrlen(m_pszCurrent) + lstrlen(szFormat);
  1009. if (iLen < ARRAYSIZE(szNewText))
  1010. {
  1011. // If the quick complete is already present, don't add it again
  1012. LPWSTR pszInsertion = StrStrI(szFormat, L"%s");
  1013. LPWSTR pszFormat = szFormat;
  1014. if (pszInsertion)
  1015. {
  1016. // If prefix is already present, don't add it again.
  1017. // (we could improve this to only add parts of the predfix that are missing)
  1018. int iInsertion = (int)(pszInsertion - pszFormat);
  1019. if (iInsertion == 0 || StrCmpNI(pszFormat, m_pszCurrent, iInsertion) == 0)
  1020. {
  1021. // Skip over prefix
  1022. pszFormat = pszInsertion;
  1023. }
  1024. // If postfix is already present, don't add it again.
  1025. LPWSTR pszPostFix = pszInsertion + ARRAYSIZE(L"%s") - 1;
  1026. int cchCurrent = lstrlen(m_pszCurrent);
  1027. int cchPostFix = lstrlen(pszPostFix);
  1028. if (cchPostFix > 0 && cchPostFix < cchCurrent &&
  1029. StrCmpI(m_pszCurrent + (cchCurrent - cchPostFix), pszPostFix) == 0)
  1030. {
  1031. // Lop off postfix
  1032. *pszPostFix = 0;
  1033. }
  1034. }
  1035. StringCchPrintf(szNewText, ARRAYSIZE(szNewText), pszFormat, m_pszCurrent);
  1036. SetStr(&m_pszCurrent, szNewText);
  1037. }
  1038. return m_pszCurrent;
  1039. }
  1040. BOOL CAutoComplete::_ResetSearch(void)
  1041. {
  1042. TraceMsg(AC_GENERAL, "CAutoComplete::_ResetSearch()");
  1043. m_dwFlags = ACF_RESET;
  1044. return TRUE;
  1045. }
  1046. //+-------------------------------------------------------------------------
  1047. // Returns TRUE if the char is a forward or backackwards slash
  1048. //--------------------------------------------------------------------------
  1049. BOOL CAutoComplete::_IsWhack(TCHAR ch)
  1050. {
  1051. return (ch == TEXT('/')) || (ch == TEXT('\\'));
  1052. }
  1053. //+-------------------------------------------------------------------------
  1054. // Returns TRUE if the string points to a character used to separate words
  1055. //--------------------------------------------------------------------------
  1056. BOOL CAutoComplete::_IsBreakChar(WCHAR wch)
  1057. {
  1058. // Do a binary search in our table of break characters
  1059. int iMin = 0;
  1060. int iMax = ARRAYSIZE(g_szBreakChars) - 1;
  1061. while (iMax - iMin >= 2)
  1062. {
  1063. int iTry = (iMax + iMin + 1) / 2;
  1064. if (wch < g_szBreakChars[iTry])
  1065. iMax = iTry;
  1066. else if (wch > g_szBreakChars[iTry])
  1067. iMin = iTry;
  1068. else
  1069. return TRUE;
  1070. }
  1071. return (wch == g_szBreakChars[iMin] || wch == g_szBreakChars[iMax]);
  1072. }
  1073. //+-------------------------------------------------------------------------
  1074. // Returns TRUE if we want to append to the current edit box contents
  1075. //--------------------------------------------------------------------------
  1076. BOOL CAutoComplete::_WantToAppendResults()
  1077. {
  1078. //
  1079. // Users get annoyed if we append real text after a
  1080. // slash, because they type "c:\" and we complete
  1081. // it to "c:\windows" when they aren't looking.
  1082. //
  1083. // Also, it's annoying to have "\" autocompleted to "\\"
  1084. //
  1085. return (m_pszCurrent &&
  1086. (!(_IsWhack(m_pszCurrent[0]) && m_pszCurrent[1] == NULL) &&
  1087. !_IsWhack(m_pszCurrent[lstrlen(m_pszCurrent)-1])));
  1088. }
  1089. //+-------------------------------------------------------------------------
  1090. // Callback routine used by the edit window to determine where to break
  1091. // words. We install this custom callback dor the ctl arrow keys
  1092. // recognize our break characters.
  1093. //--------------------------------------------------------------------------
  1094. int CALLBACK CAutoComplete::EditWordBreakProcW
  1095. (
  1096. LPWSTR pszEditText, // pointer to edit text
  1097. int ichCurrent, // index of starting point
  1098. int cch, // length in characters of edit text
  1099. int code // action to take
  1100. )
  1101. {
  1102. LPWSTR psz = pszEditText + ichCurrent;
  1103. int iIndex;
  1104. BOOL fFoundNonDelimiter = FALSE;
  1105. static BOOL fRight = FALSE; // hack due to bug in USER
  1106. switch (code)
  1107. {
  1108. case WB_ISDELIMITER:
  1109. fRight = TRUE;
  1110. // Simple case - is the current character a delimiter?
  1111. iIndex = (int)_IsBreakChar(*psz);
  1112. break;
  1113. case WB_LEFT:
  1114. // Move to the left to find the first delimiter. If we are
  1115. // currently at a delimiter, then skip delimiters until we
  1116. // find the first non-delimiter, then start from there.
  1117. //
  1118. // Special case for fRight - if we are currently at a delimiter
  1119. // then just return the current word!
  1120. while ((psz = CharPrev(pszEditText, psz)) != pszEditText)
  1121. {
  1122. if (_IsBreakChar(*psz))
  1123. {
  1124. if (fRight || fFoundNonDelimiter)
  1125. break;
  1126. }
  1127. else
  1128. {
  1129. fFoundNonDelimiter = TRUE;
  1130. fRight = FALSE;
  1131. }
  1132. }
  1133. iIndex = (int)(psz - pszEditText);
  1134. // We are currently pointing at the delimiter, next character
  1135. // is the beginning of the next word.
  1136. if (iIndex > 0 && iIndex < cch)
  1137. iIndex++;
  1138. break;
  1139. case WB_RIGHT:
  1140. fRight = FALSE;
  1141. // If we are not at a delimiter, then skip to the right until
  1142. // we find the first delimiter. If we started at a delimiter, or
  1143. // we have just finished scanning to the first delimiter, then
  1144. // skip all delimiters until we find the first non delimiter.
  1145. //
  1146. // Careful - the string passed in to us may not be NULL terminated!
  1147. fFoundNonDelimiter = !_IsBreakChar(*psz);
  1148. if (psz != (pszEditText + cch))
  1149. {
  1150. while ((psz = CharNext(psz)) != (pszEditText + cch))
  1151. {
  1152. if (_IsBreakChar(*psz))
  1153. {
  1154. fFoundNonDelimiter = FALSE;
  1155. }
  1156. else
  1157. {
  1158. if (!fFoundNonDelimiter)
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. // We are currently pointing at the next word.
  1164. iIndex = (int) (psz - pszEditText);
  1165. break;
  1166. default:
  1167. iIndex = 0;
  1168. break;
  1169. }
  1170. return iIndex;
  1171. }
  1172. //+-------------------------------------------------------------------------
  1173. // Returns the index of the next or previous break character in m_pszCurrent
  1174. //--------------------------------------------------------------------------
  1175. int CAutoComplete::_JumpToNextBreak
  1176. (
  1177. int iLoc, // current location
  1178. DWORD dwFlags // direction (WB_RIGHT or WB_LEFT)
  1179. )
  1180. {
  1181. return EditWordBreakProcW(m_pszCurrent, iLoc, lstrlen(m_pszCurrent), dwFlags);
  1182. }
  1183. //+-------------------------------------------------------------------------
  1184. // Handles Horizontal cursor movement. Returns TRUE if the message should
  1185. // passed on to the OS. Note that we only call this on win9x. On NT we
  1186. // use EM_SETWORDBREAKPROC to set a callback instead because it sets the
  1187. // caret correctly. This callback can crash on win9x.
  1188. //--------------------------------------------------------------------------
  1189. BOOL CAutoComplete::_CursorMovement
  1190. (
  1191. WPARAM wParam // virtual key data from WM_KEYDOWN
  1192. )
  1193. {
  1194. BOOL fShift, fControl;
  1195. DWORD dwKey = (DWORD)wParam;
  1196. int iStart, iEnd;
  1197. TraceMsg(AC_GENERAL, "CAutoComplete::_CursorMovement(wParam = 0x%x)",
  1198. wParam);
  1199. fShift = (0 > GetKeyState(VK_SHIFT)) ;
  1200. fControl = (0 > GetKeyState(VK_CONTROL));
  1201. // We don't do anything special unless the CTRL
  1202. // key is down so we don't want to mess up arrowing around
  1203. // UNICODE character clusters. (INDIC o+d+j+d+k+w)
  1204. if (!fControl)
  1205. return TRUE; // let OS handle because of UNICODE char clusters
  1206. // get the current selection
  1207. SendMessage(m_hwndEdit, EM_GETSEL, (WPARAM)&iStart, (LPARAM)&iEnd);
  1208. // the user is editting the text, so this is now invalid.
  1209. m_dwFlags = ACF_RESET;
  1210. _GetEditText();
  1211. if (!m_pszCurrent)
  1212. return TRUE; // we didn't handle it... let the default wndproc try
  1213. // Determine the previous selection direction
  1214. int dwSelectionDirection;
  1215. if (iStart == iEnd)
  1216. {
  1217. // Nothing previously selected, so use new direction
  1218. dwSelectionDirection = dwKey;
  1219. }
  1220. else
  1221. {
  1222. // Base the selection direction on whether the caret is positioned
  1223. // at the beginning or the end of the selection
  1224. POINT pt;
  1225. int cchCaret = iEnd;
  1226. if (GetCaretPos(&pt))
  1227. {
  1228. cchCaret = (int)SendMessage(m_hwndEdit, EM_CHARFROMPOS, 0, (LPARAM)MAKELPARAM(pt.x, 0));
  1229. }
  1230. dwSelectionDirection = (cchCaret >= iEnd) ? VK_RIGHT : VK_LEFT;
  1231. }
  1232. if (fControl)
  1233. {
  1234. if (dwKey == VK_RIGHT)
  1235. {
  1236. // did we orginally go to the left?
  1237. if (dwSelectionDirection == VK_LEFT)
  1238. {
  1239. // yes...unselect
  1240. iStart = _JumpToNextBreak(iStart, WB_RIGHT);
  1241. // if (!iStart)
  1242. // iStart = m_iCurrent;
  1243. }
  1244. else if (iEnd != m_iCurrent)
  1245. {
  1246. // select or "jump over" characters
  1247. iEnd = _JumpToNextBreak(iEnd, WB_RIGHT);
  1248. // if (!iEnd)
  1249. // iEnd = m_iCurrent;
  1250. }
  1251. }
  1252. else // dwKey == VK_LEFT
  1253. {
  1254. // did we orginally go to the right?
  1255. if (dwSelectionDirection == VK_RIGHT)
  1256. {
  1257. // yes...unselect
  1258. // int iRemember = iEnd;
  1259. iEnd = _JumpToNextBreak(iEnd, WB_LEFT);
  1260. }
  1261. else if (iStart) // != 0
  1262. {
  1263. // select or "jump over" characters
  1264. iStart = _JumpToNextBreak(iStart, WB_LEFT);
  1265. }
  1266. }
  1267. }
  1268. else // if !fControl
  1269. {
  1270. // This code is benign if the SHIFT key isn't down
  1271. // because it has to do with modifying the selection.
  1272. if (dwKey == VK_RIGHT)
  1273. {
  1274. if (dwSelectionDirection == VK_LEFT)
  1275. {
  1276. iStart++;
  1277. }
  1278. else
  1279. {
  1280. iEnd++;
  1281. }
  1282. }
  1283. else // dwKey == VK_LEFT
  1284. {
  1285. LPTSTR pszPrev;
  1286. if (dwSelectionDirection == VK_RIGHT)
  1287. {
  1288. pszPrev = CharPrev(m_pszCurrent, &m_pszCurrent[iEnd]);
  1289. iEnd = (int)(pszPrev - m_pszCurrent);
  1290. }
  1291. else
  1292. {
  1293. pszPrev = CharPrev(m_pszCurrent, &m_pszCurrent[iStart]);
  1294. iStart = (int)(pszPrev - m_pszCurrent);
  1295. }
  1296. }
  1297. }
  1298. // Are we selecting or moving?
  1299. if (!fShift)
  1300. { // just moving...
  1301. if (dwKey == VK_RIGHT)
  1302. {
  1303. iStart = iEnd;
  1304. }
  1305. else // pachi->dwSelectionDirection == VK_LEFT
  1306. {
  1307. iEnd = iStart;
  1308. }
  1309. }
  1310. //
  1311. // If we are selecting text to the left, we have to jump hoops
  1312. // to get the caret on the left of the selection. Edit_SetSel
  1313. // always places the caret on the right, and if we position the
  1314. // caret ourselves the edit control still uses the old caret
  1315. // position. So we have to send VK_LEFT messages to the edit
  1316. // control to get it to select things properly.
  1317. //
  1318. if (fShift && dwSelectionDirection == VK_LEFT && iStart < iEnd)
  1319. {
  1320. // Temporarily reset the control key (yuk!)
  1321. BYTE keyState[256];
  1322. BOOL fGetKeyboardState;
  1323. if (fControl)
  1324. {
  1325. fGetKeyboardState = GetKeyboardState(keyState);
  1326. if (fGetKeyboardState )
  1327. {
  1328. keyState[VK_CONTROL] &= 0x7f;
  1329. SetKeyboardState(keyState);
  1330. }
  1331. }
  1332. // Select the last character and select left
  1333. // one character at a time. Arrrggg.
  1334. SendMessage(m_hwndEdit, WM_SETREDRAW, FALSE, 0);
  1335. Edit_SetSel(m_hwndEdit, iEnd, iEnd);
  1336. while (iEnd > iStart)
  1337. {
  1338. DefSubclassProc(m_hwndEdit, WM_KEYDOWN, VK_LEFT, 0);
  1339. --iEnd;
  1340. }
  1341. SendMessage(m_hwndEdit, WM_SETREDRAW, TRUE, 0);
  1342. InvalidateRect(m_hwndEdit, NULL, FALSE);
  1343. UpdateWindow(m_hwndEdit);
  1344. // Restore the control key
  1345. if (fControl && fGetKeyboardState )
  1346. {
  1347. keyState[VK_CONTROL] |= 0x80;
  1348. SetKeyboardState(keyState);
  1349. }
  1350. }
  1351. else
  1352. {
  1353. Edit_SetSel(m_hwndEdit, iStart, iEnd);
  1354. }
  1355. return FALSE; // we handled it
  1356. }
  1357. //+-------------------------------------------------------------------------
  1358. // Process WM_KEYDOWN message. Returns TRUE if the message should be passed
  1359. // to the original wndproc.
  1360. //--------------------------------------------------------------------------
  1361. BOOL CAutoComplete::_OnKeyDown(WPARAM wParam)
  1362. {
  1363. WPARAM wParamTranslated;
  1364. TraceMsg(AC_GENERAL, "CAutoComplete::_OnKeyDown(wParam = 0x%x)",
  1365. wParam);
  1366. if (m_pThread->IsDisabled())
  1367. {
  1368. //
  1369. // Let the original wndproc handle it.
  1370. //
  1371. return TRUE;
  1372. }
  1373. wParamTranslated = wParam;
  1374. switch (wParamTranslated)
  1375. {
  1376. case VK_RETURN:
  1377. {
  1378. if (0 > GetKeyState(VK_CONTROL))
  1379. {
  1380. //
  1381. // Ctrl-Enter does some quick formatting.
  1382. //
  1383. _GetEditText();
  1384. _SetEditText(_QuickEnter());
  1385. }
  1386. else
  1387. {
  1388. //
  1389. // Reset the search criteria.
  1390. //
  1391. _ResetSearch();
  1392. //
  1393. // Highlight entire text.
  1394. //
  1395. Edit_SetSel(m_hwndEdit, 0, (LPARAM)-1);
  1396. }
  1397. //
  1398. // Stop any searches that are going on.
  1399. //
  1400. _StopSearch();
  1401. //
  1402. // For intelliforms, if the dropdown is visible and something
  1403. // is selected in the dropdown, we simulate an activation event.
  1404. //
  1405. if (m_hwndList)
  1406. {
  1407. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  1408. if ((iCurSel != -1) && m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  1409. {
  1410. WCHAR szBuf[MAX_URL_STRING];
  1411. _GetItem(iCurSel, szBuf, ARRAYSIZE(szBuf), FALSE);
  1412. SendMessage(m_hwndEdit, m_uMsgItemActivate, 0, (LPARAM)szBuf);
  1413. }
  1414. }
  1415. //
  1416. // Hide the dropdown
  1417. //
  1418. _HideDropDown();
  1419. // APPCOMPAT: For some reason, the original windproc is ignoring the return key.
  1420. // It should hide the dropdown!
  1421. if (m_hwndCombo)
  1422. {
  1423. SendMessage(m_hwndCombo, CB_SHOWDROPDOWN, FALSE, 0);
  1424. }
  1425. //
  1426. // Let the original wndproc handle it.
  1427. //
  1428. break;
  1429. }
  1430. case VK_ESCAPE:
  1431. _StopSearch();
  1432. _HideDropDown();
  1433. // APPCOMPAT: For some reason, the original windproc is ignoring the enter key.
  1434. // It should hide the dropdown!
  1435. if (m_hwndCombo)
  1436. {
  1437. SendMessage(m_hwndCombo, CB_SHOWDROPDOWN, FALSE, 0);
  1438. }
  1439. break;
  1440. case VK_LEFT:
  1441. case VK_RIGHT:
  1442. // We do our own cursor movement on win9x because EM_SETWORDBREAKPROC is broken.
  1443. if (!g_fRunningOnNT)
  1444. {
  1445. return _CursorMovement(wParam);
  1446. }
  1447. break;
  1448. case VK_PRIOR:
  1449. case VK_UP:
  1450. if (!(m_dwFlags & ACF_IGNOREUPDOWN) && !_IsComboboxDropped())
  1451. {
  1452. //
  1453. // If the dropdown is visible, the up-down keys navigate our list
  1454. //
  1455. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  1456. {
  1457. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  1458. if (iCurSel == 0)
  1459. {
  1460. // If at top, move back up into the edit box
  1461. // Deselect the dropdown and select the edit box
  1462. ListView_SetItemState(m_hwndList, 0, 0, 0x000f);
  1463. if (m_pszCurrent)
  1464. {
  1465. // Restore original text if they arrow out of listview
  1466. _SetEditText(m_pszCurrent);
  1467. }
  1468. Edit_SetSel(m_hwndEdit, MAX_URL_STRING, MAX_URL_STRING);
  1469. }
  1470. else if (iCurSel != -1)
  1471. {
  1472. // If in middle or at bottom, move up
  1473. SendMessage(m_hwndList, WM_KEYDOWN, wParam, 0);
  1474. SendMessage(m_hwndList, WM_KEYUP, wParam, 0);
  1475. }
  1476. else
  1477. {
  1478. int iSelect = ListView_GetItemCount(m_hwndList)-1;
  1479. // If in edit box, move to bottom
  1480. ListView_SetItemState(m_hwndList, iSelect, LVIS_SELECTED|LVIS_FOCUSED, 0x000f);
  1481. ListView_EnsureVisible(m_hwndList, iSelect, FALSE);
  1482. }
  1483. return FALSE;
  1484. }
  1485. //
  1486. // If Autosuggest drop-down enabled but not popped up then start a search
  1487. // based on the current edit box contents. If the edit box is empty,
  1488. // search for everything.
  1489. //
  1490. else if ((m_dwOptions & ACO_UPDOWNKEYDROPSLIST) && _IsAutoSuggestEnabled())
  1491. {
  1492. // Ensure the background thread knows we have focus
  1493. _GotFocus();
  1494. _StartCompletion(FALSE, TRUE);
  1495. return FALSE;
  1496. }
  1497. //
  1498. // Otherwise we see if we should append the completions in place
  1499. //
  1500. else if (_IsAutoAppendEnabled())
  1501. {
  1502. if (_AppendPrevious(FALSE))
  1503. {
  1504. return FALSE;
  1505. }
  1506. }
  1507. }
  1508. break;
  1509. case VK_NEXT:
  1510. case VK_DOWN:
  1511. if (!(m_dwFlags & ACF_IGNOREUPDOWN) && !_IsComboboxDropped())
  1512. {
  1513. //
  1514. // If the dropdown is visible, the up-down keys navigate our list
  1515. //
  1516. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  1517. {
  1518. ASSERT(m_hdpa);
  1519. ASSERT(DPA_GetPtrCount(m_hdpa) != 0);
  1520. ASSERT(m_iFirstMatch != -1);
  1521. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  1522. if (iCurSel == -1)
  1523. {
  1524. // If no item selected, first down arrow selects first item
  1525. ListView_SetItemState(m_hwndList, 0, LVIS_SELECTED | LVIS_FOCUSED, 0x000f);
  1526. ListView_EnsureVisible(m_hwndList, 0, FALSE);
  1527. }
  1528. else if (iCurSel == ListView_GetItemCount(m_hwndList)-1)
  1529. {
  1530. // If last item selected, down arrow goes into edit box
  1531. ListView_SetItemState(m_hwndList, iCurSel, 0, 0x000f);
  1532. if (m_pszCurrent)
  1533. {
  1534. // Restore original text if they arrow out of listview
  1535. _SetEditText(m_pszCurrent);
  1536. }
  1537. Edit_SetSel(m_hwndEdit, MAX_URL_STRING, MAX_URL_STRING);
  1538. }
  1539. else
  1540. {
  1541. // If first or middle item selected, down arrow selects next item
  1542. SendMessage(m_hwndList, WM_KEYDOWN, wParam, 0);
  1543. SendMessage(m_hwndList, WM_KEYUP, wParam, 0);
  1544. }
  1545. return FALSE;
  1546. }
  1547. //
  1548. // If Autosuggest drop-down enabled but not popped up then start a search
  1549. // based on the current edit box contents. If the edit box is empty,
  1550. // search for everything.
  1551. //
  1552. else if ((m_dwOptions & ACO_UPDOWNKEYDROPSLIST) && _IsAutoSuggestEnabled())
  1553. {
  1554. // Ensure the background thread knows we have focus
  1555. _GotFocus();
  1556. _StartCompletion(FALSE, TRUE);
  1557. return FALSE;
  1558. }
  1559. //
  1560. // Otherwise we see if we should append the completions in place
  1561. //
  1562. else if (_IsAutoAppendEnabled())
  1563. {
  1564. if (_AppendNext(FALSE))
  1565. {
  1566. return FALSE;
  1567. }
  1568. }
  1569. }
  1570. break;
  1571. case VK_END:
  1572. case VK_HOME:
  1573. _ResetSearch();
  1574. break;
  1575. case VK_BACK:
  1576. //
  1577. // Indicate that selection doesn't match m_psrCurrentlyDisplayed.
  1578. //
  1579. if (0 > GetKeyState(VK_CONTROL))
  1580. {
  1581. //
  1582. // Handle Ctrl-Backspace to delete word.
  1583. //
  1584. int iStart, iEnd;
  1585. SendMessage(m_hwndEdit, EM_GETSEL, (WPARAM)&iStart, (LPARAM)&iEnd);
  1586. //
  1587. // Nothing else must be selected.
  1588. //
  1589. if (iStart == iEnd)
  1590. {
  1591. _GetEditText();
  1592. if (!m_pszCurrent)
  1593. {
  1594. //
  1595. // We didn't handle it, let the
  1596. // other wndprocs try.
  1597. //
  1598. return TRUE;
  1599. }
  1600. //
  1601. // Erase the "word".
  1602. //
  1603. iStart = EditWordBreakProcW(m_pszCurrent, iStart, iStart+1, WB_LEFT);
  1604. Edit_SetSel(m_hwndEdit, iStart, iEnd);
  1605. Edit_ReplaceSel(m_hwndEdit, TEXT(""));
  1606. }
  1607. //
  1608. // We handled it.
  1609. //
  1610. return FALSE;
  1611. }
  1612. break;
  1613. }
  1614. //
  1615. // Let the original wndproc handle it.
  1616. //
  1617. return TRUE;
  1618. }
  1619. LRESULT CAutoComplete::_OnChar(WPARAM wParam, LPARAM lParam)
  1620. {
  1621. LRESULT lres = 0; // means nothing, but we handled the call
  1622. TCHAR cKey = (TCHAR) wParam;
  1623. if (wParam == VK_TAB)
  1624. {
  1625. // Ignore tab characters
  1626. return 0;
  1627. }
  1628. // Ensure the background thread knows we have focus
  1629. _GotFocus();
  1630. if (m_pThread->IsDisabled())
  1631. {
  1632. //
  1633. // Just follow the chain.
  1634. //
  1635. return DefSubclassProc(m_hwndEdit, WM_CHAR, wParam, lParam);
  1636. }
  1637. if (cKey != 127 && cKey != VK_ESCAPE && cKey != VK_RETURN && cKey != 0x0a) // control-backspace is ignored
  1638. {
  1639. // let the default edit wndproc do its thing first
  1640. lres = DefSubclassProc(m_hwndEdit, WM_CHAR, wParam, lParam);
  1641. // ctrl-c is generating a VK_CANCEL. Don't bring up autosuggest in this case.
  1642. if (cKey != VK_CANCEL)
  1643. {
  1644. BOOL fAppend = (cKey != VK_BACK);
  1645. _StartCompletion(fAppend);
  1646. }
  1647. }
  1648. else
  1649. {
  1650. _StopSearch();
  1651. _HideDropDown();
  1652. }
  1653. return lres;
  1654. }
  1655. //+-------------------------------------------------------------------------
  1656. // Starts autocomplete based on the current editbox contents
  1657. //--------------------------------------------------------------------------
  1658. void CAutoComplete::_StartCompletion
  1659. (
  1660. BOOL fAppend, // Ok to append completion in edit box
  1661. BOOL fEvenIfEmpty // = FALSE, Completes to everything if edit box is empty
  1662. )
  1663. {
  1664. // Get the text typed in
  1665. WCHAR szCurrent[MAX_URL_STRING];
  1666. int cchCurrent = GetWindowText(m_hwndEdit, szCurrent, ARRAYSIZE(szCurrent));
  1667. // See if we want a wildcard search
  1668. if (fEvenIfEmpty && cchCurrent == 0)
  1669. {
  1670. cchCurrent = 1;
  1671. szCurrent[0] = CH_WILDCARD;
  1672. szCurrent[1] = 0;
  1673. }
  1674. // If unchanged, we are done
  1675. if (m_pszLastSearch && m_pszCurrent && StrCmpI(m_pszCurrent, szCurrent) == 0)
  1676. {
  1677. if (!(m_hwndDropDown && IsWindowVisible(m_hwndDropDown)) &&
  1678. (-1 != m_iFirstMatch) && _IsAutoSuggestEnabled() &&
  1679. // Don't show drop-down if only one exact match (IForms)
  1680. (m_hdpa &&
  1681. ((m_iLastMatch != m_iFirstMatch) || (((CACString*)DPA_GetPtr(m_hdpa, m_iFirstMatch))->StrCmpI(szCurrent) != 0))))
  1682. {
  1683. _ShowDropDown();
  1684. }
  1685. return;
  1686. }
  1687. // Save the current text
  1688. if (szCurrent[0] == CH_WILDCARD)
  1689. {
  1690. SetStr(&m_pszCurrent, szCurrent);
  1691. }
  1692. else
  1693. {
  1694. _GetEditText();
  1695. }
  1696. //
  1697. // Deselect the current selection in the dropdown
  1698. //
  1699. if (m_hwndList)
  1700. {
  1701. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  1702. if (iCurSel != -1)
  1703. {
  1704. ListView_SetItemState(m_hwndList, iCurSel, 0, 0x000f);
  1705. }
  1706. }
  1707. //
  1708. // If nothing typed in, stop any pending search
  1709. //
  1710. if (cchCurrent == 0)
  1711. {
  1712. if (m_pszCurrent)
  1713. {
  1714. _StopSearch();
  1715. if (m_pszCurrent)
  1716. {
  1717. SetStr(&m_pszCurrent, NULL);
  1718. }
  1719. // Free last completion
  1720. _HideDropDown();
  1721. }
  1722. }
  1723. //
  1724. // See if we need to generate a new list
  1725. //
  1726. else
  1727. {
  1728. int iCompleted = m_pszLastSearch ? lstrlen(m_pszLastSearch) : 0;
  1729. int iScheme = URL_SCHEME_UNKNOWN;
  1730. // Get length of common prefix (if any)
  1731. int cchPrefix = IsFlagSet(m_dwOptions, ACO_FILTERPREFIXES) ?
  1732. CACThread::GetSpecialPrefixLen(szCurrent) : 0;
  1733. if (
  1734. // If no previous completion, start a new search
  1735. (0 == iCompleted) ||
  1736. // If the list was truncated (reached limit), we need to refetch
  1737. m_fNeedNewList ||
  1738. // We purge matches to common prefixes ("www.", "http://" etc). If the
  1739. // last search may have resulted in items being filtered out, and the
  1740. // new string will not, then we need to refetch.
  1741. (cchPrefix > 0 && cchPrefix < cchCurrent && CACThread::MatchesSpecialPrefix(m_pszLastSearch)) ||
  1742. // If the portion we last completed to was altered, we need to refetch
  1743. (StrCmpNI(m_pszLastSearch, szCurrent, iCompleted) != 0) ||
  1744. // If we have entered a new folder, we need to refetch
  1745. (StrChrI(szCurrent + iCompleted, DIR_SEPARATOR_CHAR) != NULL) ||
  1746. // If we have entered a url folder, we need to refetch (ftp://shapitst/Bryanst/)
  1747. ((StrChrI(szCurrent + iCompleted, URL_SEPARATOR_CHAR) != NULL) &&
  1748. (URL_SCHEME_FTP == (iScheme = GetUrlScheme(szCurrent))))
  1749. )
  1750. {
  1751. // If the last search was truncated, make sure we try the next search with more characters
  1752. int cchMin = cchPrefix + 1;
  1753. if (m_fNeedNewList)
  1754. {
  1755. cchMin = iCompleted + 1;
  1756. }
  1757. // Find the last '\\' (or '/' for ftp)
  1758. int i = cchCurrent - 1;
  1759. while ((szCurrent[i] != DIR_SEPARATOR_CHAR) &&
  1760. !((szCurrent[i] == URL_SEPARATOR_CHAR) && (iScheme == URL_SCHEME_FTP)) &&
  1761. (i >= cchMin))
  1762. {
  1763. --i;
  1764. }
  1765. // Start a new search
  1766. szCurrent[i+1] = 0;
  1767. if (_StartSearch(szCurrent))
  1768. SetStr(&m_pszLastSearch, szCurrent);
  1769. }
  1770. // Otherwise we can simply update from our last completion list
  1771. else
  1772. {
  1773. //
  1774. if (m_hdpa)
  1775. {
  1776. _UpdateCompletion(szCurrent, -1, fAppend);
  1777. }
  1778. else
  1779. {
  1780. // Awaiting completion, cache new match...
  1781. }
  1782. }
  1783. }
  1784. }
  1785. //+-------------------------------------------------------------------------
  1786. // Get the background thread to start a new search
  1787. //--------------------------------------------------------------------------
  1788. BOOL CAutoComplete::_StartSearch(LPCWSTR pszSeatch)
  1789. {
  1790. // Empty the dropdown list. To minimize flash, we don't hide it unless
  1791. // the search comes up empty
  1792. if (m_hwndList)
  1793. {
  1794. ListView_SetItemCountEx(m_hwndList, 0, 0);
  1795. }
  1796. return m_pThread->StartSearch(pszSeatch, m_dwOptions);
  1797. }
  1798. //+-------------------------------------------------------------------------
  1799. // Get the background thread to abort the last search
  1800. //--------------------------------------------------------------------------
  1801. void CAutoComplete::_StopSearch()
  1802. {
  1803. SetStr(&m_pszLastSearch, NULL);
  1804. m_pThread->StopSearch();
  1805. }
  1806. //+-------------------------------------------------------------------------
  1807. // Informs the background thread that we have focus.
  1808. //--------------------------------------------------------------------------
  1809. void CAutoComplete::_GotFocus()
  1810. {
  1811. if (!m_pThread->HasFocus())
  1812. {
  1813. m_pThread->GotFocus();
  1814. }
  1815. }
  1816. //+-------------------------------------------------------------------------
  1817. // Message from background thread indicating that the search was completed
  1818. //--------------------------------------------------------------------------
  1819. void CAutoComplete::_OnSearchComplete
  1820. (
  1821. HDPA hdpa, // New completion list
  1822. DWORD dwSearchStatus // see SRCH_* flags
  1823. )
  1824. {
  1825. _FreeDPAPtrs(m_hdpa);
  1826. m_hdpa = hdpa;
  1827. m_fNeedNewList = IsFlagSet(dwSearchStatus, SRCH_LIMITREACHED);
  1828. if (IsFlagSet(dwSearchStatus, SRCH_USESORTINDEX))
  1829. {
  1830. if (NULL == m_hdpaSortIndex)
  1831. {
  1832. m_hdpaSortIndex = DPA_Create(AC_LIST_GROWTH_CONST);
  1833. }
  1834. }
  1835. else if (m_hdpaSortIndex)
  1836. {
  1837. DPA_Destroy(m_hdpaSortIndex);
  1838. m_hdpaSortIndex = NULL;
  1839. }
  1840. // Was it a wildcard search?
  1841. BOOL fWildCard = m_pszLastSearch && (m_pszLastSearch[0] == CH_WILDCARD) && (m_pszLastSearch[1] == L'\0');
  1842. //
  1843. // See if we should add "Search for <stuff typed in>" to the end of
  1844. // the list.
  1845. //
  1846. m_fSearchForAdded = FALSE;
  1847. if (!fWildCard && (m_dwOptions & ACO_SEARCH))
  1848. {
  1849. // Add "Search for <stuff typed in>" to the end of the list
  1850. // First make sure we have a dpa
  1851. if (m_hdpa == NULL)
  1852. {
  1853. m_hdpa = DPA_Create(AC_LIST_GROWTH_CONST);
  1854. }
  1855. if (m_hdpa)
  1856. {
  1857. // Create a bogus entry and add to the end of the list. This place
  1858. // holder makes sure the drop-down does not go away when there are no
  1859. // matching entries.
  1860. CACString* pStr = CreateACString(L"", 0, 0);
  1861. if (pStr)
  1862. {
  1863. if (DPA_AppendPtr(m_hdpa, pStr) != -1)
  1864. {
  1865. m_fSearchForAdded = TRUE;
  1866. }
  1867. else
  1868. {
  1869. pStr->Release();
  1870. }
  1871. }
  1872. }
  1873. }
  1874. // If no search results, hide our dropdown
  1875. if (NULL == m_hdpa || 0 == DPA_GetPtrCount(m_hdpa))
  1876. {
  1877. _HideDropDown();
  1878. if (m_hwndList)
  1879. {
  1880. ListView_SetItemCountEx(m_hwndList, 0, 0);
  1881. }
  1882. m_iFirstMatch = -1;
  1883. }
  1884. else
  1885. {
  1886. if (m_pszCurrent)
  1887. {
  1888. // If we are still waiting for a completion, then update the completion list
  1889. if (m_pszLastSearch)
  1890. {
  1891. _UpdateCompletion(m_pszCurrent, -1, TRUE);
  1892. }
  1893. }
  1894. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  1895. {
  1896. _PositionDropDown(); // Resize based on number of hits
  1897. _UpdateScrollbar();
  1898. }
  1899. }
  1900. }
  1901. //+-------------------------------------------------------------------------
  1902. // Returns the text for an item in the autocomplete list
  1903. //--------------------------------------------------------------------------
  1904. BOOL CAutoComplete::_GetItem
  1905. (
  1906. int iIndex, // zero-based index
  1907. LPWSTR pszText, // location to return text
  1908. int cchMax, // size of pszText buffer
  1909. BOOL fDisplayName // TRUE = return name to display
  1910. // FALSE = return name to go to edit box
  1911. )
  1912. {
  1913. // Check for special "Search for <typed in>" entry at end of the list
  1914. if (m_fSearchFor && iIndex == m_iLastMatch - m_iFirstMatch)
  1915. {
  1916. ASSERT(NULL != m_pszCurrent);
  1917. WCHAR szFormat[MAX_PATH];
  1918. int id = fDisplayName ? IDS_SEARCHFOR : IDS_SEARCHFORCMD;
  1919. MLLoadString(id, szFormat, ARRAYSIZE(szFormat));
  1920. StringCchPrintf(pszText, cchMax, szFormat, m_pszCurrent);
  1921. }
  1922. // Normal list entry
  1923. else
  1924. {
  1925. CACString* pStr;
  1926. // If we're using Sorting indicies, then we are retrieving our entries
  1927. // out of m_hdpaPrefix sort, which contains only matched entries
  1928. if (m_hdpaSortIndex)
  1929. {
  1930. pStr = (CACString *)DPA_GetPtr(m_hdpaSortIndex, iIndex);
  1931. }
  1932. else
  1933. {
  1934. pStr = (CACString *)DPA_GetPtr(m_hdpa, iIndex + m_iFirstMatch);
  1935. }
  1936. if (pStr)
  1937. {
  1938. StringCchCopy(pszText, cchMax, pStr->GetStr());
  1939. }
  1940. else if (cchMax >= 1)
  1941. {
  1942. pszText[0] = 0;
  1943. }
  1944. }
  1945. return TRUE;
  1946. }
  1947. //+-------------------------------------------------------------------------
  1948. // Frees an item in our autocomplete list
  1949. //--------------------------------------------------------------------------
  1950. int CAutoComplete::_DPADestroyCallback(LPVOID p, LPVOID d)
  1951. {
  1952. ((CACString*)p)->Release();
  1953. return 1;
  1954. }
  1955. //+-------------------------------------------------------------------------
  1956. // Frees our last completion list
  1957. //--------------------------------------------------------------------------
  1958. void CAutoComplete::_FreeDPAPtrs(HDPA hdpa)
  1959. {
  1960. TraceMsg(AC_GENERAL, "CAutoComplete::_FreeDPAPtrs(hdpa = 0x%x)", hdpa);
  1961. if (hdpa)
  1962. {
  1963. DPA_DestroyCallback(hdpa, _DPADestroyCallback, 0);
  1964. hdpa = NULL;
  1965. }
  1966. }
  1967. //+-------------------------------------------------------------------------
  1968. // DPA callback used to order the matches by the sort index
  1969. //--------------------------------------------------------------------------
  1970. int CALLBACK CAutoComplete::_DPACompareSortIndex(LPVOID p1, LPVOID p2, LPARAM lParam)
  1971. {
  1972. CACString* ps1 = (CACString*)p1;
  1973. CACString* ps2 = (CACString*)p2;
  1974. return ps1->CompareSortingIndex(*ps2);
  1975. }
  1976. //+-------------------------------------------------------------------------
  1977. // Updates the matching completion
  1978. //--------------------------------------------------------------------------
  1979. void CAutoComplete::_UpdateCompletion
  1980. (
  1981. LPCWSTR pszTyped, // typed in string to match
  1982. int iChanged, // char added since last update or -1
  1983. BOOL fAppend // ok to append completion
  1984. )
  1985. {
  1986. int iFirstMatch = -1;
  1987. int iLastMatch = -1;
  1988. int nChars = lstrlen(pszTyped);
  1989. // Was it a wildcard search?
  1990. BOOL fWildCard = pszTyped && (pszTyped[0] == CH_WILDCARD) && (pszTyped[1] == L'\0');
  1991. if (fWildCard && DPA_GetPtrCount(m_hdpa))
  1992. {
  1993. // Everything matches
  1994. iFirstMatch = 0;
  1995. iLastMatch = DPA_GetPtrCount(m_hdpa) - 1;
  1996. }
  1997. else
  1998. {
  1999. // PERF: Special case where current == search string
  2000. /*
  2001. //
  2002. // Find the first matching index
  2003. //
  2004. if (iChanged > 0)
  2005. {
  2006. // PERF: Get UC and LC versions of WC for compare below?
  2007. WCHAR wc = pszTyped[iChanged];
  2008. // A character was added so search from current location
  2009. for (int i = m_iFirstMatch; i < DPA_GetPtrCount(m_hdpa); ++i)
  2010. {
  2011. CACString* pStr;
  2012. pStr = (CACString*)DPA_GetPtr(m_hdpa, i);
  2013. ASSERT(pStr);
  2014. if (pStr && pStr->GetLength() >= iChanged && ChrCmpI((*pStr)[iChanged], wc) == 0)
  2015. {
  2016. // This is the first match
  2017. iFirstMatch = i;
  2018. break;
  2019. }
  2020. }
  2021. }
  2022. else
  2023. */
  2024. {
  2025. // We have to search the whole list
  2026. // PERF: Switch to a binary search.
  2027. for (int i = 0; i < DPA_GetPtrCount(m_hdpa); ++i)
  2028. {
  2029. CACString* pStr;
  2030. pStr = (CACString*)DPA_GetPtr(m_hdpa, i);
  2031. ASSERT(pStr);
  2032. if (pStr &&
  2033. (pStr->StrCmpNI(pszTyped, nChars) == 0))
  2034. {
  2035. iFirstMatch = i;
  2036. break;
  2037. }
  2038. }
  2039. }
  2040. if (-1 != iFirstMatch)
  2041. {
  2042. //
  2043. // Find the last match
  2044. //
  2045. // PERF: Should we binary search up to the last end of list?
  2046. for (iLastMatch = iFirstMatch; iLastMatch + 1 < DPA_GetPtrCount(m_hdpa); ++iLastMatch)
  2047. {
  2048. CACString* pStr;
  2049. pStr = (CACString*)DPA_GetPtr(m_hdpa, iLastMatch + 1);
  2050. ASSERT(pStr);
  2051. if (NULL == pStr || (pStr->StrCmpNI(pszTyped, nChars) != 0))
  2052. {
  2053. break;
  2054. }
  2055. }
  2056. }
  2057. }
  2058. //
  2059. // See if we should add "Search for <stuff typed in>" to the end of
  2060. // the list.
  2061. //
  2062. int iSearchFor = 0;
  2063. int nScheme;
  2064. if (m_fSearchForAdded &&
  2065. // Not a drive letter
  2066. (*pszTyped && pszTyped[1] != L':') &&
  2067. // Not a UNC path
  2068. (pszTyped[0] != L'\\' && pszTyped[1] != L'\\') &&
  2069. // Not a known scheme
  2070. ((nScheme = GetUrlScheme(pszTyped)) == URL_SCHEME_UNKNOWN ||
  2071. nScheme == URL_SCHEME_INVALID) &&
  2072. // Ignore anything theat begins with "www"
  2073. !(pszTyped[0] == L'w' && pszTyped[1] == L'w' && pszTyped[2] == L'w')
  2074. // Not a search keyword
  2075. // !Is
  2076. )
  2077. {
  2078. // Add "Search for <stuff typed in>"
  2079. iSearchFor = 1;
  2080. }
  2081. m_fSearchFor = iSearchFor;
  2082. m_iLastMatch = iLastMatch + iSearchFor;
  2083. m_iFirstMatch = iFirstMatch;
  2084. if (iSearchFor && iFirstMatch == -1)
  2085. {
  2086. // There is one entry - the special "search for <>" entry
  2087. m_iFirstMatch = 0;
  2088. }
  2089. // Sort the matches using the sort index
  2090. if (m_hdpaSortIndex && iFirstMatch != -1)
  2091. {
  2092. // First put the matches in this sorted dpa
  2093. DPA_GetPtrCount(m_hdpaSortIndex) = 0;
  2094. for (int i=0; i <= m_iLastMatch-m_iFirstMatch; i++)
  2095. {
  2096. CACString *pStr = (CACString *)DPA_GetPtr(m_hdpa, m_iFirstMatch+i);
  2097. DPA_InsertPtr(m_hdpaSortIndex, i, (LPVOID)pStr);
  2098. }
  2099. // Now order it by sort index (rather than alphabetically)
  2100. DPA_Sort(m_hdpaSortIndex, _DPACompareSortIndex, 0);
  2101. }
  2102. if (_IsAutoSuggestEnabled())
  2103. {
  2104. // Update our drop-down list
  2105. if ((m_iFirstMatch == -1) || // Hide if there are no matches
  2106. ((m_iLastMatch == m_iFirstMatch) && // Or if only one match which we've already typed (IForms)
  2107. (((CACString*)DPA_GetPtr(m_hdpa, m_iFirstMatch))->StrCmpI(pszTyped) == 0)))
  2108. {
  2109. _HideDropDown();
  2110. }
  2111. else
  2112. {
  2113. if (m_hwndList)
  2114. {
  2115. int cItems = m_iLastMatch - m_iFirstMatch + 1;
  2116. ListView_SetItemCountEx(m_hwndList, cItems, 0);
  2117. }
  2118. _ShowDropDown();
  2119. _UpdateScrollbar();
  2120. }
  2121. }
  2122. if (_IsAutoAppendEnabled() && fAppend && m_iFirstMatch != -1 && !fWildCard)
  2123. {
  2124. // If caret is not at the end of the string, don't append
  2125. DWORD dwSel = Edit_GetSel(m_hwndEdit);
  2126. int iStartSel = LOWORD(dwSel);
  2127. int iEndSel = HIWORD(dwSel);
  2128. int iLen = lstrlen(pszTyped);
  2129. if (iStartSel == iStartSel && iStartSel != iLen)
  2130. {
  2131. return;
  2132. }
  2133. // FEATURE: Should use the shortest match
  2134. m_iAppended = -1;
  2135. _AppendNext(TRUE);
  2136. }
  2137. }
  2138. //+-------------------------------------------------------------------------
  2139. // Appends the next completion to the current edit text. Returns TRUE if
  2140. // successful.
  2141. //--------------------------------------------------------------------------
  2142. BOOL CAutoComplete::_AppendNext
  2143. (
  2144. BOOL fAppendToWhack // Apend to next whack (false = append entire match)
  2145. )
  2146. {
  2147. // Nothing to complete?
  2148. if (NULL == m_hdpa || 0 == DPA_GetPtrCount(m_hdpa) ||
  2149. m_iFirstMatch == -1 || !_WantToAppendResults())
  2150. return FALSE;
  2151. //
  2152. // If nothing currently appended, init to the
  2153. // last item so that we will wrap around to the
  2154. // first item
  2155. //
  2156. if (m_iAppended == -1)
  2157. {
  2158. m_iAppended = m_iLastMatch;
  2159. }
  2160. int iAppend = m_iAppended;
  2161. CACString* pStr;
  2162. //
  2163. // Loop through the items until we find one without a prefix
  2164. //
  2165. do
  2166. {
  2167. if (++iAppend > m_iLastMatch)
  2168. {
  2169. iAppend = m_iFirstMatch;
  2170. }
  2171. pStr = (CACString*)DPA_GetPtr(m_hdpa, iAppend);
  2172. if (pStr &&
  2173. // Don't append if match has as www. prefix
  2174. (pStr->PrefixLength() < 4 || StrCmpNI(pStr->GetStr() + pStr->PrefixLength() - 4, L"www.", 4) != 0) &&
  2175. // Ignore the "Search for" if present
  2176. !(m_fSearchFor && iAppend == m_iLastMatch))
  2177. {
  2178. // We found one so append it
  2179. _Append(*pStr, fAppendToWhack);
  2180. m_iAppended = iAppend;
  2181. }
  2182. }
  2183. while (iAppend != m_iAppended);
  2184. return TRUE;
  2185. }
  2186. //+-------------------------------------------------------------------------
  2187. // Appends the previous completion to the current edit text. Returns TRUE
  2188. // if successful.
  2189. //--------------------------------------------------------------------------
  2190. BOOL CAutoComplete::_AppendPrevious
  2191. (
  2192. BOOL fAppendToWhack // Append to next whack (false = append entire match)
  2193. )
  2194. {
  2195. // Nothing to complete?
  2196. if (NULL == m_hdpa || 0 == DPA_GetPtrCount(m_hdpa) ||
  2197. m_iFirstMatch == -1 || !_WantToAppendResults())
  2198. return FALSE;
  2199. //
  2200. // If nothing currently appended, init to the
  2201. // first item so that we will wrap around to the
  2202. // last item
  2203. //
  2204. if (m_iAppended == -1)
  2205. {
  2206. m_iAppended = m_iFirstMatch;
  2207. }
  2208. int iAppend = m_iAppended;
  2209. CACString* pStr;
  2210. //
  2211. // Loop through the items until we find one without a prefix
  2212. //
  2213. do
  2214. {
  2215. if (--iAppend < m_iFirstMatch)
  2216. {
  2217. iAppend = m_iLastMatch;
  2218. }
  2219. pStr = (CACString*)DPA_GetPtr(m_hdpa, iAppend);
  2220. if (pStr &&
  2221. // Don't append if match has as www. prefix
  2222. (pStr->PrefixLength() < 4 || StrCmpNI(pStr->GetStr() + pStr->PrefixLength() - 4, L"www.", 4) != 0) &&
  2223. // Ignore the "Search for" if present
  2224. !(m_fSearchFor && iAppend == m_iLastMatch))
  2225. {
  2226. // We found one so append it
  2227. _Append(*pStr, fAppendToWhack);
  2228. m_iAppended = iAppend;
  2229. }
  2230. }
  2231. while (iAppend != m_iAppended);
  2232. return TRUE;
  2233. }
  2234. //+-------------------------------------------------------------------------
  2235. // Appends the completion to the current edit text
  2236. //--------------------------------------------------------------------------
  2237. void CAutoComplete::_Append
  2238. (
  2239. CACString& rStr, // item to append to the editbox text
  2240. BOOL fAppendToWhack // Apend to next whack (false = append entire match)
  2241. )
  2242. {
  2243. ASSERT(_IsAutoAppendEnabled());
  2244. if (m_pszCurrent)
  2245. {
  2246. int cchCurrent = lstrlen(m_pszCurrent);
  2247. LPCWSTR pszAppend = rStr.GetStrToCompare() + cchCurrent;
  2248. int cchAppend;
  2249. if (fAppendToWhack)
  2250. {
  2251. //
  2252. // Advance to the whacks.
  2253. //
  2254. const WCHAR *pch = pszAppend;
  2255. cchAppend = 0;
  2256. while (*pch && !_IsWhack(*pch))
  2257. {
  2258. ++cchAppend;
  2259. pch++;
  2260. }
  2261. //
  2262. // Advance past the whacks.
  2263. //
  2264. while (*pch && _IsWhack(*pch))
  2265. {
  2266. ++cchAppend;
  2267. pch++;
  2268. }
  2269. }
  2270. else
  2271. {
  2272. // Append entire match
  2273. cchAppend = lstrlen(pszAppend);
  2274. }
  2275. WCHAR szAppend[MAX_URL_STRING];
  2276. StringCchCopy(szAppend, cchAppend + 1, pszAppend);
  2277. _UpdateText(cchCurrent, cchCurrent + cchAppend, m_pszCurrent, szAppend);
  2278. m_fAppended = TRUE;
  2279. }
  2280. }
  2281. //+-------------------------------------------------------------------------
  2282. // Hides the AutoSuggest dropdown
  2283. //--------------------------------------------------------------------------
  2284. void CAutoComplete::_HideDropDown()
  2285. {
  2286. if (m_hwndDropDown)
  2287. {
  2288. ShowWindow(m_hwndDropDown, SW_HIDE);
  2289. }
  2290. }
  2291. //+-------------------------------------------------------------------------
  2292. // Shows and positions the autocomplete dropdown
  2293. //--------------------------------------------------------------------------
  2294. void CAutoComplete::_ShowDropDown()
  2295. {
  2296. if (m_hwndDropDown && !_IsComboboxDropped() && !m_fImeCandidateOpen)
  2297. {
  2298. // If the edit window is visible, it better have focus!
  2299. // (Intelliforms uses an invisible window that doesn't
  2300. // get focus.)
  2301. if (IsWindowVisible(m_hwndEdit) && m_hwndEdit != GetFocus())
  2302. {
  2303. ShowWindow(m_hwndDropDown, SW_HIDE);
  2304. return;
  2305. }
  2306. if (!IsWindowVisible(m_hwndDropDown))
  2307. {
  2308. // It should not be possible to open a new dropdown while
  2309. // another dropdown is visible! But to be safe we'll check ...
  2310. if (s_hwndDropDown)
  2311. {
  2312. ASSERT(FALSE);
  2313. ShowWindow(s_hwndDropDown, SW_HIDE);
  2314. }
  2315. s_hwndDropDown = m_hwndDropDown;
  2316. //
  2317. // Install a thread hook so that we can detect when something
  2318. // happens that should hide the dropdown.
  2319. //
  2320. ENTERCRITICAL;
  2321. if (s_hhookMouse)
  2322. {
  2323. // Should never happen because the hook is removed when the dropdown
  2324. // is hidden. But we can't afford to orphan a hook so we check just
  2325. // in case!
  2326. ASSERT(FALSE);
  2327. UnhookWindowsHookEx(s_hhookMouse);
  2328. }
  2329. s_hhookMouse = SetWindowsHookEx(WH_MOUSE, s_MouseHook, HINST_THISDLL, NULL);
  2330. LEAVECRITICAL;
  2331. //
  2332. // Subclass the parent windows so that we can detect when something
  2333. // happens that should hide the dropdown
  2334. //
  2335. _SubClassParent(m_hwndEdit);
  2336. }
  2337. _PositionDropDown();
  2338. }
  2339. }
  2340. //+-------------------------------------------------------------------------
  2341. // Positions dropdown based on edit window position
  2342. //--------------------------------------------------------------------------
  2343. void CAutoComplete::_PositionDropDown()
  2344. {
  2345. RECT rcEdit;
  2346. GetWindowRect(m_hwndEdit, &rcEdit);
  2347. int x = rcEdit.left;
  2348. int y = rcEdit.bottom;
  2349. // Don't resize if user already has
  2350. if (!m_fDropDownResized)
  2351. {
  2352. m_nDropHeight = 100;
  2353. MINMAXINFO mmi = {0};
  2354. SendMessage(m_hwndDropDown, WM_GETMINMAXINFO, 0, (LPARAM)&mmi);
  2355. m_nDropWidth = max(RECTWIDTH(rcEdit), mmi.ptMinTrackSize.x);
  2356. // Calculate dropdown height based on number of string matches
  2357. if (m_hdpa)
  2358. {
  2359. /*
  2360. int iDropDownHeight =
  2361. m_nStatusHeight +
  2362. ListView_GetItemSpacing(m_hwndList, FALSE) * DPA_GetPtrCount(m_hdpa);
  2363. */
  2364. int iDropDownHeight =
  2365. m_nStatusHeight - GetSystemMetrics(SM_CYBORDER) +
  2366. HIWORD(ListView_ApproximateViewRect(m_hwndList, -1, -1, -1));
  2367. if (m_nDropHeight > iDropDownHeight)
  2368. {
  2369. m_nDropHeight = iDropDownHeight;
  2370. }
  2371. }
  2372. }
  2373. int w = m_nDropWidth;
  2374. int h = m_nDropHeight;
  2375. BOOL fDroppedUp = FALSE;
  2376. //
  2377. // Make sure we don't go off the screen
  2378. //
  2379. HMONITOR hMonitor = MonitorFromWindow(m_hwndEdit, MONITOR_DEFAULTTONEAREST);
  2380. if (hMonitor)
  2381. {
  2382. MONITORINFO mi;
  2383. mi.cbSize = sizeof(mi);
  2384. if (GetMonitorInfo(hMonitor, &mi))
  2385. {
  2386. RECT rcMon = mi.rcMonitor;
  2387. int cxMax = rcMon.right - rcMon.left;
  2388. if (w > cxMax)
  2389. {
  2390. w = cxMax;
  2391. }
  2392. /*
  2393. if (x < rcMon.left)
  2394. {
  2395. // Off the left edge, so move right
  2396. x += rcMon.left - x;
  2397. }
  2398. else if (x + w > rcMon.right)
  2399. {
  2400. // Off the right edge, so move left
  2401. x -= (x + w - rcMon.right);
  2402. }
  2403. */
  2404. int cyMax = (RECTHEIGHT(rcMon) - RECTHEIGHT(rcEdit));
  2405. if (h > cyMax)
  2406. {
  2407. h = cyMax;
  2408. }
  2409. if (y + h > rcMon.bottom
  2410. #ifdef ALLOW_ALWAYS_DROP_UP
  2411. || m_fAlwaysDropUp
  2412. #endif
  2413. )
  2414. {
  2415. // Off the bottom of the screen, so see if there is more
  2416. // room in the up direction
  2417. if (rcEdit.top > rcMon.bottom - rcEdit.bottom)
  2418. {
  2419. // There's more room to pop up
  2420. y = max(rcEdit.top - h, 0);
  2421. h = rcEdit.top - y;
  2422. fDroppedUp = TRUE;
  2423. }
  2424. else
  2425. {
  2426. // Don't let it go past the bottom
  2427. h = rcMon.bottom - y;
  2428. }
  2429. }
  2430. }
  2431. }
  2432. BOOL fFlipped = BOOLIFY(m_fDroppedUp) ^ BOOLIFY(fDroppedUp);
  2433. m_fDroppedUp = fDroppedUp;
  2434. SetWindowPos(m_hwndDropDown, HWND_TOP, x, y, w, h, SWP_SHOWWINDOW | SWP_NOACTIVATE);
  2435. if (fFlipped)
  2436. {
  2437. _UpdateGrip();
  2438. }
  2439. }
  2440. //+-------------------------------------------------------------------------
  2441. // Window procedure for the subclassed edit box
  2442. //--------------------------------------------------------------------------
  2443. LRESULT CAutoComplete::_EditWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  2444. {
  2445. switch (uMsg)
  2446. {
  2447. case WM_SETTEXT:
  2448. //
  2449. // If the text is changed programmatically, we hide the dropdown.
  2450. // This fixed a bug in the dialog at:
  2451. //
  2452. // Internet Options\security\Local Intranet\Sites\Advanced
  2453. //
  2454. // - If you select something in the dropdown the press enter,
  2455. // the enter key is intercepeted by the dialog which clears
  2456. // the edit field, but the drop-down is not hidden.
  2457. //
  2458. if (!m_fSettingText)
  2459. {
  2460. _HideDropDown();
  2461. }
  2462. break;
  2463. case WM_GETDLGCODE:
  2464. {
  2465. //
  2466. // If the auto-suggest drop-down if up, we process
  2467. // the tab key.
  2468. //
  2469. BOOL fDropDownVisible = m_hwndDropDown && IsWindowVisible(m_hwndDropDown);
  2470. if (wParam == VK_TAB && IsFlagSet(m_dwOptions, ACO_USETAB))
  2471. {
  2472. if ((GetKeyState(VK_CONTROL) < 0) ||
  2473. !fDropDownVisible)
  2474. {
  2475. break;
  2476. }
  2477. // We want the tab key
  2478. return DLGC_WANTTAB;
  2479. }
  2480. else if (wParam == VK_ESCAPE && fDropDownVisible)
  2481. {
  2482. // eat escape so that dialog boxes (e.g. File Open) are not closed
  2483. return DLGC_WANTALLKEYS;
  2484. }
  2485. break;
  2486. }
  2487. case WM_KEYDOWN:
  2488. if (wParam == VK_TAB)
  2489. {
  2490. BOOL fDropDownVisible = m_hwndDropDown && IsWindowVisible(m_hwndDropDown);
  2491. if (fDropDownVisible &&
  2492. GetKeyState(VK_CONTROL) >= 0)
  2493. {
  2494. // Map tab to down-arrow and shift-tab to up-arrow
  2495. wParam = (GetKeyState(VK_SHIFT) >= 0) ? VK_DOWN : VK_UP;
  2496. }
  2497. else
  2498. {
  2499. return 0;
  2500. }
  2501. }
  2502. // Ensure the background thread knows we have focus
  2503. _GotFocus();
  2504. // ASSERT(m_hThread || m_pThread->IsDisabled()); // If this occurs then we didn't process a WM_SETFOCUS when we should have. BryanSt.
  2505. if (_OnKeyDown(wParam) == 0)
  2506. {
  2507. //
  2508. // We handled it.
  2509. //
  2510. return 0;
  2511. }
  2512. if (wParam == VK_DELETE)
  2513. {
  2514. LRESULT lRes = DefSubclassProc(m_hwndEdit, uMsg, wParam, lParam);
  2515. _StartCompletion(FALSE);
  2516. return lRes;
  2517. }
  2518. break;
  2519. case WM_CHAR:
  2520. return _OnChar(wParam, lParam);
  2521. case WM_CUT:
  2522. case WM_PASTE:
  2523. case WM_CLEAR:
  2524. {
  2525. LRESULT lRet = DefSubclassProc(m_hwndEdit, uMsg, wParam, lParam);
  2526. // See if we need to update the completion
  2527. if (!m_pThread->IsDisabled())
  2528. {
  2529. _GotFocus();
  2530. _StartCompletion(TRUE);
  2531. }
  2532. return lRet;
  2533. }
  2534. case WM_SETFOCUS:
  2535. m_pThread->GotFocus();
  2536. break;
  2537. case WM_KILLFOCUS:
  2538. {
  2539. HWND hwndGetFocus = (HWND)wParam;
  2540. // Ignore focus change to ourselves
  2541. if (m_hwndEdit != hwndGetFocus)
  2542. {
  2543. if (m_hwndDropDown && GetFocus() != m_hwndDropDown)
  2544. {
  2545. _HideDropDown();
  2546. }
  2547. m_pThread->LostFocus();
  2548. }
  2549. break;
  2550. }
  2551. case WM_DESTROY:
  2552. {
  2553. HWND hwndEdit = m_hwndEdit;
  2554. TraceMsg(AC_GENERAL, "CAutoComplete::_WndProc(WM_DESTROY) releasing subclass.");
  2555. RemoveWindowSubclass(hwndEdit, s_EditWndProc, 0);
  2556. if (m_hwndDropDown)
  2557. {
  2558. //
  2559. // If the dropdown and its associated listview are fully created, we can simply
  2560. // destroy them.
  2561. //
  2562. if (m_hwndList)
  2563. {
  2564. DestroyWindow(m_hwndDropDown);
  2565. }
  2566. //
  2567. // We are likely in the middle of creating the listview and destroy the dropdown now
  2568. // can result in a crash (another thread probably destroyed the edit window). So we
  2569. // post a message to get the dropdown to destroy itself. The background thread will
  2570. // hold this dll in memory until the dropdown is gone.
  2571. //
  2572. else
  2573. {
  2574. // Don't call DestroyWindow. See AM_DESTROY comment in dropdown's wndproc.
  2575. _GotFocus();
  2576. if (m_pThread->HasFocus())
  2577. {
  2578. PostMessage(m_hwndDropDown, AM_DESTROY, 0, 0);
  2579. }
  2580. else
  2581. {
  2582. // If the background thread is not running, we can't rely on it to
  2583. // keep us in memory during shutdown, so synchronously destroy the
  2584. // window and cross your fingers.
  2585. DestroyWindow(m_hwndDropDown);
  2586. }
  2587. }
  2588. }
  2589. m_pThread->SyncShutDownBGThread();
  2590. SAFERELEASE(m_pThread);
  2591. Release(); // Release subclass Ref.
  2592. // Pass it onto the old wndproc.
  2593. return DefSubclassProc(hwndEdit, uMsg, wParam, lParam);
  2594. }
  2595. break;
  2596. case WM_MOVE:
  2597. {
  2598. if (m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  2599. {
  2600. // Follow edit window, for example when intelliforms window scrolls w/intellimouse
  2601. _PositionDropDown();
  2602. }
  2603. }
  2604. break;
  2605. /*
  2606. case WM_COMMAND:
  2607. if (m_pThread->IsDisabled())
  2608. {
  2609. break;
  2610. }
  2611. return _OnCommand(wParam, lParam);
  2612. */
  2613. /*
  2614. case WM_CONTEXTMENU:
  2615. if (m_pThread->IsDisabled())
  2616. {
  2617. break;
  2618. }
  2619. return _ContextMenu(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  2620. */
  2621. case WM_LBUTTONDBLCLK:
  2622. {
  2623. //
  2624. // Bypass our word break routine. We only register this callback on NT because it
  2625. // doesn't work right on win9x.
  2626. //
  2627. if (m_fEditControlUnicode)
  2628. {
  2629. //
  2630. // We break words at url delimiters for ctrl-left & ctrl-right, but
  2631. // we want double-click to use standard word selection so that it is easy
  2632. // to select the URL.
  2633. //
  2634. SendMessage(m_hwndEdit, EM_SETWORDBREAKPROC, 0, (DWORD_PTR)m_oldEditWordBreakProc);
  2635. LRESULT lres = DefSubclassProc(m_hwndEdit, uMsg, wParam, lParam);
  2636. // Restore our word-break callback
  2637. SendMessage(m_hwndEdit, EM_SETWORDBREAKPROC, 0, (DWORD_PTR)EditWordBreakProcW);
  2638. return lres;
  2639. }
  2640. break;
  2641. }
  2642. case WM_SETFONT:
  2643. {
  2644. // If we have a dropdown, recreate it with the latest font
  2645. m_hfontListView = (HFONT)wParam;
  2646. if (m_hwndDropDown)
  2647. {
  2648. _StopSearch();
  2649. DestroyWindow(m_hwndDropDown);
  2650. m_hwndDropDown = NULL;
  2651. _SeeWhatsEnabled();
  2652. }
  2653. break;
  2654. }
  2655. case WM_IME_NOTIFY:
  2656. {
  2657. // We don't want autocomplete to obsure the IME candidate window
  2658. DWORD dwCommand = (DWORD)wParam;
  2659. if (dwCommand == IMN_OPENCANDIDATE)
  2660. {
  2661. m_fImeCandidateOpen = TRUE;
  2662. _HideDropDown();
  2663. }
  2664. else if (dwCommand == IMN_CLOSECANDIDATE)
  2665. {
  2666. m_fImeCandidateOpen = FALSE;
  2667. }
  2668. }
  2669. break;
  2670. default:
  2671. // Handle registered messages
  2672. if (uMsg == m_uMsgSearchComplete)
  2673. {
  2674. _OnSearchComplete((HDPA)lParam, (BOOL)wParam);
  2675. return 0;
  2676. }
  2677. // Pass mouse wheel messages to the drop-down if it is visible
  2678. else if ((uMsg == WM_MOUSEWHEEL || uMsg == g_msgMSWheel) &&
  2679. m_hwndDropDown && IsWindowVisible(m_hwndDropDown))
  2680. {
  2681. SendMessage(m_hwndList, uMsg, wParam, lParam);
  2682. return 0;
  2683. }
  2684. break;
  2685. }
  2686. return DefSubclassProc(m_hwndEdit, uMsg, wParam, lParam);
  2687. }
  2688. //+-------------------------------------------------------------------------
  2689. // Static window procedure for the subclassed edit box
  2690. //--------------------------------------------------------------------------
  2691. LRESULT CALLBACK CAutoComplete::s_EditWndProc
  2692. (
  2693. HWND hwnd,
  2694. UINT uMsg,
  2695. WPARAM wParam,
  2696. LPARAM lParam,
  2697. UINT_PTR uIdSubclass, // always zero for us
  2698. DWORD_PTR dwRefData // -> CAutoComplete
  2699. )
  2700. {
  2701. CAutoComplete* pac = (CAutoComplete*)dwRefData;
  2702. if (pac)
  2703. {
  2704. ASSERT(pac->m_hwndEdit == hwnd);
  2705. return pac->_EditWndProc(uMsg, wParam, lParam);
  2706. }
  2707. else
  2708. {
  2709. TraceMsg(TF_WARNING, "CAutoComplete::s_EditWndProc() --- pac == NULL");
  2710. return DefWindowProcWrap(hwnd, uMsg, wParam, lParam);
  2711. }
  2712. }
  2713. //+-------------------------------------------------------------------------
  2714. // Draws the sizing grip. We do this ourselves rather than call
  2715. // DrawFrameControl because the standard API does not flip upside down on
  2716. // all platforms. (NT and win98 seem to use a font and thus ignore the map
  2717. // mode)
  2718. //--------------------------------------------------------------------------
  2719. BOOL DrawGrip(register HDC hdc, LPRECT lprc, BOOL fEraseBackground)
  2720. {
  2721. int x, y;
  2722. int xMax, yMax;
  2723. int dMin;
  2724. HBRUSH hbrOld;
  2725. HPEN hpen, hpenOld;
  2726. DWORD rgbHilight, rgbShadow;
  2727. //
  2728. // The grip is really a pattern of 4 repeating diagonal lines:
  2729. // One glare
  2730. // Two raised
  2731. // One empty
  2732. // These lines run from bottom left to top right, in the bottom right
  2733. // corner of the square given by (lprc->left, lprc->top, dMin by dMin.
  2734. //
  2735. dMin = min(lprc->right-lprc->left, lprc->bottom-lprc->top);
  2736. xMax = lprc->left + dMin;
  2737. yMax = lprc->top + dMin;
  2738. //
  2739. // Setup colors
  2740. //
  2741. hbrOld = GetSysColorBrush(COLOR_3DFACE);
  2742. rgbHilight = GetSysColor(COLOR_3DHILIGHT);
  2743. rgbShadow = GetSysColor(COLOR_3DSHADOW);
  2744. //
  2745. // Fill in background of ENTIRE rect
  2746. //
  2747. if (fEraseBackground)
  2748. {
  2749. hbrOld = SelectBrush(hdc, hbrOld);
  2750. PatBlt(hdc, lprc->left, lprc->top, lprc->right-lprc->left,
  2751. lprc->bottom-lprc->top, PATCOPY);
  2752. SelectBrush(hdc, hbrOld);
  2753. }
  2754. else
  2755. {
  2756. /*
  2757. hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_WINDOW));
  2758. if (hpen == NULL)
  2759. return FALSE;
  2760. hpenOld = SelectPen(hdc, hpen);
  2761. x = lprc->left - 1;
  2762. y = lprc->top - 1;
  2763. MoveToEx(hdc, x, yMax, NULL);
  2764. LineTo(hdc, xMax, y);
  2765. SelectPen(hdc, hpenOld);
  2766. DeletePen(hpen);
  2767. */
  2768. //
  2769. // Draw background color directly under grip:
  2770. //
  2771. hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DFACE));
  2772. if (hpen == NULL)
  2773. return FALSE;
  2774. hpenOld = SelectPen(hdc, hpen);
  2775. x = lprc->left + 3;
  2776. y = lprc->top + 3;
  2777. while (x < xMax)
  2778. {
  2779. //
  2780. // Since dMin is the same horz and vert, x < xMax and y < yMax
  2781. // are interchangeable...
  2782. //
  2783. MoveToEx(hdc, x, yMax, NULL);
  2784. LineTo(hdc, xMax, y);
  2785. // Skip 3 lines in between
  2786. x += 4;
  2787. y += 4;
  2788. }
  2789. SelectPen(hdc, hpenOld);
  2790. DeletePen(hpen);
  2791. }
  2792. //
  2793. // Draw glare with COLOR_3DHILIGHT:
  2794. // Create proper pen
  2795. // Select into hdc
  2796. // Starting at lprc->left, draw a diagonal line then skip the
  2797. // next 3
  2798. // Select out of hdc
  2799. //
  2800. hpen = CreatePen(PS_SOLID, 1, rgbHilight);
  2801. if (hpen == NULL)
  2802. return FALSE;
  2803. hpenOld = SelectPen(hdc, hpen);
  2804. x = lprc->left;
  2805. y = lprc->top;
  2806. while (x < xMax)
  2807. {
  2808. //
  2809. // Since dMin is the same horz and vert, x < xMax and y < yMax
  2810. // are interchangeable...
  2811. //
  2812. MoveToEx(hdc, x, yMax, NULL);
  2813. LineTo(hdc, xMax, y);
  2814. // Skip 3 lines in between
  2815. x += 4;
  2816. y += 4;
  2817. }
  2818. SelectPen(hdc, hpenOld);
  2819. DeletePen(hpen);
  2820. //
  2821. // Draw raised part with COLOR_3DSHADOW:
  2822. // Create proper pen
  2823. // Select into hdc
  2824. // Starting at lprc->left+1, draw 2 diagonal lines, then skip
  2825. // the next 2
  2826. // Select outof hdc
  2827. //
  2828. hpen = CreatePen(PS_SOLID, 1, rgbShadow);
  2829. if (hpen == NULL)
  2830. return FALSE;
  2831. hpenOld = SelectPen(hdc, hpen);
  2832. x = lprc->left+1;
  2833. y = lprc->top+1;
  2834. while (x < xMax)
  2835. {
  2836. //
  2837. // Draw two diagonal lines touching each other.
  2838. //
  2839. MoveToEx(hdc, x, yMax, NULL);
  2840. LineTo(hdc, xMax, y);
  2841. x++;
  2842. y++;
  2843. MoveToEx(hdc, x, yMax, NULL);
  2844. LineTo(hdc, xMax, y);
  2845. //
  2846. // Skip 2 lines inbetween
  2847. //
  2848. x += 3;
  2849. y += 3;
  2850. }
  2851. SelectPen(hdc, hpenOld);
  2852. DeletePen(hpen);
  2853. return TRUE;
  2854. }
  2855. //+-------------------------------------------------------------------------
  2856. // Update the visible characteristics of the gripper depending on whether
  2857. // the dropdown is "dropped up" or the scrollbar is visible
  2858. //--------------------------------------------------------------------------
  2859. void CAutoComplete::_UpdateGrip()
  2860. {
  2861. if (m_hwndGrip)
  2862. {
  2863. //
  2864. // If we have a scrollbar the gripper has a rectangular shape.
  2865. //
  2866. if (m_hwndScroll && IsWindowVisible(m_hwndScroll))
  2867. {
  2868. SetWindowRgn(m_hwndGrip, NULL, FALSE);
  2869. }
  2870. //
  2871. // Otherwise, give it a trinagular window region
  2872. //
  2873. else
  2874. {
  2875. int nWidth = GetSystemMetrics(SM_CXVSCROLL);
  2876. int nHeight = GetSystemMetrics(SM_CYHSCROLL);
  2877. POINT rgpt[3] =
  2878. {
  2879. {nWidth, 0},
  2880. {nWidth, nHeight},
  2881. {0, nHeight},
  2882. };
  2883. //
  2884. // If dropped up, convert the "bottom-Right" tringle into
  2885. // a "top-right" triangle
  2886. //
  2887. if (m_fDroppedUp)
  2888. {
  2889. rgpt[2].y = 0;
  2890. }
  2891. HRGN hrgn = CreatePolygonRgn(rgpt, ARRAYSIZE(rgpt), WINDING);
  2892. if (hrgn && !SetWindowRgn(m_hwndGrip, hrgn, TRUE))
  2893. DeleteObject(hrgn);
  2894. }
  2895. InvalidateRect(m_hwndGrip, NULL, TRUE);
  2896. }
  2897. }
  2898. //+-------------------------------------------------------------------------
  2899. // Transfer the listview scroll info into our scrollbar control
  2900. //--------------------------------------------------------------------------
  2901. void CAutoComplete::_UpdateScrollbar()
  2902. {
  2903. if (m_hwndScroll)
  2904. {
  2905. SCROLLINFO si;
  2906. si.cbSize = sizeof(si);
  2907. si.fMask = SIF_ALL;
  2908. BOOL fScrollVisible = IsWindowVisible(m_hwndScroll);
  2909. if (GetScrollInfo(m_hwndList, SB_VERT, &si))
  2910. {
  2911. SetScrollInfo(m_hwndScroll, SB_CTL, &si, TRUE);
  2912. UINT nRange = si.nMax - si.nMin;
  2913. BOOL fShow = (nRange != 0) && (nRange != (UINT)(si.nPage - 1));
  2914. ShowScrollBar(m_hwndScroll, SB_CTL, fShow);
  2915. if (BOOLIFY(fScrollVisible) ^ BOOLIFY(fShow))
  2916. {
  2917. _UpdateGrip();
  2918. }
  2919. }
  2920. }
  2921. }
  2922. //+-------------------------------------------------------------------------
  2923. // Window procedure for the AutoSuggest drop-down
  2924. //--------------------------------------------------------------------------
  2925. LRESULT CAutoComplete::_DropDownWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  2926. {
  2927. switch (uMsg)
  2928. {
  2929. case WM_NCCREATE:
  2930. {
  2931. //
  2932. // Add a listview to the dropdown
  2933. //
  2934. m_hwndList = CreateWindowEx(0,
  2935. WC_LISTVIEW,
  2936. c_szAutoSuggestTitle,
  2937. WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | LVS_REPORT | LVS_NOCOLUMNHEADER | LVS_SINGLESEL | LVS_OWNERDATA | LVS_OWNERDRAWFIXED,
  2938. 0, 0, 30000, 30000,
  2939. m_hwndDropDown, NULL, HINST_THISDLL, NULL);
  2940. if (m_hwndList)
  2941. {
  2942. SetWindowTheme(m_hwndList, L"AutoComplete", NULL);
  2943. // Subclass the listview window
  2944. if (SetProp(m_hwndList, c_szAutoCompleteProp, this))
  2945. {
  2946. // point it to our wndproc and save the old one
  2947. m_pOldListViewWndProc = (WNDPROC)SetWindowLongPtr(m_hwndList, GWLP_WNDPROC, (LONG_PTR) &s_ListViewWndProc);
  2948. }
  2949. ListView_SetExtendedListViewStyle(m_hwndList, LVS_EX_FULLROWSELECT | LVS_EX_ONECLICKACTIVATE | LVS_EX_TRACKSELECT);
  2950. LV_COLUMN lvColumn;
  2951. lvColumn.mask = LVCF_FMT | LVCF_WIDTH;
  2952. lvColumn.fmt = LVCFMT_LEFT;
  2953. lvColumn.cx = LISTVIEW_COLUMN_WIDTH;
  2954. ListView_InsertColumn(m_hwndList, 0, &lvColumn);
  2955. // We'll get the default dimensions when we first show it
  2956. m_nDropWidth = 0;
  2957. m_nDropHeight = 0;
  2958. // Add a scrollbar
  2959. m_hwndScroll = CreateWindowEx(0, WC_SCROLLBAR, NULL,
  2960. WS_CHILD | SBS_VERT | SBS_RIGHTALIGN,
  2961. 0, 0, 20, 100, m_hwndDropDown, 0, HINST_THISDLL, NULL);
  2962. SetWindowTheme(m_hwndScroll, L"AutoComplete", NULL);
  2963. // Add a sizebox
  2964. m_hwndGrip = CreateWindowEx(0, WC_SCROLLBAR, NULL,
  2965. WS_CHILD | WS_VISIBLE | SBS_SIZEBOX | SBS_SIZEBOXBOTTOMRIGHTALIGN,
  2966. 0, 0, 20, 100, m_hwndDropDown, 0, HINST_THISDLL, NULL);
  2967. if (m_hwndGrip)
  2968. {
  2969. SetWindowSubclass(m_hwndGrip, s_GripperWndProc, 0, (ULONG_PTR)this);
  2970. _UpdateGrip();
  2971. SetWindowTheme(m_hwndGrip, L"AutoComplete", NULL);
  2972. }
  2973. }
  2974. return (m_hwndList != NULL);
  2975. }
  2976. case WM_DESTROY:
  2977. {
  2978. //
  2979. // I'm paranoid - should happen when we're hidden
  2980. //
  2981. if (s_hwndDropDown != NULL && s_hwndDropDown == m_hwndDropDown)
  2982. {
  2983. // Should never happen, but we take extra care not to leak a window hook!
  2984. ASSERT(FALSE);
  2985. ENTERCRITICAL;
  2986. if (s_hhookMouse)
  2987. {
  2988. UnhookWindowsHookEx(s_hhookMouse);
  2989. s_hhookMouse = NULL;
  2990. }
  2991. LEAVECRITICAL;
  2992. s_hwndDropDown = NULL;
  2993. }
  2994. _UnSubClassParent(m_hwndEdit);
  2995. // Unsubclass this window
  2996. SetWindowLongPtr(m_hwndDropDown, GWLP_USERDATA, (LONG_PTR)NULL);
  2997. HWND hwnd = m_hwndDropDown;
  2998. m_hwndDropDown = NULL;
  2999. if (m_hwndList)
  3000. {
  3001. DestroyWindow(m_hwndList);
  3002. m_hwndList = NULL;
  3003. }
  3004. if (m_hwndScroll)
  3005. {
  3006. DestroyWindow(m_hwndScroll);
  3007. m_hwndScroll = NULL;
  3008. }
  3009. if (m_hwndGrip)
  3010. {
  3011. DestroyWindow(m_hwndGrip);
  3012. m_hwndGrip = NULL;
  3013. }
  3014. // The dropdown incremented the autocomplete object's ref count.
  3015. Release();
  3016. return DefWindowProcWrap(hwnd, uMsg, wParam, lParam);
  3017. }
  3018. case WM_SYSCOLORCHANGE:
  3019. SendMessage(m_hwndList, uMsg, wParam, lParam);
  3020. break;
  3021. case WM_WININICHANGE:
  3022. SendMessage(m_hwndList, uMsg, wParam, lParam);
  3023. if (wParam == SPI_SETNONCLIENTMETRICS)
  3024. {
  3025. _UpdateGrip();
  3026. }
  3027. break;
  3028. case WM_GETMINMAXINFO:
  3029. {
  3030. //
  3031. // Don't shrink smaller than the size of the gripper
  3032. //
  3033. LPMINMAXINFO pMmi = (LPMINMAXINFO)lParam;
  3034. pMmi->ptMinTrackSize.x = GetSystemMetrics(SM_CXVSCROLL);
  3035. pMmi->ptMinTrackSize.y = GetSystemMetrics(SM_CYHSCROLL);
  3036. return 0;
  3037. }
  3038. case WM_MOVE:
  3039. {
  3040. //
  3041. // Reposition the list view in case we switch between dropping-down
  3042. // and dropping up.
  3043. //
  3044. RECT rc;
  3045. GetClientRect(m_hwndDropDown, &rc);
  3046. int nWidth = RECTWIDTH(rc);
  3047. int nHeight = RECTHEIGHT(rc);
  3048. int cxGrip = GetSystemMetrics(SM_CXVSCROLL);
  3049. int cyGrip = GetSystemMetrics(SM_CYHSCROLL);
  3050. if (m_fDroppedUp)
  3051. {
  3052. SetWindowPos(m_hwndGrip, HWND_TOP, nWidth - cxGrip, 0, cxGrip, cyGrip, SWP_NOACTIVATE);
  3053. SetWindowPos(m_hwndScroll, HWND_TOP, nWidth - cxGrip, cyGrip, cxGrip, nHeight-cyGrip, SWP_NOACTIVATE);
  3054. }
  3055. else
  3056. {
  3057. SetWindowPos(m_hwndGrip, HWND_TOP, nWidth - cxGrip, nHeight - cyGrip, cxGrip, cyGrip, SWP_NOACTIVATE);
  3058. SetWindowPos(m_hwndScroll, HWND_TOP, nWidth - cxGrip, 0, cxGrip, nHeight-cyGrip, SWP_NOACTIVATE);
  3059. }
  3060. break;
  3061. }
  3062. case WM_SIZE:
  3063. {
  3064. int nWidth = LOWORD(lParam);
  3065. int nHeight = HIWORD(lParam);
  3066. int cxGrip = GetSystemMetrics(SM_CXVSCROLL);
  3067. int cyGrip = GetSystemMetrics(SM_CYHSCROLL);
  3068. if (m_fDroppedUp)
  3069. {
  3070. SetWindowPos(m_hwndGrip, HWND_TOP, nWidth - cxGrip, 0, cxGrip, cyGrip, SWP_NOACTIVATE);
  3071. SetWindowPos(m_hwndScroll, HWND_TOP, nWidth - cxGrip, cyGrip, cxGrip, nHeight-cyGrip, SWP_NOACTIVATE);
  3072. }
  3073. else
  3074. {
  3075. SetWindowPos(m_hwndGrip, HWND_TOP, nWidth - cxGrip, nHeight - cyGrip, cxGrip, cyGrip, SWP_NOACTIVATE);
  3076. SetWindowPos(m_hwndScroll, HWND_TOP, nWidth - cxGrip, 0, cxGrip, nHeight-cyGrip, SWP_NOACTIVATE);
  3077. }
  3078. // Save the new dimensions
  3079. m_nDropWidth = nWidth + 2*GetSystemMetrics(SM_CXBORDER);
  3080. m_nDropHeight = nHeight + 2*GetSystemMetrics(SM_CYBORDER);
  3081. MoveWindow(m_hwndList, 0, 0, LISTVIEW_COLUMN_WIDTH + 10*cxGrip, nHeight, TRUE);
  3082. _UpdateScrollbar();
  3083. InvalidateRect(m_hwndList, NULL, FALSE);
  3084. break;
  3085. }
  3086. case WM_NCHITTEST:
  3087. {
  3088. RECT rc;
  3089. POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
  3090. // If the in the grip, show the sizing cursor
  3091. if (m_hwndGrip)
  3092. {
  3093. GetWindowRect(m_hwndGrip, &rc);
  3094. if (PtInRect(&rc, pt))
  3095. {
  3096. if(IS_WINDOW_RTL_MIRRORED(m_hwndDropDown))
  3097. {
  3098. return (m_fDroppedUp) ? HTTOPLEFT : HTBOTTOMLEFT;
  3099. }
  3100. else
  3101. {
  3102. return (m_fDroppedUp) ? HTTOPRIGHT : HTBOTTOMRIGHT;
  3103. }
  3104. }
  3105. }
  3106. }
  3107. break;
  3108. case WM_SHOWWINDOW:
  3109. {
  3110. s_fNoActivate = FALSE;
  3111. BOOL fShow = (BOOL)wParam;
  3112. if (!fShow)
  3113. {
  3114. //
  3115. // We are being hidden so we no longer need to
  3116. // subclass the parent windows.
  3117. //
  3118. _UnSubClassParent(m_hwndEdit);
  3119. //
  3120. // Remove the mouse hook. We shouldn't need to protect this global with
  3121. // a critical section because another dropdown cannot be shown
  3122. // before we are hidden. But we don't want to chance orphaning a hook
  3123. // so to be safe we protect write access to this variable.
  3124. //
  3125. ENTERCRITICAL;
  3126. if (s_hhookMouse)
  3127. {
  3128. UnhookWindowsHookEx(s_hhookMouse);
  3129. s_hhookMouse = NULL;
  3130. }
  3131. LEAVECRITICAL;
  3132. s_hwndDropDown = NULL;
  3133. // Deselect the current selection
  3134. int iCurSel = ListView_GetNextItem(m_hwndList, -1, LVNI_SELECTED);
  3135. if (iCurSel)
  3136. {
  3137. ListView_SetItemState(m_hwndList, iCurSel, 0, 0x000f);
  3138. }
  3139. }
  3140. }
  3141. break;
  3142. case WM_MOUSEACTIVATE:
  3143. //
  3144. // We don't want mouse clicks to activate us and
  3145. // take focus from the edit box.
  3146. //
  3147. return (LRESULT)MA_NOACTIVATE;
  3148. case WM_NCLBUTTONDOWN:
  3149. //
  3150. // We don't want resizing to activate us and deactivate the app.
  3151. // The WM_MOUSEACTIVATE message above prevents mouse downs from
  3152. // activating us, but mouse up after a resize still activates us.
  3153. //
  3154. if (wParam == HTBOTTOMRIGHT ||
  3155. wParam == HTTOPRIGHT)
  3156. {
  3157. s_fNoActivate = TRUE;
  3158. }
  3159. break;
  3160. case WM_VSCROLL:
  3161. {
  3162. ASSERT(m_hwndScroll);
  3163. //
  3164. // Pass the scroll messages from our control to the listview
  3165. //
  3166. WORD nScrollCode = LOWORD(wParam);
  3167. if (nScrollCode == SB_THUMBTRACK || nScrollCode == SB_THUMBPOSITION)
  3168. {
  3169. //
  3170. // The listview ignores the 16-bit position passed in and
  3171. // queries the internal window scrollbar for the tracking
  3172. // position. Since this returns the wrong track position,
  3173. // we have to handle thumb tracking ourselves.
  3174. //
  3175. WORD nPos = HIWORD(wParam);
  3176. SCROLLINFO si;
  3177. si.cbSize = sizeof(si);
  3178. si.fMask = SIF_ALL;
  3179. if (GetScrollInfo(m_hwndScroll, SB_CTL, &si))
  3180. {
  3181. //
  3182. // The track position is always at the top of the list.
  3183. // So, if we are scrolling up, make sure that the track
  3184. // position is visible. Otherwise we need to ensure
  3185. // that a full page is visible below the track positon.
  3186. //
  3187. int nEnsureVisible = si.nTrackPos;
  3188. if (si.nTrackPos > si.nPos)
  3189. {
  3190. nEnsureVisible += si.nPage - 1;
  3191. }
  3192. SendMessage(m_hwndList, LVM_ENSUREVISIBLE, nEnsureVisible, FALSE);
  3193. }
  3194. }
  3195. else
  3196. {
  3197. // Let listview handle it
  3198. SendMessage(m_hwndList, uMsg, wParam, lParam);
  3199. }
  3200. _UpdateScrollbar();
  3201. return 0;
  3202. }
  3203. case WM_EXITSIZEMOVE:
  3204. //
  3205. // Resize operation is over so permit the app to lose acitvation
  3206. //
  3207. s_fNoActivate = FALSE;
  3208. m_fDropDownResized = TRUE;
  3209. return 0;
  3210. case WM_DRAWITEM:
  3211. _DropDownDrawItem((LPDRAWITEMSTRUCT)lParam);
  3212. break;
  3213. case WM_NOTIFY:
  3214. if (_DropDownNotify((LPNMHDR)lParam))
  3215. {
  3216. return TRUE;
  3217. }
  3218. break;
  3219. case AM_UPDATESCROLLPOS:
  3220. {
  3221. if (m_hwndScroll)
  3222. {
  3223. int nTop = ListView_GetTopIndex(m_hwndList);
  3224. SetScrollPos(m_hwndScroll, SB_CTL, nTop, TRUE);
  3225. }
  3226. return 0;
  3227. }
  3228. case AM_BUTTONCLICK:
  3229. {
  3230. //
  3231. // This message is sent by the thread hook when a mouse click is detected outside
  3232. // the drop-down window. Unless the click occurred inside the combobox, we will
  3233. // hide the dropdown.
  3234. //
  3235. MOUSEHOOKSTRUCT *pmhs = (MOUSEHOOKSTRUCT*)lParam;
  3236. HWND hwnd = pmhs->hwnd;
  3237. RECT rc;
  3238. if (hwnd != m_hwndCombo && hwnd != m_hwndEdit &&
  3239. // See if we clicked within the bounds of the editbox. This is
  3240. // necessary for intelliforms.
  3241. // FEATURE: This assumes that the editbox is entirely visible!
  3242. GetWindowRect(m_hwndEdit, &rc) && !PtInRect(&rc, pmhs->pt))
  3243. {
  3244. _HideDropDown();
  3245. }
  3246. return 0;
  3247. }
  3248. case AM_DESTROY:
  3249. {
  3250. //
  3251. // We post this message to destroy the dropdown to avoid a strange crash
  3252. // that happens if we call DestroyWindow when the edit window is destroyed.
  3253. // The crash happens when a parent of the endit window is on another thread
  3254. // and that parent is destroyed in the middle of us creating a listview
  3255. // child of the dropdown.
  3256. //
  3257. DestroyWindow(m_hwndDropDown);
  3258. return 0;
  3259. }
  3260. }
  3261. return DefWindowProcWrap(m_hwndDropDown, uMsg, wParam, lParam);
  3262. }
  3263. void CAutoComplete::_DropDownDrawItem(LPDRAWITEMSTRUCT pdis)
  3264. {
  3265. //
  3266. // We need to draw the contents of the list view ourselves
  3267. // so that we can show items in the selected state even
  3268. // when the edit control has focus.
  3269. //
  3270. if (pdis->itemID != -1)
  3271. {
  3272. HDC hdc = pdis->hDC;
  3273. RECT rc = pdis->rcItem;
  3274. BOOL fTextHighlight = pdis->itemState & ODS_SELECTED;
  3275. // Setup the dc before we use it.
  3276. BOOL fRTLReading = GetWindowLong(pdis->hwndItem, GWL_EXSTYLE) & WS_EX_RTLREADING;
  3277. UINT uiOldTextAlign;
  3278. if (fRTLReading)
  3279. {
  3280. uiOldTextAlign = GetTextAlign(hdc);
  3281. SetTextAlign(hdc, uiOldTextAlign | TA_RTLREADING);
  3282. }
  3283. if (m_hfontListView)
  3284. {
  3285. SelectObject(hdc, m_hfontListView);
  3286. }
  3287. SetBkColor(hdc, GetSysColor(fTextHighlight ?
  3288. COLOR_HIGHLIGHT : COLOR_WINDOW));
  3289. SetTextColor(hdc, GetSysColor(fTextHighlight ?
  3290. COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT));
  3291. // Center the string vertically within rc
  3292. SIZE sizeText;
  3293. WCHAR szText[MAX_URL_STRING];
  3294. _GetItem(pdis->itemID, szText, ARRAYSIZE(szText), TRUE);
  3295. int cch = lstrlen(szText);
  3296. GetTextExtentPoint(hdc, szText, cch, &sizeText);
  3297. int yMid = (rc.top + rc.bottom) / 2;
  3298. int yString = yMid - (sizeText.cy/2);
  3299. int xString = 5;
  3300. //
  3301. // If this is a .url string, don't display the extension
  3302. //
  3303. if (cch > 4 && StrCmpNI(szText + cch - 4, L".url", 4) == 0)
  3304. {
  3305. cch -= 4;
  3306. }
  3307. ExtTextOut(hdc, xString, yString, ETO_OPAQUE | ETO_CLIPPED, &rc, szText, cch, NULL);
  3308. // Restore the text align in the dc.
  3309. if (fRTLReading)
  3310. {
  3311. SetTextAlign(hdc, uiOldTextAlign);
  3312. }
  3313. }
  3314. }
  3315. BOOL CAutoComplete::_DropDownNotify(LPNMHDR pnmhdr)
  3316. {
  3317. WCHAR szBuf[MAX_URL_STRING]; // Just one instance of the buffer
  3318. //
  3319. // Respond to notification messages from the list view
  3320. //
  3321. switch (pnmhdr->code)
  3322. {
  3323. case LVN_GETDISPINFO:
  3324. {
  3325. //
  3326. // Return the text for an autosuggest item
  3327. //
  3328. ASSERT(pnmhdr->hwndFrom == m_hwndList);
  3329. LV_DISPINFO* pdi = (LV_DISPINFO*)pnmhdr;
  3330. if (pdi->item.mask & LVIF_TEXT)
  3331. {
  3332. _GetItem(pdi->item.iItem, pdi->item.pszText, pdi->item.cchTextMax, TRUE);
  3333. }
  3334. break;
  3335. }
  3336. case LVN_GETDISPINFOA:
  3337. {
  3338. //
  3339. // For win9x support (added for automated testing)
  3340. //
  3341. ASSERT(pnmhdr->hwndFrom == m_hwndList);
  3342. LV_DISPINFOA* pdi = (LV_DISPINFOA*)pnmhdr;
  3343. if (pdi->item.mask & LVIF_TEXT)
  3344. {
  3345. _GetItem(pdi->item.iItem, szBuf, ARRAYSIZE(szBuf), TRUE);
  3346. SHUnicodeToAnsi(szBuf, pdi->item.pszText, pdi->item.cchTextMax);
  3347. }
  3348. break;
  3349. }
  3350. case LVN_ITEMCHANGED:
  3351. {
  3352. //
  3353. // When an item is selected in the list view, we transfer it to the
  3354. // edit control. But only if the selection was not caused by the
  3355. // mouse passing over an element (hot tracking).
  3356. //
  3357. if (!m_fInHotTracking)
  3358. {
  3359. LPNMLISTVIEW pnmv = (LPNMLISTVIEW)pnmhdr;
  3360. if ((pnmv->uChanged & LVIF_STATE) && (pnmv->uNewState & (LVIS_FOCUSED | LVIS_SELECTED)))
  3361. {
  3362. _GetItem(pnmv->iItem, szBuf, ARRAYSIZE(szBuf), FALSE);
  3363. // Copy the selection to the edit box and place caret at the end
  3364. _SetEditText(szBuf);
  3365. int cch = lstrlen(szBuf);
  3366. Edit_SetSel(m_hwndEdit, cch, cch);
  3367. }
  3368. }
  3369. //
  3370. // Update the scrollbar. Note that we have to post a message to do this
  3371. // after returning from this function. Otherwise we get old info
  3372. // from the listview about the scroll positon.
  3373. //
  3374. PostMessage(m_hwndDropDown, AM_UPDATESCROLLPOS, 0, 0);
  3375. break;
  3376. }
  3377. case LVN_ITEMACTIVATE:
  3378. {
  3379. //
  3380. // Someone activated an item in the listview. We want to make sure that
  3381. // the items is selected (without hot tracking) so that the contents
  3382. // are moved to the edit box, and then simulate and enter key press.
  3383. //
  3384. LPNMITEMACTIVATE lpnmia = (LPNMITEMACTIVATE)pnmhdr;
  3385. _GetItem(lpnmia->iItem, szBuf, ARRAYSIZE(szBuf), FALSE);
  3386. // Copy the selection to the edit box and place caret at the end
  3387. _SetEditText(szBuf);
  3388. int cch = lstrlen(szBuf);
  3389. Edit_SetSel(m_hwndEdit, cch, cch);
  3390. //
  3391. // Intelliforms don't want an enter key because this would submit the
  3392. // form, so first we try sending a notification.
  3393. //
  3394. if (SendMessage(m_hwndEdit, m_uMsgItemActivate, 0, (LPARAM)szBuf) == 0)
  3395. {
  3396. // Not an intelliform, so simulate an enter key instead.
  3397. SendMessage(m_hwndEdit, WM_KEYDOWN, VK_RETURN, 0);
  3398. SendMessage(m_hwndEdit, WM_KEYUP, VK_RETURN, 0);
  3399. }
  3400. _HideDropDown();
  3401. break;
  3402. }
  3403. case LVN_HOTTRACK:
  3404. {
  3405. //
  3406. // Select items as we mouse-over them
  3407. //
  3408. LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)pnmhdr;
  3409. LVHITTESTINFO lvh;
  3410. lvh.pt = lpnmlv->ptAction;
  3411. int iItem = ListView_HitTest(m_hwndList, &lvh);
  3412. if (iItem != -1)
  3413. {
  3414. // Update the current selection. The m_fInHotTracking flag prevents the
  3415. // edit box contents from being updated
  3416. m_fInHotTracking = TRUE;
  3417. ListView_SetItemState(m_hwndList, iItem, LVIS_SELECTED|LVIS_FOCUSED, 0x000f);
  3418. SendMessage(m_hwndList, LVM_ENSUREVISIBLE, iItem, FALSE);
  3419. m_fInHotTracking = FALSE;
  3420. }
  3421. // We processed this...
  3422. return TRUE;
  3423. }
  3424. }
  3425. return FALSE;
  3426. }
  3427. //+-------------------------------------------------------------------------
  3428. // Static window procedure for the AutoSuggest drop-down
  3429. //--------------------------------------------------------------------------
  3430. LRESULT CALLBACK CAutoComplete::s_DropDownWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  3431. {
  3432. CAutoComplete* pThis;
  3433. if (uMsg == WM_NCCREATE)
  3434. {
  3435. pThis = (CAutoComplete*)((LPCREATESTRUCT)lParam)->lpCreateParams;
  3436. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pThis);
  3437. pThis->m_hwndDropDown = hwnd;
  3438. }
  3439. else
  3440. {
  3441. pThis = (CAutoComplete*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  3442. }
  3443. if (pThis && (pThis->m_hwndDropDown == hwnd))
  3444. {
  3445. return pThis->_DropDownWndProc(uMsg, wParam, lParam);
  3446. }
  3447. else
  3448. {
  3449. return DefWindowProcWrap(hwnd, uMsg, wParam, lParam);
  3450. }
  3451. }
  3452. //+-------------------------------------------------------------------------
  3453. // We subclass the listview to prevent it from activating the drop-down
  3454. // when someone clicks on it.
  3455. //--------------------------------------------------------------------------
  3456. LRESULT CAutoComplete::_ListViewWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  3457. {
  3458. LRESULT lRet;
  3459. switch (uMsg)
  3460. {
  3461. case WM_LBUTTONDOWN:
  3462. case WM_RBUTTONDOWN:
  3463. case WM_MBUTTONDOWN:
  3464. case WM_LBUTTONUP:
  3465. case WM_RBUTTONUP:
  3466. case WM_MBUTTONUP:
  3467. //
  3468. // Prevent mouse clicks from activating this view
  3469. //
  3470. s_fNoActivate = TRUE;
  3471. lRet = CallWindowProc(m_pOldListViewWndProc, m_hwndList, uMsg, wParam, lParam);
  3472. s_fNoActivate = FALSE;
  3473. return 0;
  3474. case WM_DESTROY:
  3475. // Restore old wndproc.
  3476. RemoveProp(m_hwndList, c_szAutoCompleteProp);
  3477. if (m_pOldListViewWndProc)
  3478. {
  3479. SetWindowLongPtr(m_hwndList, GWLP_WNDPROC, (LONG_PTR) m_pOldListViewWndProc);
  3480. lRet = CallWindowProc(m_pOldListViewWndProc, m_hwndList, uMsg, wParam, lParam);
  3481. m_pOldListViewWndProc = NULL;
  3482. }
  3483. return 0;
  3484. case WM_GETOBJECT:
  3485. if ((DWORD)lParam == OBJID_CLIENT)
  3486. {
  3487. SAFERELEASE(m_pDelegateAccObj);
  3488. if (SUCCEEDED(CreateStdAccessibleObject(m_hwndList,
  3489. OBJID_CLIENT,
  3490. IID_IAccessible,
  3491. (void **)&m_pDelegateAccObj)))
  3492. {
  3493. return LresultFromObject(IID_IAccessible, wParam, SAFECAST(this, IAccessible *));
  3494. }
  3495. }
  3496. break;
  3497. case WM_NCHITTEST:
  3498. {
  3499. RECT rc;
  3500. POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
  3501. // If in the grip area, let our parent handle it
  3502. if (m_hwndGrip)
  3503. {
  3504. GetWindowRect(m_hwndGrip, &rc);
  3505. if (PtInRect(&rc, pt))
  3506. {
  3507. return HTTRANSPARENT;
  3508. }
  3509. }
  3510. break;
  3511. }
  3512. }
  3513. lRet = CallWindowProc(m_pOldListViewWndProc, m_hwndList, uMsg, wParam, lParam);
  3514. return lRet;
  3515. }
  3516. //+-------------------------------------------------------------------------
  3517. // Static window procedure for the subclassed listview
  3518. //--------------------------------------------------------------------------
  3519. LRESULT CAutoComplete::s_ListViewWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  3520. {
  3521. CAutoComplete* pac = (CAutoComplete*)GetProp(hwnd, c_szAutoCompleteProp);
  3522. if (pac)
  3523. {
  3524. return pac->_ListViewWndProc(uMsg, wParam, lParam);
  3525. }
  3526. else
  3527. {
  3528. TraceMsg(TF_WARNING, "CAutoComplete::s_ListViewWndProc() --- pac == NULL");
  3529. return DefWindowProcWrap(hwnd, uMsg, wParam, lParam);
  3530. }
  3531. }
  3532. //+-------------------------------------------------------------------------
  3533. // This message hook is only installed when the AutoSuggest dropdown
  3534. // is visible. It hides the dropdown if you click on any window other than
  3535. // the dropdown or associated editbox/combobox.
  3536. //--------------------------------------------------------------------------
  3537. LRESULT CALLBACK CAutoComplete::s_MouseHook(int nCode, WPARAM wParam, LPARAM lParam)
  3538. {
  3539. if (nCode >= 0)
  3540. {
  3541. MOUSEHOOKSTRUCT *pmhs = (MOUSEHOOKSTRUCT*)lParam;
  3542. ASSERT(pmhs);
  3543. switch (wParam)
  3544. {
  3545. case WM_LBUTTONDOWN:
  3546. case WM_MBUTTONDOWN:
  3547. case WM_RBUTTONDOWN:
  3548. case WM_NCLBUTTONDOWN:
  3549. case WM_NCMBUTTONDOWN:
  3550. case WM_NCRBUTTONDOWN:
  3551. {
  3552. HWND hwnd = pmhs->hwnd;
  3553. // If the click was outside the edit/combobox/dropdown, then
  3554. // hide the dropdown.
  3555. if (hwnd != s_hwndDropDown)
  3556. {
  3557. // Ignore if the button was clicked in the dropdown
  3558. RECT rc;
  3559. if (GetWindowRect(s_hwndDropDown, &rc) && !PtInRect(&rc, pmhs->pt))
  3560. {
  3561. // Inform the dropdown
  3562. SendMessage(s_hwndDropDown, AM_BUTTONCLICK, 0, (LPARAM)pmhs);
  3563. }
  3564. }
  3565. break;
  3566. }
  3567. }
  3568. }
  3569. return CallNextHookEx(s_hhookMouse, nCode, wParam, lParam);
  3570. }
  3571. //+-------------------------------------------------------------------------
  3572. // Subclasses all of the parents of hwnd so we can determine when they
  3573. // are moved, deactivated, or clicked on. We use these events to signal
  3574. // the window that has focus to hide its autocomplete dropdown. This is
  3575. // similar to the CB_SHOWDROPDOWN message sent to comboboxes, but we cannot
  3576. // assume that we are autocompleting a combobox.
  3577. //--------------------------------------------------------------------------
  3578. void CAutoComplete::_SubClassParent
  3579. (
  3580. HWND hwnd // window to notify of events
  3581. )
  3582. {
  3583. //
  3584. // Subclass all the parent windows because any of them could cause
  3585. // the position of hwnd to change which should hide the dropdown.
  3586. //
  3587. HWND hwndParent = hwnd;
  3588. DWORD dwThread = GetCurrentThreadId();
  3589. while (hwndParent = GetParent(hwndParent))
  3590. {
  3591. // Only subclass if this window is owned by our thread
  3592. if (dwThread == GetWindowThreadProcessId(hwndParent, NULL))
  3593. {
  3594. SetWindowSubclass(hwndParent, s_ParentWndProc, 0, (ULONG_PTR)this);
  3595. }
  3596. }
  3597. }
  3598. //+-------------------------------------------------------------------------
  3599. // Unsubclasses all of the parents of hwnd. We use the helper functions in
  3600. // comctl32 to safely unsubclass a window even if someone else subclassed
  3601. // the window after us.
  3602. //--------------------------------------------------------------------------
  3603. void CAutoComplete::_UnSubClassParent
  3604. (
  3605. HWND hwnd // window to notify of events
  3606. )
  3607. {
  3608. HWND hwndParent = hwnd;
  3609. DWORD dwThread = GetCurrentThreadId();
  3610. while (hwndParent = GetParent(hwndParent))
  3611. {
  3612. // Only need to unsubclass if this window is owned by our thread
  3613. if (dwThread == GetWindowThreadProcessId(hwndParent, NULL))
  3614. {
  3615. RemoveWindowSubclass(hwndParent, s_ParentWndProc, 0);
  3616. }
  3617. }
  3618. }
  3619. //+-------------------------------------------------------------------------
  3620. // Subclassed window procedure of the parents ot the editbox being
  3621. // autocompleted. This intecepts messages that should case the autocomplete
  3622. // dropdown to be hidden.
  3623. //--------------------------------------------------------------------------
  3624. LRESULT CALLBACK CAutoComplete::s_ParentWndProc
  3625. (
  3626. HWND hwnd,
  3627. UINT uMsg,
  3628. WPARAM wParam,
  3629. LPARAM lParam,
  3630. UINT_PTR uIdSubclass, // always zero for us
  3631. DWORD_PTR dwRefData // -> CParentWindow
  3632. )
  3633. {
  3634. CAutoComplete* pThis = (CAutoComplete*)dwRefData;
  3635. if (!pThis)
  3636. return DefSubclassProc(hwnd, uMsg, wParam, lParam);
  3637. switch (uMsg)
  3638. {
  3639. case WM_WINDOWPOSCHANGED:
  3640. {
  3641. //
  3642. // Check the elapsed time since this was last called. We want to avoid an infinite loop
  3643. // with another window that also wants to be on top.
  3644. //
  3645. static DWORD s_dwTicks = 0;
  3646. DWORD dwTicks = GetTickCount();
  3647. DWORD dwEllapsed = dwTicks - s_dwTicks;
  3648. s_dwTicks = dwTicks;
  3649. if (dwEllapsed > 100)
  3650. {
  3651. // Make sure our dropdown stays on top
  3652. LPWINDOWPOS pwp = (LPWINDOWPOS)lParam;
  3653. if (IsFlagClear(pwp->flags, SWP_NOZORDER) && IsWindowVisible(pThis->m_hwndDropDown))
  3654. {
  3655. SetWindowPos(pThis->m_hwndDropDown, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  3656. }
  3657. }
  3658. break;
  3659. }
  3660. case WM_ACTIVATE:
  3661. {
  3662. // Ignore if we are not being deactivated
  3663. WORD fActive = LOWORD(wParam);
  3664. if (fActive != WA_INACTIVE)
  3665. {
  3666. break;
  3667. }
  3668. // Drop through
  3669. }
  3670. case WM_MOVING:
  3671. case WM_SIZE:
  3672. case WM_LBUTTONDOWN:
  3673. case WM_RBUTTONDOWN:
  3674. case WM_MBUTTONDOWN:
  3675. pThis->_HideDropDown();
  3676. break;
  3677. case WM_NCACTIVATE:
  3678. //
  3679. // While clicking on the autosuggest dropdown, we
  3680. // want to prevent the dropdown from being activated.
  3681. //
  3682. if (s_fNoActivate)
  3683. return FALSE;
  3684. break;
  3685. case WM_DESTROY:
  3686. RemoveWindowSubclass(hwnd, s_ParentWndProc, 0);
  3687. break;
  3688. default:
  3689. break;
  3690. }
  3691. return DefSubclassProc(hwnd, uMsg, wParam, lParam);
  3692. }
  3693. //+-------------------------------------------------------------------------
  3694. // Subclassed window procedure fir the grip re-sizer control
  3695. //--------------------------------------------------------------------------
  3696. LRESULT CALLBACK CAutoComplete::s_GripperWndProc
  3697. (
  3698. HWND hwnd,
  3699. UINT uMsg,
  3700. WPARAM wParam,
  3701. LPARAM lParam,
  3702. UINT_PTR uIdSubclass, // always zero for us
  3703. DWORD_PTR dwRefData // -> CParentWindow
  3704. )
  3705. {
  3706. CAutoComplete* pThis = (CAutoComplete*)dwRefData;
  3707. if (!pThis)
  3708. return DefSubclassProc(hwnd, uMsg, wParam, lParam);
  3709. switch (uMsg)
  3710. {
  3711. case WM_NCHITTEST:
  3712. return HTTRANSPARENT;
  3713. case WM_PAINT:
  3714. PAINTSTRUCT ps;
  3715. BeginPaint(hwnd, &ps);
  3716. EndPaint(hwnd, &ps);
  3717. break;
  3718. case WM_ERASEBKGND:
  3719. {
  3720. HDC hdc = (HDC)wParam;
  3721. RECT rc;
  3722. GetClientRect(hwnd, &rc);
  3723. int nOldMapMode = 0;
  3724. BOOL fScrollVisible = pThis->m_hwndScroll && IsWindowVisible(pThis->m_hwndScroll);
  3725. //
  3726. // See if we need to vertically flip the grip
  3727. //
  3728. if (pThis->m_fDroppedUp)
  3729. {
  3730. nOldMapMode = SetMapMode(hdc, MM_ANISOTROPIC);
  3731. SetWindowOrgEx(hdc, 0, 0, NULL);
  3732. SetWindowExtEx(hdc, 1, 1, NULL);
  3733. SetViewportOrgEx(hdc, 0, GetSystemMetrics(SM_CYHSCROLL), NULL);
  3734. SetViewportExtEx(hdc, 1, -1, NULL);
  3735. }
  3736. // The standard DrawFrameControl API does not draw upside down on all platforms
  3737. // DrawFrameControl(hdc, &rc, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
  3738. DrawGrip(hdc, &rc, fScrollVisible);
  3739. if (nOldMapMode)
  3740. {
  3741. SetViewportOrgEx(hdc, 0, 0, NULL);
  3742. SetViewportExtEx(hdc, 1, 1, NULL);
  3743. SetMapMode(hdc, nOldMapMode);
  3744. }
  3745. return 1;
  3746. }
  3747. case WM_DESTROY:
  3748. RemoveWindowSubclass(hwnd, s_GripperWndProc, 0);
  3749. break;
  3750. default:
  3751. break;
  3752. }
  3753. return DefSubclassProc(hwnd, uMsg, wParam, lParam);
  3754. }