OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/platform_util.h" |
| 6 |
| 7 #include <atlbase.h> |
| 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "chrome/common/win_util.h" |
| 13 |
| 14 namespace platform_util { |
| 15 |
| 16 void ShowItemInFolder(const FilePath& full_path) { |
| 17 FilePath dir = full_path.DirName(); |
| 18 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\". |
| 19 if (dir.value() == L"" || !file_util::EnsureEndsWithSeparator(&dir)) |
| 20 return; |
| 21 |
| 22 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)( |
| 23 PCIDLIST_ABSOLUTE pidl_Folder, |
| 24 UINT cidl, |
| 25 PCUITEMID_CHILD_ARRAY pidls, |
| 26 DWORD flags); |
| 27 |
| 28 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr = |
| 29 NULL; |
| 30 static bool initialize_open_folder_proc = true; |
| 31 if (initialize_open_folder_proc) { |
| 32 initialize_open_folder_proc = false; |
| 33 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6 |
| 34 // and does not exist in Win2K. We attempt to retrieve this function export |
| 35 // from shell32 and if it does not exist, we just invoke ShellExecute to |
| 36 // open the folder thus losing the functionality to select the item in |
| 37 // the process. |
| 38 HMODULE shell32_base = GetModuleHandle(L"shell32.dll"); |
| 39 if (!shell32_base) { |
| 40 NOTREACHED(); |
| 41 return; |
| 42 } |
| 43 open_folder_and_select_itemsPtr = |
| 44 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr> |
| 45 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems")); |
| 46 } |
| 47 if (!open_folder_and_select_itemsPtr) { |
| 48 ShellExecute(NULL, _T("open"), dir.value().c_str(), NULL, NULL, SW_SHOW); |
| 49 return; |
| 50 } |
| 51 |
| 52 CComPtr<IShellFolder> desktop; |
| 53 HRESULT hr = SHGetDesktopFolder(&desktop); |
| 54 if (FAILED(hr)) |
| 55 return; |
| 56 |
| 57 win_util::CoMemReleaser<ITEMIDLIST> dir_item; |
| 58 hr = desktop->ParseDisplayName(NULL, NULL, |
| 59 const_cast<wchar_t *>(dir.value().c_str()), |
| 60 NULL, &dir_item, NULL); |
| 61 if (FAILED(hr)) |
| 62 return; |
| 63 |
| 64 win_util::CoMemReleaser<ITEMIDLIST> file_item; |
| 65 hr = desktop->ParseDisplayName(NULL, NULL, |
| 66 const_cast<wchar_t *>(full_path.value().c_str()), |
| 67 NULL, &file_item, NULL); |
| 68 if (FAILED(hr)) |
| 69 return; |
| 70 |
| 71 const ITEMIDLIST* highlight[] = { |
| 72 {file_item}, |
| 73 }; |
| 74 (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight), |
| 75 highlight, NULL); |
| 76 } |
| 77 |
| 78 } // namespace platform_util |
OLD | NEW |