Skip to content

Commit

Permalink
Add "Show in Explorer" and "Open in Explorer" context menu for metapath.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 3, 2018
1 parent 3bf1fe0 commit 7e373b2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
73 changes: 73 additions & 0 deletions metapath/src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,79 @@ BOOL PathCreateLnk(LPCWSTR pszLnkDir, LPCWSTR pszPath) {
return bSucceeded;
}

void OpenContainingFolder(HWND hwnd, LPCWSTR pszFile, BOOL bSelect) {
WCHAR wchDirectory[MAX_PATH];
lstrcpyn(wchDirectory, pszFile, COUNTOF(wchDirectory));

LPCWSTR path = NULL;
DWORD dwAttributes = GetFileAttributes(pszFile);
if (bSelect || dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
PathRemoveFileSpec(wchDirectory);
}
if (bSelect && dwAttributes != INVALID_FILE_ATTRIBUTES) {
// if pszFile is root, open the volume instead of open My Computer and select the volume
if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) && PathIsRoot(pszFile)) {
bSelect = FALSE;
} else {
path = pszFile;
}
}

dwAttributes = GetFileAttributes(wchDirectory);
if (dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
return;
}

LPITEMIDLIST pidl = ILCreateFromPath(wchDirectory);
if (pidl) {
HRESULT hr;
LPITEMIDLIST pidlEntry = path ? ILCreateFromPath(path) : NULL;
if (pidlEntry) {
hr = SHOpenFolderAndSelectItems(pidl, 1, (LPCITEMIDLIST *)(&pidlEntry), 0);
ILFree(pidlEntry);
} else if (!bSelect) {
#if 0
// Use an invalid item to open the folder?
hr = SHOpenFolderAndSelectItems(pidl, 1, (LPCITEMIDLIST *)(&pidl), 0);
#else
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));

sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_IDLIST;
sei.hwnd = hwnd;
//sei.lpVerb = L"explore";
sei.lpVerb = L"open";
sei.lpIDList = pidl;
sei.nShow = SW_SHOW;

const BOOL result = ShellExecuteEx(&sei);
hr = result ? S_OK : S_FALSE;
#endif
} else {
// open parent folder and select the folder
hr = SHOpenFolderAndSelectItems(pidl, 0, NULL, 0);
}
ILFree(pidl);
if (hr == S_OK) {
return;
}
}

if (path == NULL) {
path = wchDirectory;
}

// open a new explorer window every time
LPWSTR szParameters = (LPWSTR)NP2HeapAlloc((lstrlen(path) + 64) * sizeof(WCHAR));
lstrcpy(szParameters, bSelect ? L"/select," : L"");
lstrcat(szParameters, L"\"");
lstrcat(szParameters, path);
lstrcat(szParameters, L"\"");
ShellExecute(hwnd, L"open", L"explorer", szParameters, NULL, SW_SHOW);
NP2HeapFree(szParameters);
}

//=============================================================================
//
// ExtractFirstArgument()
Expand Down
2 changes: 1 addition & 1 deletion metapath/src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ BOOL PathIsLnkFile(LPCWSTR pszPath);
BOOL PathGetLnkPath(LPCWSTR pszLnkFile, LPWSTR pszResPath, int cchResPath);
BOOL PathIsLnkToDirectory(LPCWSTR pszPath, LPWSTR pszResPath, int cchResPath);
BOOL PathCreateLnk(LPCWSTR pszLnkDir, LPCWSTR pszPath);

void OpenContainingFolder(HWND hwnd, LPCWSTR pszFile, BOOL bSelect);

NP2_inline void TrimString(LPWSTR lpString) {
StrTrim(lpString, L" ");
Expand Down
12 changes: 12 additions & 0 deletions metapath/src/metapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,18 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
DriveBox_PropertyDlg(hwndDriveBox);
break;

case IDM_FILE_EXPLORER: {
if (ListView_GetSelectedCount(hwndDirList)) {
DLITEM dli;
dli.mask = DLI_FILENAME;
DirList_GetItem(hwndDirList, -1, &dli);
OpenContainingFolder(hwndMain, dli.szFileName, TRUE);
} else {
OpenContainingFolder(hwndMain, szCurDir, FALSE);
}
}
break;

case IDM_VIEW_NEWWINDOW: {
WCHAR szModuleName[MAX_PATH];
WCHAR szParameters[1024];
Expand Down
2 changes: 2 additions & 0 deletions metapath/src/metapath.rc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ BEGIN
MENUITEM "&Copy Name", IDM_POP_COPY_FILENAME
MENUITEM "&Copy Path Name", IDM_POP_COPY_PATHNAME
MENUITEM SEPARATOR
MENUITEM "Show in Explorer", IDM_FILE_EXPLORER
MENUITEM "Create Lin&k...", IDM_FILE_CREATELINK
MENUITEM "&Save As...", IDM_FILE_SAVEAS
MENUITEM "Copy/&Move...", IDM_FILE_COPYMOVE
Expand Down Expand Up @@ -116,6 +117,7 @@ BEGIN
MENUITEM "&Reverse", IDM_SORT_REVERSE
END
MENUITEM SEPARATOR
MENUITEM "Open in Explorer", IDM_FILE_EXPLORER
MENUITEM "&Goto Favorites", IDM_VIEW_FAVORITES
MENUITEM "&Edit Favorites", IDM_VIEW_EDITFAVORITES
MENUITEM "&Directory...", IDM_FILE_CHANGEDIR
Expand Down
1 change: 1 addition & 0 deletions metapath/src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
#define IDM_FILE_CHANGEDIR 40017
#define IDM_FILE_PROPERTIES 40018
#define IDM_FILE_DRIVEPROP 40019
#define IDM_FILE_EXPLORER 40020
#define IDM_VIEW_NEWWINDOW 40201
#define IDM_VIEW_FOLDERS 40202
#define IDM_VIEW_FILES 40203
Expand Down

0 comments on commit 7e373b2

Please sign in to comment.