[RSHELL]
[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 //#define TEST_TRACKPOPUPMENU_SUBMENUS
23
24
25 /* NOTE: The following constants *MUST NOT* be changed because
26 they're hardcoded and need to be the exact values
27 in order to get the start menu to work! */
28 #define IDM_PROGRAMS 504
29 #define IDM_FAVORITES 507
30 #define IDM_DOCUMENTS 501
31 #define IDM_SETTINGS 508
32 #define IDM_CONTROLPANEL 505
33 #define IDM_SECURITY 5001
34 #define IDM_NETWORKCONNECTIONS 557
35 #define IDM_PRINTERSANDFAXES 510
36 #define IDM_TASKBARANDSTARTMENU 413
37 #define IDM_SEARCH 520
38 #define IDM_HELPANDSUPPORT 503
39 #define IDM_RUN 401
40 #define IDM_SYNCHRONIZE 553
41 #define IDM_LOGOFF 402
42 #define IDM_DISCONNECT 5000
43 #define IDM_UNDOCKCOMPUTER 410
44 #define IDM_SHUTDOWN 506
45 #define IDM_LASTSTARTMENU_SEPARATOR 450
46
47 /*
48 * TODO:
49 * 1. append the start menu contents from all users
50 * 2. implement the context menu for start menu entries (programs, control panel, network connetions, printers)
51 * 3. filter out programs folder from the shell folder part of the start menu
52 * 4. showing the programs start menu is SLOW compared to windows. this needs some investigation
53 */
54
55 class CShellMenuCallback :
56 public CComObjectRootEx<CComMultiThreadModelNoCS>,
57 public IShellMenuCallback
58 {
59 private:
60
61 HWND m_hwndTray;
62 CComPtr<IShellMenu> m_pShellMenu;
63 CComPtr<IBandSite> m_pBandSite;
64 CComPtr<IDeskBar> m_pDeskBar;
65 CComPtr<ITrayPriv> m_pTrayPriv;
66
67 HRESULT OnInitMenu()
68 {
69 HMENU hmenu;
70 HRESULT hr;
71
72 if (m_pTrayPriv.p)
73 return S_OK;
74
75 hr = IUnknown_GetSite(m_pDeskBar, IID_PPV_ARG(ITrayPriv, &m_pTrayPriv));
76 hr = IUnknown_GetWindow(m_pTrayPriv, &m_hwndTray);
77 hr = m_pTrayPriv->AppendMenuW(&hmenu);
78 #ifndef TEST_TRACKPOPUPMENU_SUBMENUS
79 hr = m_pShellMenu->SetMenu(hmenu, NULL, SMSET_BOTTOM);
80 #else
81 hr = m_pShellMenu->SetMenu(hmenu, m_hwndTray, SMSET_BOTTOM);
82 #endif
83
84 return hr;
85 }
86
87 HRESULT OnGetInfo(LPSMDATA psmd, SMINFO *psminfo)
88 {
89 int iconIndex = 0;
90
91 switch (psmd->uId)
92 {
93 // Smaller "24x24" icons used for the start menu
94 // The bitmaps are still 32x32, but the image is centered
95 case IDM_FAVORITES: iconIndex = -209; break;
96 case IDM_SEARCH: iconIndex = -210; break;
97 case IDM_HELPANDSUPPORT: iconIndex = -211; break;
98 case IDM_LOGOFF: iconIndex = -212; break;
99 case IDM_PROGRAMS: iconIndex = -213; break;
100 case IDM_DOCUMENTS: iconIndex = -214; break;
101 case IDM_RUN: iconIndex = -215; break;
102 case IDM_SHUTDOWN: iconIndex = -216; break;
103 case IDM_SETTINGS: iconIndex = -217; break;
104
105 case IDM_CONTROLPANEL: iconIndex = -22; break;
106 case IDM_NETWORKCONNECTIONS: iconIndex = -257; break;
107 case IDM_PRINTERSANDFAXES: iconIndex = -138; break;
108 case IDM_TASKBARANDSTARTMENU: iconIndex = -40; break;
109 //case IDM_SECURITY: iconIndex = -21; break;
110 //case IDM_SYNCHRONIZE: iconIndex = -21; break;
111 //case IDM_DISCONNECT: iconIndex = -21; break;
112 //case IDM_UNDOCKCOMPUTER: iconIndex = -21; break;
113 default:
114 return S_FALSE;
115 }
116
117 if (iconIndex)
118 {
119 if ((psminfo->dwMask & SMIM_ICON) != 0)
120 psminfo->iIcon = Shell_GetCachedImageIndex(L"shell32.dll", iconIndex, FALSE);
121 #ifdef TEST_TRACKPOPUPMENU_SUBMENUS
122 if ((psminfo->dwMask & SMIM_FLAGS) != 0)
123 psminfo->dwFlags |= SMIF_TRACKPOPUP;
124 #endif
125 }
126 return S_OK;
127 }
128
129 HRESULT OnGetSubMenu(LPSMDATA psmd, REFIID iid, void ** pv)
130 {
131 HRESULT hr;
132 int csidl = 0;
133 IShellMenu *pShellMenu;
134
135 switch (psmd->uId)
136 {
137 case IDM_PROGRAMS: csidl = CSIDL_PROGRAMS; break;
138 case IDM_FAVORITES: csidl = CSIDL_FAVORITES; break;
139 case IDM_DOCUMENTS: csidl = CSIDL_RECENT; break;
140 }
141
142 #if USE_SYSTEM_MENUBAND
143 hr = CoCreateInstance(CLSID_MenuBand,
144 NULL,
145 CLSCTX_INPROC_SERVER,
146 IID_PPV_ARG(IShellMenu, &pShellMenu));
147 #else
148 hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &pShellMenu));
149 #endif
150 if (FAILED(hr))
151 return hr;
152 #if WRAP_MENUBAND
153 hr = CMenuBand_Wrapper(pShellMenu, IID_PPV_ARG(IShellMenu, &pShellMenu));
154 if (FAILED(hr))
155 return hr;
156 #endif
157
158 hr = pShellMenu->Initialize(this, 0, ANCESTORDEFAULT, SMINIT_VERTICAL);
159
160 if (csidl)
161 {
162 LPITEMIDLIST pidlStartMenu;
163 IShellFolder *psfDestop, *psfStartMenu;
164
165 hr = SHGetFolderLocation(NULL, csidl, 0, 0, &pidlStartMenu);
166 hr = SHGetDesktopFolder(&psfDestop);
167 hr = psfDestop->BindToObject(pidlStartMenu, NULL, IID_PPV_ARG(IShellFolder, &psfStartMenu));
168
169 hr = pShellMenu->SetShellFolder(psfStartMenu, NULL, NULL, 0);
170 }
171 else
172 {
173 MENUITEMINFO mii;
174 mii.cbSize = sizeof(mii);
175 mii.fMask = MIIM_SUBMENU;
176 if (GetMenuItemInfoW(psmd->hmenu, psmd->uId, FALSE, &mii))
177 {
178 hr = pShellMenu->SetMenu(mii.hSubMenu, NULL, SMSET_BOTTOM);
179 }
180 }
181 return pShellMenu->QueryInterface(iid, pv);
182 }
183
184 HRESULT OnGetContextMenu(LPSMDATA psmd, REFIID iid, void ** pv)
185 {
186 if (psmd->uId == IDM_PROGRAMS ||
187 psmd->uId == IDM_CONTROLPANEL ||
188 psmd->uId == IDM_NETWORKCONNECTIONS ||
189 psmd->uId == IDM_PRINTERSANDFAXES)
190 {
191 //UNIMPLEMENTED
192 }
193
194 return S_FALSE;
195 }
196
197 HRESULT OnGetObject(LPSMDATA psmd, REFIID iid, void ** pv)
198 {
199 if (IsEqualIID(iid, IID_IShellMenu))
200 return OnGetSubMenu(psmd, iid, pv);
201 else if (IsEqualIID(iid, IID_IContextMenu))
202 return OnGetContextMenu(psmd, iid, pv);
203
204 return S_FALSE;
205 }
206
207 HRESULT OnExec(LPSMDATA psmd)
208 {
209 if (psmd->uId == IDM_CONTROLPANEL)
210 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}", NULL, NULL, 1);
211 else if (psmd->uId == IDM_NETWORKCONNECTIONS)
212 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}", NULL, NULL, 1);
213 else if (psmd->uId == IDM_PRINTERSANDFAXES)
214 ShellExecuteW(NULL, L"open", L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\\::{2227A280-3AEA-1069-A2DE-08002B30309D}", NULL, NULL, 1);
215 else
216 PostMessageW(m_hwndTray, WM_COMMAND, psmd->uId, 0);
217
218 return S_OK;
219 }
220
221 public:
222
223 DECLARE_NOT_AGGREGATABLE(CShellMenuCallback)
224 DECLARE_PROTECT_FINAL_CONSTRUCT()
225 BEGIN_COM_MAP(CShellMenuCallback)
226 COM_INTERFACE_ENTRY_IID(IID_IShellMenuCallback, IShellMenuCallback)
227 END_COM_MAP()
228
229 void Initialize(
230 IShellMenu* pShellMenu,
231 IBandSite* pBandSite,
232 IDeskBar* pDeskBar)
233 {
234 m_pShellMenu.Attach(pShellMenu);
235 m_pBandSite.Attach(pBandSite);
236 m_pDeskBar.Attach(pDeskBar);
237 }
238
239 ~CShellMenuCallback()
240 {
241 m_pShellMenu.Release();
242 m_pBandSite.Release();
243 m_pDeskBar.Release();
244 }
245
246 HRESULT STDMETHODCALLTYPE CallbackSM(
247 LPSMDATA psmd,
248 UINT uMsg,
249 WPARAM wParam,
250 LPARAM lParam)
251 {
252 switch (uMsg)
253 {
254 case SMC_INITMENU:
255 return OnInitMenu();
256 case SMC_GETINFO:
257 return OnGetInfo(psmd, reinterpret_cast<SMINFO*>(lParam));
258 case SMC_GETOBJECT:
259 return OnGetObject(psmd, *reinterpret_cast<IID *>(wParam), reinterpret_cast<void **>(lParam));
260 case SMC_EXEC:
261 return OnExec(psmd);
262 case SMC_SFEXEC:
263 m_pTrayPriv->Execute(psmd->psf, psmd->pidlItem);
264 break;
265 }
266
267 return S_FALSE;
268 }
269 };
270
271 extern "C"
272 HRESULT WINAPI
273 CStartMenu_Constructor(REFIID riid, void **ppv)
274 {
275 IShellMenu* pShellMenu;
276 IBandSite* pBandSite;
277 IDeskBar* pDeskBar;
278 LPITEMIDLIST pidlStartMenu;
279
280 HRESULT hr;
281 IShellFolder *shellFolder;
282 IShellFolder *psfStartMenu;
283
284 #if USE_SYSTEM_MENUBAND
285 hr = CoCreateInstance(CLSID_MenuBand,
286 NULL,
287 CLSCTX_INPROC_SERVER,
288 IID_PPV_ARG(IShellMenu, &pShellMenu));
289 #else
290 hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &pShellMenu));
291 #endif
292 if (FAILED(hr))
293 return hr;
294 #if WRAP_MENUBAND
295 hr = CMenuBand_Wrapper(pShellMenu, IID_PPV_ARG(IShellMenu, &pShellMenu));
296 if (FAILED(hr))
297 return hr;
298 #endif
299
300 #if USE_SYSTEM_MENUSITE
301 hr = CoCreateInstance(CLSID_MenuBandSite,
302 NULL,
303 CLSCTX_INPROC_SERVER,
304 IID_PPV_ARG(IBandSite, &pBandSite));
305 #else
306 hr = CMenuSite_Constructor(IID_PPV_ARG(IBandSite, &pBandSite));
307 #endif
308 if (FAILED(hr))
309 return hr;
310 #if WRAP_MENUSITE
311 hr = CMenuSite_Wrapper(pBandSite, IID_PPV_ARG(IBandSite, &pBandSite));
312 if (FAILED(hr))
313 return hr;
314 #endif
315
316 #if USE_SYSTEM_MENUDESKBAR
317 hr = CoCreateInstance(CLSID_MenuDeskBar,
318 NULL,
319 CLSCTX_INPROC_SERVER,
320 IID_PPV_ARG(IDeskBar, &pDeskBar));
321 #else
322 hr = CMenuDeskBar_Constructor(IID_PPV_ARG(IDeskBar, &pDeskBar));
323 #endif
324 if (FAILED(hr))
325 return hr;
326 #if WRAP_MENUDESKBAR
327 hr = CMenuDeskBar_Wrapper(pDeskBar, IID_PPV_ARG(IDeskBar, &pDeskBar));
328 if (FAILED(hr))
329 return hr;
330 #endif
331
332 CComObject<CShellMenuCallback> *pCallback;
333 hr = CComObject<CShellMenuCallback>::CreateInstance(&pCallback);
334 if (FAILED(hr))
335 return hr;
336 pCallback->AddRef(); // CreateInstance returns object with 0 ref count */
337 pCallback->Initialize(pShellMenu, pBandSite, pDeskBar);
338
339 pShellMenu->Initialize(pCallback, (UINT)-1, 0, SMINIT_TOPLEVEL | SMINIT_VERTICAL);
340 if (FAILED(hr))
341 return hr;
342
343 /* FIXME: Use CLSID_MergedFolder class and IID_IAugmentedShellFolder2 interface here */
344 /* CLSID_MergedFolder 26fdc864-be88-46e7-9235-032d8ea5162e */
345 /* IID_IAugmentedShellFolder2 8db3b3f4-6cfe-11d1-8ae9-00c04fd918d0 */
346 hr = SHGetFolderLocation(NULL, CSIDL_STARTMENU, 0, 0, &pidlStartMenu);
347 hr = SHGetDesktopFolder(&shellFolder);
348 hr = shellFolder->BindToObject(pidlStartMenu, NULL, IID_IShellFolder, (void**) &psfStartMenu);
349
350 hr = pShellMenu->SetShellFolder(psfStartMenu, NULL, NULL, 0);
351
352 hr = pDeskBar->SetClient(pBandSite);
353 if (FAILED(hr))
354 return hr;
355
356 hr = pBandSite->AddBand(pShellMenu);
357 if (FAILED(hr))
358 return hr;
359
360 return pDeskBar->QueryInterface(riid, ppv);
361 }