4 * Copyright 2016 Sylvain Deverre <deverre dot sylv at gmail dot com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <commoncontrols.h>
23 #include <undocshell.h>
28 #define UNIMPLEMENTED DbgPrint("%s is UNIMPLEMENTED!\n", __FUNCTION__)
32 HRESULT WINAPI
CExplorerBand_Constructor(REFIID riid
, LPVOID
*ppv
)
34 return ShellObjectCreator
<CExplorerBand
>(riid
, ppv
);
37 CExplorerBand::CExplorerBand() :
38 pSite(NULL
), fVisible(FALSE
), dwBandID(0)
42 CExplorerBand::~CExplorerBand()
46 void CExplorerBand::InitializeExplorerBand()
48 // Init the treeview here
51 CComPtr
<IWebBrowser2
> browserService
;
52 SHChangeNotifyEntry shcne
;
54 hr
= SHGetDesktopFolder(&pDesktop
);
55 if (FAILED_UNEXPECTEDLY(hr
))
58 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidl
);
59 if (FAILED_UNEXPECTEDLY(hr
))
63 hr
= SHGetImageList(SHIL_SMALL
, IID_PPV_ARG(IImageList
, &piml
));
64 if (FAILED_UNEXPECTEDLY(hr
))
67 TreeView_SetImageList(m_hWnd
, (HIMAGELIST
)piml
, TVSIL_NORMAL
);
69 // Insert the root node
70 HTREEITEM hItem
= InsertItem(0, pDesktop
, pidl
, pidl
, FALSE
);
73 ERR("Failed to create root item\n");
77 NodeInfo
* pNodeInfo
= GetNodeInfo(hItem
);
80 InsertSubitems(hItem
, pNodeInfo
);
81 TreeView_Expand(m_hWnd
, hItem
, TVE_EXPAND
);
83 // Navigate to current folder position
84 //NavigateToCurrentFolder();
86 // Register shell notification
88 shcne
.fRecursive
= TRUE
;
89 shellRegID
= SHChangeNotifyRegister(
91 SHCNRF_ShellLevel
| SHCNRF_InterruptLevel
| SHCNRF_RecursiveInterrupt
,
92 SHCNE_DISKEVENTS
| SHCNE_RENAMEFOLDER
| SHCNE_RMDIR
| SHCNE_MKDIR
,
98 ERR("Something went wrong, error %08x\n", GetLastError());
100 // Register browser connection endpoint
101 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
102 if (FAILED_UNEXPECTEDLY(hr
))
105 hr
= AtlAdvise(browserService
, dynamic_cast<IDispatch
*>(this), DIID_DWebBrowserEvents
, &adviseCookie
);
106 if (FAILED_UNEXPECTEDLY(hr
))
112 void CExplorerBand::DestroyExplorerBand()
115 CComPtr
<IWebBrowser2
> browserService
;
117 TRACE("Cleaning up explorer band ...\n");
119 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
120 if (FAILED_UNEXPECTEDLY(hr
))
123 hr
= AtlUnadvise(browserService
, DIID_DWebBrowserEvents
, adviseCookie
);
124 /* Remove all items of the treeview */
125 RevokeDragDrop(m_hWnd
);
126 TreeView_DeleteAllItems(m_hWnd
);
129 TRACE("Cleanup done !\n");
132 CExplorerBand::NodeInfo
* CExplorerBand::GetNodeInfo(HTREEITEM hItem
)
136 tvItem
.mask
= TVIF_PARAM
;
137 tvItem
.hItem
= hItem
;
139 if (!TreeView_GetItem(m_hWnd
, &tvItem
))
142 return reinterpret_cast<NodeInfo
*>(tvItem
.lParam
);
145 HRESULT
CExplorerBand::UpdateBrowser(LPITEMIDLIST pidlGoto
)
147 CComPtr
<IShellBrowser
> pBrowserService
;
150 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IShellBrowser
, &pBrowserService
));
151 if (FAILED_UNEXPECTEDLY(hr
))
154 hr
= pBrowserService
->BrowseObject(pidlGoto
, SBSP_SAMEBROWSER
| SBSP_ABSOLUTE
);
155 if (FAILED_UNEXPECTEDLY(hr
))
161 // *** notifications handling ***
162 BOOL
CExplorerBand::OnTreeItemExpanding(LPNMTREEVIEW pnmtv
)
166 if (pnmtv
->action
== TVE_COLLAPSE
) {
167 if (pnmtv
->itemNew
.hItem
== hRoot
)
169 // Prenvent root from collapsing
170 pnmtv
->itemNew
.mask
|= TVIF_STATE
;
171 pnmtv
->itemNew
.stateMask
|= TVIS_EXPANDED
;
172 pnmtv
->itemNew
.state
&= ~TVIS_EXPANDED
;
173 pnmtv
->action
= TVE_EXPAND
;
177 if (pnmtv
->action
== TVE_EXPAND
) {
178 // Grab our directory PIDL
179 pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
180 // We have it, let's try
181 if (pNodeInfo
&& !pNodeInfo
->expanded
)
182 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, pNodeInfo
)) {
183 // remove subitem "+" since we failed to add subitems
186 tvItem
.mask
= TVIF_CHILDREN
;
187 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
188 tvItem
.cChildren
= 0;
190 TreeView_SetItem(m_hWnd
, &tvItem
);
196 void CExplorerBand::OnSelectionChanged(LPNMTREEVIEW pnmtv
)
198 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
200 UpdateBrowser(pNodeInfo
->absolutePidl
);
203 //TreeView_Expand(m_hWnd, pnmtv->itemNew.hItem, TVE_EXPAND);
206 // *** Helper functions ***
207 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, IShellFolder
*psfParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
209 TV_INSERTSTRUCT tvInsert
;
210 HTREEITEM htiCreated
;
212 /* Get the attributes of the node */
213 SFGAOF attrs
= SFGAO_STREAM
| SFGAO_HASSUBFOLDER
;
214 HRESULT hr
= psfParent
->GetAttributesOf(1, &pEltRelative
, &attrs
);
215 if (FAILED_UNEXPECTEDLY(hr
))
219 if ((attrs
& SFGAO_STREAM
))
221 TRACE("Ignoring stream\n");
225 /* Get the name of the node */
226 WCHAR wszDisplayName
[MAX_PATH
];
227 if (!ILGetDisplayNameEx(psfParent
, pEltRelative
, wszDisplayName
, ILGDN_INFOLDER
))
229 ERR("Failed to get node name\n");
233 /* Get the icon of the node */
234 INT iIcon
= SHMapPIDLToSystemImageListIndex(psfParent
, pEltRelative
, NULL
);
236 NodeInfo
* pChildInfo
= new NodeInfo
;
239 ERR("Failed to allocate NodeInfo\n");
243 // Store our node info
244 pChildInfo
->absolutePidl
= ILClone(pElt
);
245 pChildInfo
->relativePidl
= ILClone(pEltRelative
);
246 pChildInfo
->expanded
= FALSE
;
248 // Set up our treeview template
249 tvInsert
.hParent
= hParent
;
250 tvInsert
.hInsertAfter
= TVI_LAST
;
251 tvInsert
.item
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
252 tvInsert
.item
.cchTextMax
= MAX_PATH
;
253 tvInsert
.item
.pszText
= wszDisplayName
;
254 tvInsert
.item
.iImage
= tvInsert
.item
.iSelectedImage
= iIcon
;
255 tvInsert
.item
.cChildren
= (attrs
& SFGAO_HASSUBFOLDER
) ? 1 : 0;
256 tvInsert
.item
.lParam
= (LPARAM
)pChildInfo
;
258 htiCreated
= TreeView_InsertItem(m_hWnd
, &tvInsert
);
263 BOOL
CExplorerBand::InsertSubitems(HTREEITEM hItem
, NodeInfo
*pNodeInfo
)
265 CComPtr
<IEnumIDList
> pEnumIDList
;
266 LPITEMIDLIST pidlSub
;
272 CComPtr
<IShellFolder
> pFolder
;
274 entry
= pNodeInfo
->absolutePidl
;
277 EnumFlags
= SHCONTF_FOLDERS
;
279 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidlSub
);
282 ERR("Can't get desktop PIDL !\n");
286 if (!pDesktop
->CompareIDs(NULL
, pidlSub
, entry
))
288 // We are the desktop, so use pDesktop as pFolder
293 // Get an IShellFolder of our pidl
294 hr
= pDesktop
->BindToObject(entry
, NULL
, IID_PPV_ARG(IShellFolder
, &pFolder
));
298 ERR("Can't bind folder to desktop !\n");
304 // TODO: handle hidden folders according to settings !
305 EnumFlags
|= SHCONTF_INCLUDEHIDDEN
;
307 // Enum through objects
308 hr
= pFolder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
310 // avoid broken IShellFolder implementations that return null pointer with success
311 if (!SUCCEEDED(hr
) || !pEnumIDList
)
313 ERR("Can't enum the folder !\n");
317 /* Don't redraw while we add stuff into the tree */
318 SendMessage(WM_SETREDRAW
, FALSE
, 0);
319 while(SUCCEEDED(pEnumIDList
->Next(1, &pidlSub
, &fetched
)) && pidlSub
&& fetched
)
321 LPITEMIDLIST pidlSubComplete
;
322 pidlSubComplete
= ILCombine(entry
, pidlSub
);
324 if (InsertItem(hItem
, pFolder
, pidlSubComplete
, pidlSub
, FALSE
))
326 ILFree(pidlSubComplete
);
329 pNodeInfo
->expanded
= TRUE
;
331 /* Now we can redraw */
332 SendMessage(WM_SETREDRAW
, TRUE
, 0);
334 return (uItemCount
> 0) ? TRUE
: FALSE
;
337 // *** IOleWindow methods ***
338 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetWindow(HWND
*lphwnd
)
346 HRESULT STDMETHODCALLTYPE
CExplorerBand::ContextSensitiveHelp(BOOL fEnterMode
)
353 // *** IDockingWindow methods ***
354 HRESULT STDMETHODCALLTYPE
CExplorerBand::CloseDW(DWORD dwReserved
)
356 // We do nothing, we don't have anything to save yet
357 TRACE("CloseDW called\n");
361 HRESULT STDMETHODCALLTYPE
CExplorerBand::ResizeBorderDW(const RECT
*prcBorder
, IUnknown
*punkToolbarSite
, BOOL fReserved
)
363 /* Must return E_NOTIMPL according to MSDN */
367 HRESULT STDMETHODCALLTYPE
CExplorerBand::ShowDW(BOOL fShow
)
375 // *** IDeskBand methods ***
376 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetBandInfo(DWORD dwBandID
, DWORD dwViewMode
, DESKBANDINFO
*pdbi
)
382 this->dwBandID
= dwBandID
;
384 if (pdbi
->dwMask
& DBIM_MINSIZE
)
386 pdbi
->ptMinSize
.x
= 200;
387 pdbi
->ptMinSize
.y
= 30;
390 if (pdbi
->dwMask
& DBIM_MAXSIZE
)
392 pdbi
->ptMaxSize
.y
= -1;
395 if (pdbi
->dwMask
& DBIM_INTEGRAL
)
397 pdbi
->ptIntegral
.y
= 1;
400 if (pdbi
->dwMask
& DBIM_ACTUAL
)
402 pdbi
->ptActual
.x
= 200;
403 pdbi
->ptActual
.y
= 30;
406 if (pdbi
->dwMask
& DBIM_TITLE
)
408 lstrcpyW(pdbi
->wszTitle
, L
"Explorer");
411 if (pdbi
->dwMask
& DBIM_MODEFLAGS
)
413 pdbi
->dwModeFlags
= DBIMF_NORMAL
| DBIMF_VARIABLEHEIGHT
;
416 if (pdbi
->dwMask
& DBIM_BKCOLOR
)
418 pdbi
->dwMask
&= ~DBIM_BKCOLOR
;
424 // *** IObjectWithSite methods ***
425 HRESULT STDMETHODCALLTYPE
CExplorerBand::SetSite(IUnknown
*pUnkSite
)
430 if (pUnkSite
== pSite
)
433 TRACE("SetSite called \n");
436 DestroyExplorerBand();
441 if (pUnkSite
!= pSite
)
449 hr
= IUnknown_GetWindow(pUnkSite
, &parentWnd
);
452 ERR("Could not get parent's window ! Status: %08lx\n", hr
);
461 SetParent(parentWnd
);
465 HWND wnd
= CreateWindow(WC_TREEVIEW
, NULL
,
466 WS_CHILD
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_SHOWSELALWAYS
| TVS_EDITLABELS
/* | TVS_SINGLEEXPAND*/ , // remove TVS_SINGLEEXPAND for now since it has strange behaviour
467 0, 0, 0, 0, parentWnd
, NULL
, _AtlBaseModule
.GetModuleInstance(), NULL
);
469 // Subclass the window
472 // Initialize our treeview now
473 InitializeExplorerBand();
474 RegisterDragDrop(m_hWnd
, dynamic_cast<IDropTarget
*>(this));
479 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSite(REFIID riid
, void **ppvSite
)
488 // *** IOleCommandTarget methods ***
489 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
495 HRESULT STDMETHODCALLTYPE
CExplorerBand::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
502 // *** IServiceProvider methods ***
503 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
510 // *** IInputObject methods ***
511 HRESULT STDMETHODCALLTYPE
CExplorerBand::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
518 // TODO: handle message
521 TranslateMessage(lpMsg
);
522 DispatchMessage(lpMsg
);
527 HRESULT STDMETHODCALLTYPE
CExplorerBand::HasFocusIO()
529 return bFocused
? S_OK
: S_FALSE
;
532 HRESULT STDMETHODCALLTYPE
CExplorerBand::TranslateAcceleratorIO(LPMSG lpMsg
)
534 TranslateMessage(lpMsg
);
535 DispatchMessage(lpMsg
);
540 // *** IPersist methods ***
541 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetClassID(CLSID
*pClassID
)
545 memcpy(pClassID
, &CLSID_ExplorerBand
, sizeof(CLSID
));
550 // *** IPersistStream methods ***
551 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsDirty()
557 HRESULT STDMETHODCALLTYPE
CExplorerBand::Load(IStream
*pStm
)
563 HRESULT STDMETHODCALLTYPE
CExplorerBand::Save(IStream
*pStm
, BOOL fClearDirty
)
569 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
576 // *** IWinEventHandler methods ***
577 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
579 if (uMsg
== WM_NOTIFY
)
581 NMHDR
*pNotifyHeader
= (NMHDR
*)lParam
;
582 switch (pNotifyHeader
->code
)
584 case TVN_ITEMEXPANDING
:
585 *theResult
= OnTreeItemExpanding((LPNMTREEVIEW
)lParam
);
588 OnSelectionChanged((LPNMTREEVIEW
)lParam
);
597 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsWindowOwner(HWND hWnd
)
599 return (hWnd
== m_hWnd
) ? S_OK
: S_FALSE
;
602 // *** IBandNavigate methods ***
603 HRESULT STDMETHODCALLTYPE
CExplorerBand::Select(long paramC
)
609 // *** INamespaceProxy ***
610 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetNavigateTarget(long paramC
, long param10
, long param14
)
616 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(long paramC
)
622 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnSelectionChanged(long paramC
)
628 HRESULT STDMETHODCALLTYPE
CExplorerBand::RefreshFlags(long paramC
, long param10
, long param14
)
634 HRESULT STDMETHODCALLTYPE
CExplorerBand::CacheItem(long paramC
)
640 // *** IDispatch methods ***
641 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfoCount(UINT
*pctinfo
)
647 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfo(UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
653 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetIDsOfNames(REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
659 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
665 // *** IDropTarget methods ***
666 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragEnter(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
672 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragOver(DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
678 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragLeave()
684 HRESULT STDMETHODCALLTYPE
CExplorerBand::Drop(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
690 // *** IDropSource methods ***
691 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
)
697 HRESULT STDMETHODCALLTYPE
CExplorerBand::GiveFeedback(DWORD dwEffect
)