ReactOS  0.4.15-dev-1150-g593bcce
CFontMenu.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Font Shell Extension
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: CFontMenu implementation
5  * COPYRIGHT: Copyright 2019,2020 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11 
12 static CLIPFORMAT g_cfHIDA;
13 
15 {
16  if (g_cfHIDA == NULL)
17  {
19  }
20 
21  FORMATETC fmt = { g_cfHIDA, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
22  STGMEDIUM medium;
23 
24  HRESULT hr = pDataObject->GetData(&fmt, &medium);
26  return hr;
27 
28  LPVOID lpSrc = GlobalLock(medium.hGlobal);
29  SIZE_T cbSize = GlobalSize(medium.hGlobal);
30 
31  *ppcida = (CIDA *)::CoTaskMemAlloc(cbSize);
32  if (*ppcida)
33  {
34  memcpy(*ppcida, lpSrc, cbSize);
35  hr = S_OK;
36  }
37  else
38  {
39  hr = E_FAIL;
40  }
41  ReleaseStgMedium(&medium);
42  return hr;
43 }
44 
45 const char* DFM_TO_STR(UINT uMsg)
46 {
47  switch(uMsg)
48  {
49  case DFM_MERGECONTEXTMENU: return "DFM_MERGECONTEXTMENU";
50  case DFM_INVOKECOMMAND: return "DFM_INVOKECOMMAND";
51  case DFM_MODIFYQCMFLAGS: return "DFM_MODIFYQCMFLAGS";
52  case DFM_MERGECONTEXTMENU_TOP: return "DFM_MERGECONTEXTMENU_TOP";
53  case DFM_MERGECONTEXTMENU_BOTTOM: return "DFM_MERGECONTEXTMENU_BOTTOM";
54  case DFM_GETHELPTEXTW: return "DFM_GETHELPTEXTW";
55  case DFM_GETVERBW: return "DFM_GETVERBW";
56  case DFM_GETVERBA: return "DFM_GETVERBA";
57  case DFM_WM_INITMENUPOPUP: return "DFM_WM_INITMENUPOPUP";
58  case DFM_INVOKECOMMANDEX: return "DFM_INVOKECOMMANDEX";
59  case DFM_GETDEFSTATICID: return "DFM_GETDEFSTATICID";
60  case 3: return "MENU_BEGIN";
61  case 4: return "MENU_END";
62  default: return "";
63  }
64 }
65 
66 
67 static void RunFontViewer(HWND hwnd, const FontPidlEntry* fontEntry)
68 {
69  WCHAR FontViewerPath[MAX_PATH] = L"%SystemRoot%\\System32\\fontview.exe";
70  WCHAR FontPathArg[MAX_PATH + 3];
71 
72  CStringW Path = g_FontCache->Filename(fontEntry, true);
73  if (!Path.IsEmpty())
74  {
75  // '/d' disables the install button
76  StringCchPrintfW(FontPathArg, _countof(FontPathArg), L"/d %s", Path.GetString());
77  PathQuoteSpacesW(FontPathArg + 3);
78 
79  SHELLEXECUTEINFOW si = { sizeof(si) };
81  si.hwnd = hwnd;
82  si.lpFile = FontViewerPath;
83  si.lpParameters = FontPathArg;
84  si.nShow = SW_SHOWNORMAL;
85  ShellExecuteExW(&si);
86  }
87 }
88 
91 {
92  TRACE("FontFolderMenuCallback(%u {%s})\n", uMsg, DFM_TO_STR(uMsg));
93  switch (uMsg)
94  {
96  {
97  QCMINFO *pqcminfo = (QCMINFO *)lParam;
98 
100  MENUITEMINFOW cmi = { sizeof(cmi) };
102  cmi.fType = MFT_STRING;
103  cmi.fState = MFS_DEFAULT;
104  cmi.wID = pqcminfo->idCmdFirst++;
105  cmi.dwTypeData = (LPWSTR)menuText.GetString();
106  InsertMenuItemW(pqcminfo->hmenu, pqcminfo->indexMenu, TRUE, &cmi);
107 
108  return S_OK;
109  }
110  case DFM_INVOKECOMMAND:
111  // Preview is the only item we can handle
112  if (wParam == 0)
113  {
114  CComHeapPtr<CIDA> cida;
115  HRESULT hr = _GetCidlFromDataObject(pdtobj, &cida);
116  if (FAILED_UNEXPECTEDLY(hr))
117  return hr;
118 
119  for (UINT n = 0; n < cida->cidl; ++n)
120  {
121  const FontPidlEntry* fontEntry = _FontFromIL(HIDA_GetPIDLItem(cida, n));
122  RunFontViewer(hwnd, fontEntry);
123  }
124  return S_OK;
125  }
126  return S_FALSE;
127 
128  case DFM_INVOKECOMMANDEX:
129  return E_NOTIMPL;
130  case DFM_GETDEFSTATICID: // Required for Windows 7 to pick a default
131  return S_FALSE;
132  }
133  return E_NOTIMPL;
134 }
135 
136 
138  IShellFolder *psf, REFIID riid, LPVOID* ppvOut)
139 {
140  if (cidl > 0)
141  {
142  HKEY keys[1] = {0};
143  int nkeys = 0;
144  CComPtr<IContextMenu> spMenu;
145 
146  // Use the default context menu handler, but augment it from the callbacks
147  HRESULT hr = CDefFolderMenu_Create2(NULL, hwnd, cidl, apidl, psf, FontFolderMenuCallback, nkeys, keys, &spMenu);
148 
149  if (FAILED_UNEXPECTEDLY(hr))
150  return hr;
151 
152  // See if the requested interface (e.g. IContextMenu3) is also available
153  return spMenu->QueryInterface(riid, ppvOut);
154  }
155 
156  // We can't create a background menu
157  return E_FAIL;
158 }
159 
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define MFT_STRING
Definition: winuser.h:741
#define REFIID
Definition: guiddef.h:118
#define CFSTR_SHELLIDLIST
Definition: shlobj.h:477
HRESULT hr
Definition: shlfolder.c:183
void WINAPI ReleaseStgMedium(STGMEDIUM *pmedium)
Definition: ole2.c:2033
#define TRUE
Definition: types.h:120
REFIID riid
Definition: precomp.h:44
UINT WINAPI RegisterClipboardFormatW(_In_ LPCWSTR)
#define CALLBACK
Definition: compat.h:35
static PCUIDLIST_RELATIVE HIDA_GetPIDLItem(CIDA const *pida, SIZE_T i)
Definition: shellutils.h:543
HMENU hmenu
Definition: shlobj.h:1315
GLdouble n
Definition: glext.h:7729
const FontPidlEntry * _FontFromIL(LPCITEMIDLIST pidl)
Definition: fontpidl.cpp:32
HRESULT GetData([in, unique] FORMATETC *pformatetcIn, [out] STGMEDIUM *pmedium)
HRESULT _GetCidlFromDataObject(IDataObject *pDataObject, CIDA **ppcida)
Definition: CFontMenu.cpp:14
LPWSTR dwTypeData
Definition: winuser.h:3244
WINE_DEFAULT_DEBUG_CHANNEL(fontext)
UINT_PTR WPARAM
Definition: windef.h:207
static CLIPFORMAT g_cfHIDA
Definition: CFontMenu.cpp:12
#define MFS_DEFAULT
Definition: winuser.h:743
#define DFM_MERGECONTEXTMENU_BOTTOM
Definition: shlobj.h:2421
#define E_FAIL
Definition: ddrawi.h:102
WPARAM wParam
Definition: combotst.c:138
#define DFM_MODIFYQCMFLAGS
Definition: shlobj.h:2422
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
const PCUITEMID_CHILD * PCUITEMID_CHILD_ARRAY
Definition: shtypes.idl:71
#define MIIM_STATE
Definition: winuser.h:716
#define DFM_GETVERBW
Definition: shlobj.h:2419
#define S_FALSE
Definition: winerror.h:2357
#define DFM_INVOKECOMMAND
Definition: precomp.h:45
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
HRESULT _CFontMenu_CreateInstance(HWND hwnd, UINT cidl, PCUITEMID_CHILD_ARRAY apidl, IShellFolder *psf, REFIID riid, LPVOID *ppvOut)
Definition: CFontMenu.cpp:137
UINT indexMenu
Definition: shlobj.h:1316
BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW(LPSHELLEXECUTEINFOW sei)
Definition: shlexec.cpp:2263
#define DFM_WM_INITMENUPOPUP
Definition: shlobj.h:2414
#define MIIM_ID
Definition: winuser.h:717
#define DFM_GETHELPTEXTW
Definition: shlobj.h:2417
static HRESULT CALLBACK FontFolderMenuCallback(IShellFolder *psf, HWND hwnd, IDataObject *pdtobj, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: CFontMenu.cpp:89
#define TRACE(s)
Definition: solgame.cpp:4
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define DFM_GETDEFSTATICID
Definition: precomp.h:47
LONG HRESULT
Definition: typedefs.h:79
UINT idCmdFirst
Definition: shlobj.h:1317
#define _countof(array)
Definition: sndvol32.h:68
#define FAILED_UNEXPECTEDLY(hr)
Definition: shellutils.h:71
#define MAX_PATH
Definition: compat.h:34
PCXSTR GetString() const
Definition: atlsimpstr.h:361
#define SEE_MASK_DOENVSUBST
Definition: shellapi.h:35
#define DFM_MERGECONTEXTMENU
Definition: precomp.h:44
UINT cidl
Definition: shlobj.h:499
const char * DFM_TO_STR(UINT uMsg)
Definition: CFontMenu.cpp:45
static const WCHAR L[]
Definition: oid.c:1250
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
CFontCache * g_FontCache
Definition: CFontCache.cpp:12
#define MIIM_STRING
Definition: winuser.h:722
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define S_OK
Definition: intsafe.h:51
#define SW_SHOWNORMAL
Definition: winuser.h:764
PRTL_UNICODE_STRING_BUFFER Path
static void RunFontViewer(HWND hwnd, const FontPidlEntry *fontEntry)
Definition: CFontMenu.cpp:67
#define IDS_FONT_PREVIEW
Definition: resource.h:13
LPCWSTR lpParameters
Definition: shellapi.h:331
CStringW Filename(const FontPidlEntry *fontEntry, bool alwaysFullPath=false)
Definition: CFontCache.cpp:96
STRSAFEAPI StringCchPrintfW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:530
#define E_NOTIMPL
Definition: ddrawi.h:99
#define DFM_MERGECONTEXTMENU_TOP
Definition: shlobj.h:2416
BOOL WINAPI InsertMenuItemW(_In_ HMENU, _In_ UINT, _In_ BOOL, _In_ LPCMENUITEMINFOW)
unsigned int UINT
Definition: ndis.h:50
#define DFM_INVOKECOMMANDEX
Definition: precomp.h:46
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
Definition: path.c:973
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define DFM_GETVERBA
Definition: shlobj.h:2420
LPARAM lParam
Definition: combotst.c:139
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: ifs.c:426
Definition: dsound.c:943
HRESULT WINAPI CDefFolderMenu_Create2(PCIDLIST_ABSOLUTE pidlFolder, HWND hwnd, UINT cidl, PCUITEMID_CHILD_ARRAY apidl, IShellFolder *psf, LPFNDFMCALLBACK lpfn, UINT nKeys, const HKEY *ahkeyClsKeys, IContextMenu **ppcm)
Definition: shlobj.h:498