[SHELLFIND] Set status bar text after opening search results folder
authorBrock Mammen <brockmammen@gmail.com>
Sat, 20 Jul 2019 18:05:03 +0000 (13:05 -0500)
committerGiannis Adamopoulos <gadamopoulos@reactos.org>
Sun, 15 Sep 2019 16:46:36 +0000 (19:46 +0300)
dll/win32/browseui/shellfind/CSearchBar.cpp
dll/win32/browseui/shellfind/CSearchBar.h

index 8035216..7efcd2b 100644 (file)
@@ -112,48 +112,100 @@ LRESULT CSearchBar::OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bH
     return TRUE;
 }
 
-LRESULT CSearchBar::OnSearchButtonClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+HRESULT CSearchBar::GetSearchResultsFolder(IShellBrowser **ppShellBrowser, HWND *pHwnd, IShellFolder **ppShellFolder)
 {
+    HRESULT hr;
     CComPtr<IShellBrowser> pShellBrowser;
-    HRESULT hr = IUnknown_QueryService(pSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &pShellBrowser));
-    if (FAILED_UNEXPECTEDLY(hr))
+    if (!ppShellBrowser)
+        ppShellBrowser = &pShellBrowser;
+    if (!*ppShellBrowser)
+    {
+        hr = IUnknown_QueryService(m_pSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, ppShellBrowser));
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
+
+    CComPtr<IShellView> pShellView;
+    hr = (*ppShellBrowser)->QueryActiveShellView(&pShellView);
+    if (FAILED(hr) || !pShellView)
         return hr;
 
-    WCHAR szShellGuid[MAX_PATH];
-    const WCHAR shellGuidPrefix[] = L"shell:::";
-    memcpy(szShellGuid, shellGuidPrefix, sizeof(shellGuidPrefix));
-    hr = StringFromGUID2(CLSID_FindFolder, szShellGuid + _countof(shellGuidPrefix) - 1, _countof(szShellGuid) - _countof(shellGuidPrefix));
-    if (FAILED_UNEXPECTEDLY(hr))
+    CComPtr<IFolderView> pFolderView;
+    hr = pShellView->QueryInterface(IID_PPV_ARG(IFolderView, &pFolderView));
+    if (FAILED(hr) || !pFolderView)
         return hr;
 
-    LPITEMIDLIST findFolderPidl;
-    hr = SHParseDisplayName(szShellGuid, NULL, &findFolderPidl, 0, NULL);
-    if (FAILED_UNEXPECTEDLY(hr))
+    CComPtr<IShellFolder> pShellFolder;
+    if (!ppShellFolder)
+        ppShellFolder = &pShellFolder;
+    hr = pFolderView->GetFolder(IID_PPV_ARG(IShellFolder, ppShellFolder));
+    if (FAILED(hr) || !pShellFolder)
         return hr;
 
-    return pShellBrowser->BrowseObject(findFolderPidl, 0);
+    CLSID clsid;
+    hr = IUnknown_GetClassID(*ppShellFolder, &clsid);
+    if (FAILED(hr))
+        return hr;
+    if (clsid != CLSID_FindFolder)
+        return E_FAIL;
+
+    if (pHwnd)
+    {
+        hr = pShellView->GetWindow(pHwnd);
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
+
+    return S_OK;
 }
 
-LRESULT CSearchBar::OnClicked(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+LRESULT CSearchBar::OnSearchButtonClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
 {
-    HRESULT hr;
     CComPtr<IShellBrowser> pShellBrowser;
-    hr = IUnknown_QueryService(pSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &pShellBrowser));
-    if (FAILED_UNEXPECTEDLY(hr))
-        return hr;
-    CComPtr<IShellView> pShellView;
-    hr = pShellBrowser->QueryActiveShellView(&pShellView);
+    HRESULT hr = IUnknown_QueryService(pSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &pShellBrowser));
     if (FAILED_UNEXPECTEDLY(hr))
         return hr;
+
     HWND hwnd;
-    hr = pShellView->GetWindow(&hwnd);
-    if (FAILED_UNEXPECTEDLY(hr))
-        return hr;
+    if (FAILED(GetSearchResultsFolder(&pShellBrowser, &hwnd, NULL)))
+    {
+        WCHAR szShellGuid[MAX_PATH];
+        const WCHAR shellGuidPrefix[] = L"shell:::";
+        memcpy(szShellGuid, shellGuidPrefix, sizeof(shellGuidPrefix));
+        hr = StringFromGUID2(CLSID_FindFolder, szShellGuid + _countof(shellGuidPrefix) - 1,
+                             _countof(szShellGuid) - _countof(shellGuidPrefix));
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+
+        LPITEMIDLIST findFolderPidl;
+        hr = SHParseDisplayName(szShellGuid, NULL, &findFolderPidl, 0, NULL);
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+
+        hr = pShellBrowser->BrowseObject(findFolderPidl, 0);
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
 
-    LPWSTR path = (LPWSTR) L"C:\\readme.txt";
+    GetSearchResultsFolder(&pShellBrowser, &hwnd, NULL);
+    if (hwnd)
+        // TODO: Use message ID in header file
+        ::PostMessageW(hwnd, WM_USER + 1, 0, (LPARAM) StrDupW(L"Starting search..."));
+
+    return S_OK;
+}
+
+LRESULT CSearchBar::OnClicked(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    HWND hwnd;
+    HRESULT hr = GetSearchResultsFolder(NULL, &hwnd, NULL);
+    if (SUCCEEDED(hr))
+    {
+        LPCWSTR path = L"C:\\readme.txt";
+        // TODO: Use message ID in header file
+        ::PostMessageW(hwnd, WM_USER, 0, (LPARAM) StrDupW(path));
+    }
 
-    // TODO: Use message ID in header file
-    ::PostMessageW(hwnd, WM_USER, 0, (LPARAM) StrDupW(path));
     return 0;
 }
 
index 3bef420..fd84b0c 100644 (file)
@@ -43,6 +43,7 @@ private:
     BOOL bFocused;
 
     void InitializeSearchBar();
+    HRESULT GetSearchResultsFolder(IShellBrowser **ppShellBrowser, HWND *pHwnd, IShellFolder **ppShellFolder);
     HRESULT ExecuteCommand(CComPtr<IContextMenu>& menu, UINT nCmd);
 
     // *** ATL event handlers ***