|
| 1 | +/* |
| 2 | + * PROJECT: ReactOS Picture and Fax Viewer |
| 3 | + * LICENSE: GPL-2.0 (https://spdx.org/licenses/GPL-2.0) |
| 4 | + * PURPOSE: Utility routines |
| 5 | + * COPYRIGHT: Copyright 2025 Whindmar Saksit <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include "shimgvw.h" |
| 9 | +#include <windowsx.h> |
| 10 | +#include <shlobj.h> |
| 11 | +#include <shellapi.h> |
| 12 | +#include <shellutils.h> |
| 13 | +#include <shlwapi_undoc.h> |
| 14 | + |
| 15 | +IContextMenu *g_pContextMenu = NULL; |
| 16 | + |
| 17 | +static int |
| 18 | +GetMenuItemIdByPos(HMENU hMenu, UINT Pos) |
| 19 | +{ |
| 20 | + MENUITEMINFOW mii; |
| 21 | + mii.cbSize = FIELD_OFFSET(MENUITEMINFOW, hbmpItem); /* USER32 version agnostic */ |
| 22 | + mii.fMask = MIIM_ID; |
| 23 | + mii.cch = 0; |
| 24 | + return GetMenuItemInfoW(hMenu, Pos, TRUE, &mii) ? mii.wID : -1; |
| 25 | +} |
| 26 | + |
| 27 | +static BOOL |
| 28 | +IsMenuSeparator(HMENU hMenu, UINT Pos) |
| 29 | +{ |
| 30 | + MENUITEMINFOW mii; |
| 31 | + mii.cbSize = FIELD_OFFSET(MENUITEMINFOW, hbmpItem); /* USER32 version agnostic */ |
| 32 | + mii.fMask = MIIM_FTYPE; |
| 33 | + mii.cch = 0; |
| 34 | + return GetMenuItemInfoW(hMenu, Pos, TRUE, &mii) && (mii.fType & MFT_SEPARATOR); |
| 35 | +} |
| 36 | + |
| 37 | +static BOOL |
| 38 | +IsSelfShellVerb(PCWSTR Assoc, PCWSTR Verb) |
| 39 | +{ |
| 40 | + WCHAR buf[MAX_PATH * 3]; |
| 41 | + DWORD cch = _countof(buf); |
| 42 | + HRESULT hr = AssocQueryStringW(ASSOCF_NOTRUNCATE, ASSOCSTR_COMMAND, Assoc, Verb, buf, &cch); |
| 43 | + return hr == S_OK && *Assoc == L'.' && StrStrW(buf, L",ImageView_Fullscreen"); |
| 44 | +} |
| 45 | + |
| 46 | +static void |
| 47 | +ModifyShellContextMenu(IContextMenu *pCM, HMENU hMenu, UINT CmdIdFirst, PCWSTR Assoc) |
| 48 | +{ |
| 49 | + HRESULT hr; |
| 50 | + UINT id, i; |
| 51 | + for (i = 0; i < GetMenuItemCount(hMenu); ++i) |
| 52 | + { |
| 53 | + WCHAR buf[200]; |
| 54 | + id = GetMenuItemIdByPos(hMenu, i); |
| 55 | + if (id == (UINT)-1) |
| 56 | + continue; |
| 57 | + |
| 58 | + *buf = UNICODE_NULL; |
| 59 | + /* Note: We just ask for the wide string because all the items we care about come from shell32 and it handles both */ |
| 60 | + hr = IContextMenu_GetCommandString(pCM, id - CmdIdFirst, GCS_VERBW, NULL, (char*)buf, _countof(buf)); |
| 61 | + if (SUCCEEDED(hr)) |
| 62 | + { |
| 63 | + UINT remove = FALSE; |
| 64 | + if (IsSelfShellVerb(Assoc, buf)) |
| 65 | + ++remove; |
| 66 | + else if (!lstrcmpiW(L"cut", buf) || !lstrcmpiW(L"copy", buf) || !lstrcmpiW(L"link", buf)) |
| 67 | + ++remove; |
| 68 | + |
| 69 | + if (remove && DeleteMenu(hMenu, i, MF_BYPOSITION)) |
| 70 | + { |
| 71 | + if (i-- > 0) |
| 72 | + { |
| 73 | + if (IsMenuSeparator(hMenu, i) && IsMenuSeparator(hMenu, i + 1)) |
| 74 | + DeleteMenu(hMenu, i, MF_BYPOSITION); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + while (IsMenuSeparator(hMenu, 0) && DeleteMenu(hMenu, 0, MF_BYPOSITION)) {} |
| 81 | +} |
| 82 | + |
| 83 | +static LRESULT CALLBACK |
| 84 | +ShellContextMenuWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 85 | +{ |
| 86 | + LRESULT lRes = 0; |
| 87 | + if (FAILED(SHForwardContextMenuMsg((IUnknown*)g_pContextMenu, uMsg, wParam, lParam, &lRes, TRUE))) |
| 88 | + lRes = DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 89 | + return lRes; |
| 90 | +} |
| 91 | + |
| 92 | +static void |
| 93 | +DoShellContextMenu(HWND hwnd, IContextMenu *pCM, PCWSTR File, LPARAM lParam) |
| 94 | +{ |
| 95 | + enum { first = 1, last = 0x7fff }; |
| 96 | + HRESULT hr; |
| 97 | + HMENU hMenu = CreatePopupMenu(); |
| 98 | + UINT cmf = GetKeyState(VK_SHIFT) < 0 ? CMF_EXTENDEDVERBS : 0; |
| 99 | + |
| 100 | + POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; |
| 101 | + if ((int)lParam == -1) |
| 102 | + { |
| 103 | + RECT rect; |
| 104 | + GetWindowRect(hwnd, &rect); |
| 105 | + pt.x = (rect.left + rect.right) / 2; |
| 106 | + pt.y = rect.top; |
| 107 | + } |
| 108 | + |
| 109 | + g_pContextMenu = pCM; |
| 110 | + hwnd = SHCreateWorkerWindowW(ShellContextMenuWindowProc, hwnd, 0, WS_VISIBLE | WS_CHILD, NULL, 0); |
| 111 | + if (!hwnd) |
| 112 | + goto die; |
| 113 | + hr = IContextMenu_QueryContextMenu(pCM, hMenu, 0, first, last, cmf | CMF_NODEFAULT); |
| 114 | + if (SUCCEEDED(hr)) |
| 115 | + { |
| 116 | + UINT id; |
| 117 | + ModifyShellContextMenu(pCM, hMenu, first, PathFindExtensionW(File)); |
| 118 | + id = TrackPopupMenuEx(hMenu, TPM_RETURNCMD, pt.x, pt.y, hwnd, NULL); |
| 119 | + if (id) |
| 120 | + { |
| 121 | + UINT flags = (GetKeyState(VK_SHIFT) < 0 ? CMIC_MASK_SHIFT_DOWN : 0) | |
| 122 | + (GetKeyState(VK_CONTROL) < 0 ? CMIC_MASK_CONTROL_DOWN : 0); |
| 123 | + CMINVOKECOMMANDINFO ici = { sizeof(ici), flags, hwnd, MAKEINTRESOURCEA(id - first) }; |
| 124 | + ici.nShow = SW_SHOW; |
| 125 | + hr = IContextMenu_InvokeCommand(pCM, &ici); |
| 126 | + } |
| 127 | + } |
| 128 | + DestroyWindow(hwnd); |
| 129 | +die: |
| 130 | + DestroyMenu(hMenu); |
| 131 | + g_pContextMenu = NULL; |
| 132 | +} |
| 133 | + |
| 134 | +void |
| 135 | +DoShellContextMenuOnFile(HWND hwnd, PCWSTR File, LPARAM lParam) |
| 136 | +{ |
| 137 | + HRESULT hr; |
| 138 | + IShellFolder *pSF; |
| 139 | + PCUITEMID_CHILD pidlItem; |
| 140 | + PIDLIST_ABSOLUTE pidl = ILCreateFromPath(File); |
| 141 | + if (pidl && SUCCEEDED(SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &pSF), &pidlItem))) |
| 142 | + { |
| 143 | + IContextMenu *pCM; |
| 144 | + hr = IShellFolder_GetUIObjectOf(pSF, hwnd, 1, &pidlItem, &IID_IContextMenu, NULL, (void**)&pCM); |
| 145 | + if (SUCCEEDED(hr)) |
| 146 | + { |
| 147 | + DoShellContextMenu(hwnd, pCM, File, lParam); |
| 148 | + IContextMenu_Release(pCM); |
| 149 | + } |
| 150 | + IShellFolder_Release(pSF); |
| 151 | + } |
| 152 | + SHFree(pidl); |
| 153 | +} |
| 154 | + |
| 155 | +void DisplayHelp(HWND hwnd) |
| 156 | +{ |
| 157 | + SHELL_ErrorBox(hwnd, ERROR_NOT_SUPPORTED); |
| 158 | +} |
0 commit comments