[BROWSEUI]
[reactos.git] / base / shell / rshell / CStartMenu.cpp
1 /*
2 * ReactOS Explorer
3 *
4 * Copyright 2014 Giannis Adamopoulos
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "precomp.h"
21
22 #include "CMergedFolder.h"
23
24 // TODO: declare these GUIDs and interfaces in the right place (whatever that may be)
25 IID IID_IAugmentedShellFolder = { 0x91EA3F8C, 0xC99B, 0x11D0, { 0x98, 0x15, 0x00, 0xC0, 0x4F, 0xD9, 0x19, 0x72 } };
26 IID IID_IAugmentedShellFolder2 = { 0x8DB3B3F4, 0x6CFE, 0x11D1, { 0x8A, 0xE9, 0x00, 0xC0, 0x4F, 0xD9, 0x18, 0xD0 } };
27 CLSID CLSID_MergedFolder = { 0x26FDC864, 0xBE88, 0x46E7, { 0x92, 0x35, 0x03, 0x2D, 0x8E, 0xA5, 0x16, 0x2E } };
28
29 //#define TEST_TRACKPOPUPMENU_SUBMENUS
30
31
32 /* NOTE: The following constants *MUST NOT* be changed because
33 they're hardcoded and need to be the exact values
34 in order to get the start menu to work! */
35 #define IDM_RUN 401
36 #define IDM_LOGOFF 402
37 #define IDM_UNDOCKCOMPUTER 410
38 #define IDM_TASKBARANDSTARTMENU 413
39 #define IDM_LASTSTARTMENU_SEPARATOR 450
40 #define IDM_DOCUMENTS 501
41 #define IDM_HELPANDSUPPORT 503
42 #define IDM_PROGRAMS 504
43 #define IDM_CONTROLPANEL 505
44 #define IDM_SHUTDOWN 506
45 #define IDM_FAVORITES 507
46 #define IDM_SETTINGS 508
47 #define IDM_PRINTERSANDFAXES 510
48 #define IDM_SEARCH 520
49 #define IDM_SYNCHRONIZE 553
50 #define IDM_NETWORKCONNECTIONS 557
51 #define IDM_DISCONNECT 5000
52 #define IDM_SECURITY 5001
53
54 /*
55 * TODO:
56 * 1. append the start menu contents from all users
57 * 2. implement the context menu for start menu entries (programs, control panel, network connetions, printers)
58 * 3. filter out programs folder from the shell folder part of the start menu
59 * 4. showing the programs start menu is SLOW compared to windows. this needs some investigation
60 */
61
62 class CShellMenuCallback :
63 public CComObjectRootEx<CComMultiThreadModelNoCS>,
64 public IShellMenuCallback
65 {
66 private:
67
68 HWND m_hwndTray;
69 CComPtr<IShellMenu> m_pShellMenu;
70 CComPtr<IBandSite> m_pBandSite;
71 CComPtr<IDeskBar> m_pDeskBar;
72 CComPtr<ITrayPriv> m_pTrayPriv;
73 CComPtr<IShellFolder> m_psfPrograms;
74
75 LPITEMIDLIST m_pidlPrograms;
76
77 HRESULT OnInitMenu()
78 {
79 HMENU hmenu;
80 HRESULT hr;
81
82 if (m_pTrayPriv.p)
83 return S_OK;
84
85 hr = IUnknown_GetSite(m_pDeskBar, IID_PPV_ARG(ITrayPriv, &m_pTrayPriv));
86 hr = IUnknown_GetWindow(m_pTrayPriv, &m_hwndTray);
87 hr = m_pTrayPriv->AppendMenuW(&hmenu);
88 #ifndef TEST_TRACKPOPUPMENU_SUBMENUS
89 hr = m_pShellMenu->SetMenu(hmenu, NULL, SMSET_BOTTOM);
90 #else
91 hr = m_pShellMenu->SetMenu(hmenu, m_hwndTray, SMSET_BOTTOM);
92 #endif
93
94 return hr;
95 }
96
97 HRESULT OnGetInfo(LPSMDATA psmd, SMINFO *psminfo)
98 {
99 int iconIndex = 0;
100
101 switch (psmd->uId)
102 {
103 // Smaller "24x24" icons used for the start menu
104 // The bitmaps are still 32x32, but the image is centered
105 case IDM_FAVORITES: iconIndex = -322; break;
106 case IDM_SEARCH: iconIndex = -323; break;
107 case IDM_HELPANDSUPPORT: iconIndex = -324; break;
108 case IDM_LOGOFF: iconIndex = -325; break;
109 case IDM_PROGRAMS: iconIndex = -326; break;
110 case IDM_DOCUMENTS: iconIndex = -327; break;
111 case IDM_RUN: iconIndex = -328; break;
112 case IDM_SHUTDOWN: iconIndex = -329; break;
113 case IDM_SETTINGS: iconIndex = -330; break;
114
115 case IDM_CONTROLPANEL: iconIndex = -22; break;
116 case IDM_NETWORKCONNECTIONS: iconIndex = -257; break;
117 case IDM_PRINTERSANDFAXES: iconIndex = -138; break;
118 case IDM_TASKBARANDSTARTMENU: iconIndex = -40; break;
119 //case IDM_SECURITY: iconIndex = -21; break;
120 //case IDM_SYNCHRONIZE: iconIndex = -21; break;
121 //case IDM_DISCONNECT: iconIndex = -21; break;
122 //case IDM_UNDOCKCOMPUTER: iconIndex = -21; break;
123 default:
124 return S_FALSE;
125 }
126
127 if (iconIndex)
128 {
129 if ((psminfo->dwMask & SMIM_TYPE) != 0)
130 psminfo->dwType = SMIT_STRING;
131 if ((psminfo->dwMask & SMIM_ICON) != 0)
132 psminfo->iIcon = Shell_GetCachedImageIndex(L"shell32.dll", iconIndex, FALSE);
133 if ((psminfo->dwMask & SMIM_FLAGS) != 0)
134 psminfo->dwFlags |= SMIF_ICON;
135 #ifdef TEST_TRACKPOPUPMENU_SUBMENUS
136 if ((psminfo->dwMask & SMIM_FLAGS) != 0)
137 psminfo->dwFlags |= SMIF_TRACKPOPUP;
138 #endif
139 }
140 else
141 {
142 if ((psminfo->dwMask & SMIM_TYPE) != 0)
143 psminfo->dwType = SMIT_SEPARATOR;
144 }
145 return S_OK;
146 }
147
148 HRESULT OnGetSubMenu(LPSMDATA psmd, REFIID iid, void ** pv)
149 {
150 HRESULT hr;
151 int csidl = 0;
152 IShellMenu *pShellMenu;
153
154 hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &pShellMenu));
155 if (FAILED_UNEXPECTEDLY(hr))
156 return hr;
157
158 hr = pShellMenu->Initialize(this, 0, ANCESTORDEFAULT, SMINIT_VERTICAL);
159
160 switch (psmd->uId)
161 {
162 case IDM_PROGRAMS: csidl = CSIDL_PROGRAMS; break;
163 case IDM_FAVORITES: csidl = CSIDL_FAVORITES; break;
164 case IDM_DOCUMENTS: csidl = CSIDL_RECENT; break;
165 }
166
167 if (csidl)
168 {
169 IShellFolder *psfStartMenu;
170
171 if (csidl == CSIDL_PROGRAMS && m_psfPrograms)
172 {
173 psfStartMenu = m_psfPrograms;
174 }
175 else
176 {
177 LPITEMIDLIST pidlStartMenu;
178 IShellFolder *psfDestop;
179 hr = SHGetFolderLocation(NULL, csidl, 0, 0, &pidlStartMenu);
180 hr = SHGetDesktopFolder(&psfDestop);
181 hr = psfDestop->BindToObject(pidlStartMenu, NULL, IID_PPV_ARG(IShellFolder, &psfStartMenu));
182 }
183
184 hr = pShellMenu->SetShellFolder(psfStartMenu, NULL, NULL, 0);
185 }
186 else
187 {
188 MENUITEMINFO mii;
189 mii.cbSize = sizeof(mii);
190 mii.fMask = MIIM_SUBMENU;
191 if (GetMenuItemInfoW(psmd->hmenu, psmd->uId, FALSE, &mii))
192 {
193 hr = pShellMenu->SetMenu(mii.hSubMenu, NULL, SMSET_BOTTOM);
194 }
195 }
196 return pShellMenu->QueryInterface(iid, pv);
197 }
198
199 HRESULT OnGetContextMenu(LPSMDATA psmd, REFIID iid, void ** pv)
200 {
201 if (psmd->uId == IDM_PROGRAMS ||
202 psmd->uId == IDM_CONTROLPANEL ||
203 psmd->uId == IDM_NETWORKCONNECTIONS ||
204 psmd->uId == IDM_PRINTERSANDFAXES)
205 {
206 //UNIMPLEMENTED
207 }
208
209 return S_FALSE;
210 }
211
212 HRESULT OnGetObject(LPSMDATA psmd, REFIID iid, void ** pv)
213 {
214 if (IsEqualIID(iid, IID_IShellMenu))
215 return OnGetSubMenu(psmd, iid, pv);
216 else if (IsEqualIID(iid, IID_IContextMenu))
217 return OnGetContextMenu(psmd, iid, pv);
218
219 return S_FALSE;
220 }
221
222 HRESULT OnExec(LPSMDATA psmd)
223 {
224 if (psmd->uId == IDM_CONTROLPANEL)
225 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}", NULL, NULL, 1);
226 else if (psmd->uId == IDM_NETWORKCONNECTIONS)
227 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}", NULL, NULL, 1);
228 else if (psmd->uId == IDM_PRINTERSANDFAXES)
229 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{2227A280-3AEA-1069-A2DE-08002B30309D}", NULL, NULL, 1);
230 else
231 PostMessageW(m_hwndTray, WM_COMMAND, psmd->uId, 0);
232
233 return S_OK;
234 }
235
236 public:
237
238 DECLARE_NOT_AGGREGATABLE(CShellMenuCallback)
239 DECLARE_PROTECT_FINAL_CONSTRUCT()
240 BEGIN_COM_MAP(CShellMenuCallback)
241 COM_INTERFACE_ENTRY_IID(IID_IShellMenuCallback, IShellMenuCallback)
242 END_COM_MAP()
243
244 void Initialize(
245 IShellMenu* pShellMenu,
246 IBandSite* pBandSite,
247 IDeskBar* pDeskBar)
248 {
249 m_pShellMenu.Attach(pShellMenu);
250 m_pBandSite.Attach(pBandSite);
251 m_pDeskBar.Attach(pDeskBar);
252 }
253
254 ~CShellMenuCallback()
255 {
256 m_pShellMenu.Release();
257 m_pBandSite.Release();
258 m_pDeskBar.Release();
259 }
260
261 HRESULT _SetProgramsFolder(IShellFolder * psf, LPITEMIDLIST pidl)
262 {
263 m_psfPrograms = psf;
264 m_pidlPrograms = pidl;
265 return S_OK;
266 }
267
268 HRESULT STDMETHODCALLTYPE CallbackSM(
269 LPSMDATA psmd,
270 UINT uMsg,
271 WPARAM wParam,
272 LPARAM lParam)
273 {
274 switch (uMsg)
275 {
276 case SMC_INITMENU:
277 return OnInitMenu();
278 case SMC_GETINFO:
279 return OnGetInfo(psmd, reinterpret_cast<SMINFO*>(lParam));
280 case SMC_GETOBJECT:
281 return OnGetObject(psmd, *reinterpret_cast<IID *>(wParam), reinterpret_cast<void **>(lParam));
282 case SMC_EXEC:
283 return OnExec(psmd);
284 case SMC_SFEXEC:
285 m_pTrayPriv->Execute(psmd->psf, psmd->pidlItem);
286 break;
287 case 0x10000000: // _FilterPIDL from CMenuSFToolbar
288 if (psmd->psf->CompareIDs(0, psmd->pidlItem, m_pidlPrograms) == 0)
289 return S_FALSE;
290 return S_OK;
291 }
292
293 return S_FALSE;
294 }
295 };
296
297 HRESULT BindToDesktop(LPCITEMIDLIST pidl, IShellFolder ** ppsfResult)
298 {
299 HRESULT hr;
300 CComPtr<IShellFolder> psfDesktop;
301
302 *ppsfResult = NULL;
303
304 hr = SHGetDesktopFolder(&psfDesktop);
305 if (FAILED(hr))
306 return hr;
307
308 hr = psfDesktop->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, ppsfResult));
309
310 return hr;
311 }
312
313 static HRESULT GetStartMenuFolder(IShellFolder ** ppsfStartMenu)
314 {
315 HRESULT hr;
316 LPITEMIDLIST pidlUserStartMenu;
317 LPITEMIDLIST pidlCommonStartMenu;
318 CComPtr<IShellFolder> psfUserStartMenu;
319 CComPtr<IShellFolder> psfCommonStartMenu;
320 CComPtr<IAugmentedShellFolder> pasf;
321
322 *ppsfStartMenu = NULL;
323
324 hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTMENU, &pidlUserStartMenu);
325 if (FAILED(hr))
326 return hr;
327
328 if (FAILED(SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_STARTMENU, &pidlCommonStartMenu)))
329 {
330 hr = BindToDesktop(pidlUserStartMenu, ppsfStartMenu);
331 ILFree(pidlUserStartMenu);
332 return hr;
333 }
334
335 hr = BindToDesktop(pidlUserStartMenu, &psfUserStartMenu);
336 if (FAILED_UNEXPECTEDLY(hr))
337 return hr;
338
339 hr = BindToDesktop(pidlCommonStartMenu, &psfCommonStartMenu);
340 if (FAILED_UNEXPECTEDLY(hr))
341 return hr;
342
343 #if 1
344 hr = CMergedFolder_Constructor(IID_PPV_ARG(IAugmentedShellFolder, &pasf));
345 #else
346 hr = CoCreateInstance(CLSID_MergedFolder, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IAugmentedShellFolder, &pasf));
347 #endif
348 if (FAILED_UNEXPECTEDLY(hr))
349 {
350 *ppsfStartMenu = psfUserStartMenu.Detach();
351 ILFree(pidlCommonStartMenu);
352 ILFree(pidlUserStartMenu);
353 return hr;
354 }
355
356 hr = pasf->AddNameSpace(NULL, psfUserStartMenu, pidlUserStartMenu, 0xFF00);
357 if (FAILED_UNEXPECTEDLY(hr))
358 return hr;
359
360 hr = pasf->AddNameSpace(NULL, psfCommonStartMenu, pidlCommonStartMenu, 0);
361 if (FAILED_UNEXPECTEDLY(hr))
362 return hr;
363
364 hr = pasf->QueryInterface(IID_PPV_ARG(IShellFolder, ppsfStartMenu));
365 pasf.Release();
366
367 ILFree(pidlCommonStartMenu);
368 ILFree(pidlUserStartMenu);
369
370 return hr;
371 }
372
373 extern "C"
374 HRESULT WINAPI
375 CStartMenu_Constructor(REFIID riid, void **ppv)
376 {
377 IShellMenu* pShellMenu;
378 IBandSite* pBandSite;
379 IDeskBar* pDeskBar;
380
381 HRESULT hr;
382 IShellFolder * psf;
383
384 LPITEMIDLIST pidlProgramsAbsolute;
385 LPITEMIDLIST pidlPrograms;
386 CComPtr<IShellFolder> psfPrograms;
387
388 hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &pShellMenu));
389 if (FAILED_UNEXPECTEDLY(hr))
390 return hr;
391
392 hr = CMenuSite_Constructor(IID_PPV_ARG(IBandSite, &pBandSite));
393 if (FAILED_UNEXPECTEDLY(hr))
394 return hr;
395
396 hr = CMenuDeskBar_Constructor(IID_PPV_ARG(IDeskBar, &pDeskBar));
397 if (FAILED_UNEXPECTEDLY(hr))
398 return hr;
399
400 CComObject<CShellMenuCallback> *pCallback;
401 hr = CComObject<CShellMenuCallback>::CreateInstance(&pCallback);
402 if (FAILED_UNEXPECTEDLY(hr))
403 return hr;
404 pCallback->AddRef(); // CreateInstance returns object with 0 ref count */
405 pCallback->Initialize(pShellMenu, pBandSite, pDeskBar);
406
407 pShellMenu->Initialize(pCallback, (UINT) -1, 0, SMINIT_TOPLEVEL | SMINIT_VERTICAL);
408 if (FAILED_UNEXPECTEDLY(hr))
409 return hr;
410
411 hr = GetStartMenuFolder(&psf);
412 if (FAILED_UNEXPECTEDLY(hr))
413 return hr;
414
415 hr = SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &pidlProgramsAbsolute);
416 if (FAILED_UNEXPECTEDLY(hr))
417 return hr;
418
419 pidlPrograms = ILClone(ILFindLastID(pidlProgramsAbsolute));
420 ILFree(pidlProgramsAbsolute);
421
422 hr = psf->BindToObject(pidlPrograms, NULL, IID_PPV_ARG(IShellFolder, &psfPrograms));
423 if (FAILED_UNEXPECTEDLY(hr))
424 return hr;
425
426 hr = pCallback->_SetProgramsFolder(psfPrograms, pidlPrograms);
427 if (FAILED_UNEXPECTEDLY(hr))
428 return hr;
429
430 hr = pShellMenu->SetShellFolder(psf, NULL, NULL, 0);
431 if (FAILED_UNEXPECTEDLY(hr))
432 return hr;
433
434 hr = pDeskBar->SetClient(pBandSite);
435 if (FAILED_UNEXPECTEDLY(hr))
436 return hr;
437
438 hr = pBandSite->AddBand(pShellMenu);
439 if (FAILED_UNEXPECTEDLY(hr))
440 return hr;
441
442 return pDeskBar->QueryInterface(riid, ppv);
443 }