Fix remaining text file line endings in the tree. (#18)
[reactos.git] / dll / win32 / shell32 / CFolderItemVerbs.cpp
1 /*
2 * FolderItemVerb(s) implementation
3 *
4 * Copyright 2015 Mark Jansen
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 St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "precomp.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(shell);
24
25
26 CFolderItemVerb::CFolderItemVerb()
27 {
28 }
29
30 CFolderItemVerb::~CFolderItemVerb()
31 {
32 }
33
34 void CFolderItemVerb::Init(IContextMenu* menu, BSTR name)
35 {
36 m_contextmenu = menu;
37 m_name.m_str = name;
38 }
39
40 // *** FolderItemVerb methods ***
41
42 HRESULT STDMETHODCALLTYPE CFolderItemVerb::get_Application(IDispatch **ppid)
43 {
44 TRACE("(%p, %p)\n", this, ppid);
45 return E_NOTIMPL;
46 }
47
48 HRESULT STDMETHODCALLTYPE CFolderItemVerb::get_Parent(IDispatch **ppid)
49 {
50 TRACE("(%p, %p)\n", this, ppid);
51 return E_NOTIMPL;
52 }
53
54 HRESULT STDMETHODCALLTYPE CFolderItemVerb::get_Name(BSTR *pbs)
55 {
56 if (!pbs)
57 return E_POINTER;
58 *pbs = SysAllocString(m_name);
59 return S_OK;
60 }
61
62 HRESULT STDMETHODCALLTYPE CFolderItemVerb::DoIt()
63 {
64 TRACE("(%p, %p)\n", this);
65 return E_NOTIMPL;
66 }
67
68
69
70
71
72
73 CFolderItemVerbs::CFolderItemVerbs()
74 :m_menu(NULL)
75 ,m_count(0)
76 {
77 }
78
79 CFolderItemVerbs::~CFolderItemVerbs()
80 {
81 DestroyMenu(m_menu);
82 }
83
84 HRESULT CFolderItemVerbs::Init(LPITEMIDLIST idlist)
85 {
86 CComPtr<IShellFolder> folder;
87 LPCITEMIDLIST child;
88 HRESULT hr = SHBindToParent(idlist, IID_PPV_ARG(IShellFolder, &folder), &child);
89 if (FAILED_UNEXPECTEDLY(hr))
90 return hr;
91
92 hr = folder->GetUIObjectOf(NULL, 1, &child, IID_IContextMenu, NULL, (PVOID*)&m_contextmenu);
93 if (FAILED_UNEXPECTEDLY(hr))
94 return hr;
95
96 m_menu = CreatePopupMenu();
97 hr = m_contextmenu->QueryContextMenu(m_menu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
98 if (!SUCCEEDED(hr))
99 return hr;
100
101 m_count = GetMenuItemCount(m_menu);
102 return hr;
103 }
104
105
106 // *** FolderItemVerbs methods ***
107 HRESULT STDMETHODCALLTYPE CFolderItemVerbs::get_Count(LONG *plCount)
108 {
109 if (!plCount)
110 return E_POINTER;
111 *plCount = m_count;
112 return S_OK;
113 }
114
115 HRESULT STDMETHODCALLTYPE CFolderItemVerbs::get_Application(IDispatch **ppid)
116 {
117 TRACE("(%p, %p)\n", this, ppid);
118 return E_NOTIMPL;
119 }
120
121 HRESULT STDMETHODCALLTYPE CFolderItemVerbs::get_Parent(IDispatch **ppid)
122 {
123 TRACE("(%p, %p)\n", this, ppid);
124 return E_NOTIMPL;
125 }
126
127 HRESULT STDMETHODCALLTYPE CFolderItemVerbs::Item(VARIANT indexVar, FolderItemVerb **ppid)
128 {
129 if (!ppid)
130 return E_POINTER;
131
132 CComVariant var;
133 VariantCopyInd(&var, &indexVar);
134
135 HRESULT hr = VariantChangeType(&var, &var, 0, VT_I4);
136 if (FAILED_UNEXPECTEDLY(hr))
137 return E_INVALIDARG;
138
139 int index = V_I4(&var);
140
141 if (index > m_count)
142 return S_OK;
143
144 BSTR name = NULL;
145
146 if(index == m_count)
147 name = SysAllocStringLen(NULL, 0);
148 else
149 {
150 MENUITEMINFOW info = { sizeof(info), 0 };
151 info.fMask = MIIM_STRING;
152 if (!GetMenuItemInfoW(m_menu, index, TRUE, &info))
153 return E_FAIL;
154 name = SysAllocStringLen(NULL, info.cch);
155 if (name)
156 {
157 info.dwTypeData = name;
158 info.cch++;
159 GetMenuItemInfoW(m_menu, index, TRUE, &info);
160 }
161 }
162
163 if (!name)
164 return E_OUTOFMEMORY;
165
166 CFolderItemVerb* verb = new CComObject<CFolderItemVerb>();
167 verb->Init(m_contextmenu, name);
168 verb->AddRef();
169 *ppid = verb;
170
171 return S_OK;
172 }
173
174 HRESULT STDMETHODCALLTYPE CFolderItemVerbs::_NewEnum(IUnknown **ppunk)
175 {
176 TRACE("(%p, %p)\n", this, ppunk);
177 return E_NOTIMPL;
178 }
179
180