[SHELLFIND] Subscribe to folders that are already opened

This commit is contained in:
Brock Mammen 2019-08-25 17:03:44 -05:00 committed by Giannis Adamopoulos
parent 07d37180a1
commit e6a5821e0c
2 changed files with 38 additions and 24 deletions

View file

@ -46,24 +46,20 @@ LRESULT CSearchBar::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &
if (FAILED_UNEXPECTEDLY(hResult)) if (FAILED_UNEXPECTEDLY(hResult))
return hResult; return hResult;
CComPtr<IShellService> pShellService;
hResult = m_AddressEditBox->QueryInterface(IID_PPV_ARG(IShellService, &pShellService));
if (FAILED_UNEXPECTEDLY(hResult))
return hResult;
hResult = m_AddressEditBox->Init(hCombobox, hEditControl, 0, m_pSite); hResult = m_AddressEditBox->Init(hCombobox, hEditControl, 0, m_pSite);
if (FAILED_UNEXPECTEDLY(hResult)) if (FAILED_UNEXPECTEDLY(hResult))
return hResult; return hResult;
CComPtr<IDispatch> pDispatch; // Subscribe to navigation events
hResult = m_AddressEditBox->QueryInterface(IID_PPV_ARG(IDispatch, &pDispatch)); CComPtr<IShellBrowser> pShellBrowser;
if (FAILED_UNEXPECTEDLY(hResult)) hResult = IUnknown_QueryService(m_pSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &pShellBrowser));
return hResult; DWORD dwAdviseCookie;
DISPPARAMS params = {0}; if (SUCCEEDED(hResult))
hResult = pDispatch->Invoke(DISPID_NAVIGATECOMPLETE2, GUID_NULL, 0, DISPATCH_METHOD, &params, NULL, NULL, NULL); AtlAdvise(pShellBrowser, static_cast<IDispatch *>(this), DIID_DWebBrowserEvents, &dwAdviseCookie);
hResult = pShellService->SetOwner(NULL); // Invoke the navigate event in case a search results folder is already open
if (FAILED_UNEXPECTEDLY(hResult)) DISPPARAMS params = {0};
return hResult; Invoke(DISPID_NAVIGATECOMPLETE2, GUID_NULL, 0, DISPATCH_METHOD, &params, NULL, NULL, NULL);
return 0; return 0;
} }
@ -188,14 +184,7 @@ LRESULT CSearchBar::OnSearchButtonClicked(WORD wNotifyCode, WORD wID, HWND hWndC
if (FAILED_UNEXPECTEDLY(hr)) if (FAILED_UNEXPECTEDLY(hr))
return hr; return hr;
CComPtr<IShellFolder> pShellFolder; hr = GetSearchResultsFolder(&pShellBrowser, &hwnd, NULL);
hr = GetSearchResultsFolder(*pShellBrowser, &hwnd, &pShellFolder);
if (FAILED_UNEXPECTEDLY(hr))
return hr;
// Subscribe to search events
DWORD fAdviseCookie;
hr = AtlAdvise(pShellFolder, static_cast<IDispatch *>(this), DIID_DSearchCommandEvents, &fAdviseCookie);
if (FAILED_UNEXPECTEDLY(hr)) if (FAILED_UNEXPECTEDLY(hr))
return hr; return hr;
} }
@ -232,8 +221,9 @@ BOOL CSearchBar::GetAddressEditBoxPath(WCHAR (&szPath)[MAX_PATH])
item.cchTextMax = _countof(szItemText); item.cchTextMax = _countof(szItemText);
SendMessageW(hComboboxEx, CBEM_GETITEMW, 0, (LPARAM)&item); SendMessageW(hComboboxEx, CBEM_GETITEMW, 0, (LPARAM)&item);
if (!wcscmp(szItemText, szPath) && SHGetPathFromIDListW((LPCITEMIDLIST)item.lParam, szPath)) if (!wcscmp(szItemText, szPath) && SHGetPathFromIDListW((LPCITEMIDLIST)item.lParam, szItemText))
{ {
StringCbCopyW(szPath, MAX_PATH * sizeof(WCHAR), szItemText);
return TRUE; return TRUE;
} }
} }
@ -588,12 +578,34 @@ void CSearchBar::SetSearchInProgress(BOOL bInProgress)
::PostMessage(hProgressBar, PBM_SETMARQUEE, bInProgress, 0); ::PostMessage(hProgressBar, PBM_SETMARQUEE, bInProgress, 0);
} }
HRESULT CSearchBar::TrySubscribeToSearchEvents()
{
CComPtr<IShellFolder> pShellFolder;
HRESULT hr = GetSearchResultsFolder(NULL, NULL, &pShellFolder);
if (FAILED(hr))
return hr;
DWORD fAdviseCookie;
hr = AtlAdvise(pShellFolder, static_cast<IDispatch *>(this), DIID_DSearchCommandEvents, &fAdviseCookie);
if (FAILED_UNEXPECTEDLY(hr))
return hr;
return S_OK;
}
HRESULT STDMETHODCALLTYPE CSearchBar::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) HRESULT STDMETHODCALLTYPE CSearchBar::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{ {
if (dispIdMember == DISPID_SEARCHCOMPLETE || dispIdMember == DISPID_SEARCHABORT) switch (dispIdMember)
{ {
case DISPID_NAVIGATECOMPLETE2:
case DISPID_DOCUMENTCOMPLETE:
TrySubscribeToSearchEvents();
return S_OK;
case DISPID_SEARCHCOMPLETE:
case DISPID_SEARCHABORT:
SetSearchInProgress(FALSE); SetSearchInProgress(FALSE);
return S_OK; return S_OK;
default:
return E_INVALIDARG;
} }
return E_INVALIDARG;
} }

View file

@ -35,6 +35,7 @@ private:
BOOL GetAddressEditBoxPath(WCHAR (&szPath)[MAX_PATH]); BOOL GetAddressEditBoxPath(WCHAR (&szPath)[MAX_PATH]);
void SetSearchInProgress(BOOL bInProgress); void SetSearchInProgress(BOOL bInProgress);
HRESULT ExecuteCommand(CComPtr<IContextMenu>& menu, UINT nCmd); HRESULT ExecuteCommand(CComPtr<IContextMenu>& menu, UINT nCmd);
HRESULT TrySubscribeToSearchEvents();
// *** ATL event handlers *** // *** ATL event handlers ***
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled); LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled);
@ -113,6 +114,7 @@ public:
BEGIN_COM_MAP(CSearchBar) BEGIN_COM_MAP(CSearchBar)
COM_INTERFACE_ENTRY_IID(IID_IDispatch, IDispatch) COM_INTERFACE_ENTRY_IID(IID_IDispatch, IDispatch)
COM_INTERFACE_ENTRY_IID(DIID_DWebBrowserEvents, IDispatch)
COM_INTERFACE_ENTRY_IID(DIID_DSearchCommandEvents, IDispatch) COM_INTERFACE_ENTRY_IID(DIID_DSearchCommandEvents, IDispatch)
COM_INTERFACE_ENTRY_IID(IID_IWinEventHandler, IWinEventHandler) COM_INTERFACE_ENTRY_IID(IID_IWinEventHandler, IWinEventHandler)
COM_INTERFACE_ENTRY_IID(IID_IBandNavigate, IBandNavigate) COM_INTERFACE_ENTRY_IID(IID_IBandNavigate, IBandNavigate)