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__)
33 * - Monitor correctly "external" shell interrupts (seems like we need to register/deregister them for each folder)
34 * - find and fix what cause explorer crashes sometimes (seems to be explorer that does more releases than addref)
38 typedef struct _PIDLDATA
42 } PIDLDATA
, *LPPIDLDATA
;
45 #define PT_SHELLEXT 0x2E
46 #define PT_YAGUID 0x70
48 static BOOL
_ILIsSpecialFolder (LPCITEMIDLIST pidl
)
50 LPPIDLDATA lpPData
= (LPPIDLDATA
)&pidl
->mkid
.abID
;
53 ((lpPData
&& (PT_GUID
== lpPData
->type
|| PT_SHELLEXT
== lpPData
->type
||
54 PT_YAGUID
== lpPData
->type
)) || (pidl
&& pidl
->mkid
.cb
== 0x00)));
57 static BOOL
_ILIsDesktop (LPCITEMIDLIST pidl
)
59 return (pidl
&& pidl
->mkid
.cb
== 0x00);
63 HRESULT
GetDisplayName(LPCITEMIDLIST pidlDirectory
,TCHAR
*szDisplayName
,UINT cchMax
,DWORD uFlags
)
65 IShellFolder
*pShellFolder
= NULL
;
66 LPCITEMIDLIST pidlRelative
= NULL
;
70 if (pidlDirectory
== NULL
|| szDisplayName
== NULL
)
75 hr
= SHBindToParent(pidlDirectory
, IID_PPV_ARG(IShellFolder
, &pShellFolder
), &pidlRelative
);
79 hr
= pShellFolder
->GetDisplayNameOf(pidlRelative
,uFlags
,&str
);
82 hr
= StrRetToBuf(&str
,pidlDirectory
,szDisplayName
,cchMax
);
84 pShellFolder
->Release();
90 HRESULT WINAPI
CExplorerBand_Constructor(REFIID riid
, LPVOID
*ppv
)
93 return ShellObjectCreator
<CExplorerBand
>(riid
, ppv
);
100 This is a Windows hack, because shell event messages in Windows gives an
101 ill-formed PIDL stripped from useful data that parses incorrectly with SHGetFileInfo.
102 So we need to re-enumerate subfolders until we find one with the same name.
104 HRESULT
_ReparsePIDL(LPITEMIDLIST buggyPidl
, LPITEMIDLIST
*cleanPidl
)
107 CComPtr
<IShellFolder
> folder
;
108 CComPtr
<IPersistFolder2
> persist
;
109 CComPtr
<IEnumIDList
> pEnumIDList
;
110 LPITEMIDLIST childPidl
;
111 LPITEMIDLIST correctChild
;
112 LPITEMIDLIST correctParent
;
117 EnumFlags
= SHCONTF_FOLDERS
| SHCONTF_INCLUDEHIDDEN
;
118 hr
= SHBindToParent(buggyPidl
, IID_PPV_ARG(IShellFolder
, &folder
), (LPCITEMIDLIST
*)&childPidl
);
122 ERR("Can't bind to parent folder\n");
125 hr
= folder
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &persist
));
128 ERR("PIDL doesn't belong to the shell namespace, aborting\n");
132 hr
= persist
->GetCurFolder(&correctParent
);
135 ERR("Unable to get current folder\n");
139 hr
= folder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
140 // avoid broken IShellFolder implementations that return null pointer with success
141 if (!SUCCEEDED(hr
) || !pEnumIDList
)
143 ERR("Can't enum the folder !\n");
147 while(SUCCEEDED(pEnumIDList
->Next(1, &correctChild
, &fetched
)) && correctChild
&& fetched
)
149 if (!folder
->CompareIDs(0, childPidl
, correctChild
))
151 *cleanPidl
= ILCombine(correctParent
, correctChild
);
152 ILFree(correctChild
);
155 ILFree(correctChild
);
158 ILFree(correctParent
);
162 CExplorerBand::CExplorerBand() :
163 pSite(NULL
), fVisible(FALSE
), bNavigating(FALSE
), dwBandID(0), pidlCurrent(NULL
)
167 CExplorerBand::~CExplorerBand()
175 void CExplorerBand::InitializeExplorerBand()
177 // Init the treeview here
180 CComPtr
<IWebBrowser2
> browserService
;
181 SHChangeNotifyEntry shcne
;
183 hr
= SHGetDesktopFolder(&pDesktop
);
184 if (FAILED_UNEXPECTEDLY(hr
))
187 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidl
);
188 if (FAILED_UNEXPECTEDLY(hr
))
192 hr
= SHGetImageList(SHIL_SMALL
, IID_PPV_ARG(IImageList
, &piml
));
193 if (FAILED_UNEXPECTEDLY(hr
))
196 TreeView_SetImageList(m_hWnd
, (HIMAGELIST
)piml
, TVSIL_NORMAL
);
198 // Insert the root node
199 hRoot
= InsertItem(0, pDesktop
, pidl
, pidl
, FALSE
);
202 ERR("Failed to create root item\n");
206 NodeInfo
* pNodeInfo
= GetNodeInfo(hRoot
);
208 // Insert child nodes
209 InsertSubitems(hRoot
, pNodeInfo
);
210 TreeView_Expand(m_hWnd
, hRoot
, TVE_EXPAND
);
212 // Navigate to current folder position
213 NavigateToCurrentFolder();
215 // Register shell notification
217 shcne
.fRecursive
= TRUE
;
218 shellRegID
= SHChangeNotifyRegister(
220 SHCNRF_ShellLevel
| SHCNRF_InterruptLevel
| SHCNRF_RecursiveInterrupt
,
221 SHCNE_DISKEVENTS
| SHCNE_RENAMEFOLDER
| SHCNE_RMDIR
| SHCNE_MKDIR
,
227 ERR("Something went wrong, error %08x\n", GetLastError());
229 // Register browser connection endpoint
230 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
231 if (FAILED_UNEXPECTEDLY(hr
))
234 hr
= AtlAdvise(browserService
, dynamic_cast<IDispatch
*>(this), DIID_DWebBrowserEvents
, &adviseCookie
);
235 if (FAILED_UNEXPECTEDLY(hr
))
241 void CExplorerBand::DestroyExplorerBand()
244 CComPtr
<IWebBrowser2
> browserService
;
246 TRACE("Cleaning up explorer band ...\n");
248 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
249 if (FAILED_UNEXPECTEDLY(hr
))
252 hr
= AtlUnadvise(browserService
, DIID_DWebBrowserEvents
, adviseCookie
);
253 /* Remove all items of the treeview */
254 RevokeDragDrop(m_hWnd
);
255 TreeView_DeleteAllItems(m_hWnd
);
258 TRACE("Cleanup done !\n");
261 CExplorerBand::NodeInfo
* CExplorerBand::GetNodeInfo(HTREEITEM hItem
)
265 tvItem
.mask
= TVIF_PARAM
;
266 tvItem
.hItem
= hItem
;
268 if (!TreeView_GetItem(m_hWnd
, &tvItem
))
271 return reinterpret_cast<NodeInfo
*>(tvItem
.lParam
);
274 HRESULT
CExplorerBand::ExecuteCommand(CComPtr
<IContextMenu
>& menu
, UINT nCmd
)
276 CComPtr
<IOleWindow
> pBrowserOleWnd
;
277 CMINVOKECOMMANDINFO cmi
;
281 hr
= IUnknown_QueryService(pSite
, SID_SShellBrowser
, IID_PPV_ARG(IOleWindow
, &pBrowserOleWnd
));
282 if (FAILED_UNEXPECTEDLY(hr
))
285 hr
= pBrowserOleWnd
->GetWindow(&browserWnd
);
286 if (FAILED_UNEXPECTEDLY(hr
))
289 ZeroMemory(&cmi
, sizeof(cmi
));
290 cmi
.cbSize
= sizeof(cmi
);
291 cmi
.lpVerb
= MAKEINTRESOURCEA(nCmd
);
292 cmi
.hwnd
= browserWnd
;
293 if (GetKeyState(VK_SHIFT
) & 0x8000)
294 cmi
.fMask
|= CMIC_MASK_SHIFT_DOWN
;
295 if (GetKeyState(VK_CONTROL
) & 0x8000)
296 cmi
.fMask
|= CMIC_MASK_CONTROL_DOWN
;
298 return menu
->InvokeCommand(&cmi
);
301 HRESULT
CExplorerBand::UpdateBrowser(LPITEMIDLIST pidlGoto
)
303 CComPtr
<IShellBrowser
> pBrowserService
;
306 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IShellBrowser
, &pBrowserService
));
307 if (FAILED_UNEXPECTEDLY(hr
))
310 hr
= pBrowserService
->BrowseObject(pidlGoto
, SBSP_SAMEBROWSER
| SBSP_ABSOLUTE
);
311 if (FAILED_UNEXPECTEDLY(hr
))
317 pidlCurrent
= ILClone(pidlGoto
);
322 // *** notifications handling ***
323 BOOL
CExplorerBand::OnTreeItemExpanding(LPNMTREEVIEW pnmtv
)
327 if (pnmtv
->action
== TVE_COLLAPSE
) {
328 if (pnmtv
->itemNew
.hItem
== hRoot
)
330 // Prenvent root from collapsing
331 pnmtv
->itemNew
.mask
|= TVIF_STATE
;
332 pnmtv
->itemNew
.stateMask
|= TVIS_EXPANDED
;
333 pnmtv
->itemNew
.state
&= ~TVIS_EXPANDED
;
334 pnmtv
->action
= TVE_EXPAND
;
338 if (pnmtv
->action
== TVE_EXPAND
) {
339 // Grab our directory PIDL
340 pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
341 // We have it, let's try
342 if (pNodeInfo
&& !pNodeInfo
->expanded
)
343 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, pNodeInfo
)) {
344 // remove subitem "+" since we failed to add subitems
347 tvItem
.mask
= TVIF_CHILDREN
;
348 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
349 tvItem
.cChildren
= 0;
351 TreeView_SetItem(m_hWnd
, &tvItem
);
357 BOOL
CExplorerBand::OnTreeItemDeleted(LPNMTREEVIEW pnmtv
)
359 /* Destroy memory associated to our node */
360 NodeInfo
* ptr
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
363 ILFree(ptr
->relativePidl
);
364 ILFree(ptr
->absolutePidl
);
370 void CExplorerBand::OnSelectionChanged(LPNMTREEVIEW pnmtv
)
372 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
374 /* Prevents navigation if selection is initiated inside the band */
378 UpdateBrowser(pNodeInfo
->absolutePidl
);
382 //TreeView_Expand(m_hWnd, pnmtv->itemNew.hItem, TVE_EXPAND);
385 void CExplorerBand::OnTreeItemDragging(LPNMTREEVIEW pnmtv
, BOOL isRightClick
)
387 CComPtr
<IShellFolder
> pSrcFolder
;
388 CComPtr
<IDataObject
> pObj
;
394 dwEffect
= DROPEFFECT_COPY
| DROPEFFECT_MOVE
;
395 if (!pnmtv
->itemNew
.lParam
)
397 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
398 hr
= SHBindToParent(pNodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pSrcFolder
), &pLast
);
401 hr
= pSrcFolder
->GetUIObjectOf(m_hWnd
, 1, &pLast
, IID_IDataObject
, 0, reinterpret_cast<void**>(&pObj
));
404 DoDragDrop(pObj
, this, dwEffect
, &dwEffect2
);
409 // *** ATL event handlers ***
410 LRESULT
CExplorerBand::OnContextMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
417 CComPtr
<IShellFolder
> pFolder
;
418 CComPtr
<IContextMenu
> contextMenu
;
421 LPITEMIDLIST pidlChild
;
424 item
= TreeView_GetSelection(m_hWnd
);
433 if (x
== -1 && y
== -1)
435 // TODO: grab position of tree item and position it correctly
438 info
= GetNodeInfo(item
);
441 ERR("No node data, something has gone wrong !\n");
444 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pFolder
),
445 (LPCITEMIDLIST
*)&pidlChild
);
448 ERR("Can't bind to folder!\n");
451 hr
= pFolder
->GetUIObjectOf(m_hWnd
, 1, (LPCITEMIDLIST
*)&pidlChild
, IID_IContextMenu
,
452 NULL
, reinterpret_cast<void**>(&contextMenu
));
455 ERR("Can't get IContextMenu interface\n");
458 treeMenu
= CreatePopupMenu();
459 hr
= contextMenu
->QueryContextMenu(treeMenu
, 0, FCIDM_SHVIEWFIRST
, FCIDM_SHVIEWLAST
,
463 WARN("Can't get context menu for item\n");
464 DestroyMenu(treeMenu
);
467 uCommand
= TrackPopupMenu(treeMenu
, TPM_LEFTALIGN
| TPM_RETURNCMD
| TPM_LEFTBUTTON
| TPM_RIGHTBUTTON
,
468 x
, y
, 0, m_hWnd
, NULL
);
470 ExecuteCommand(contextMenu
, uCommand
);
473 DestroyMenu(treeMenu
);
475 TreeView_SelectItem(m_hWnd
, oldSelected
);
480 LRESULT
CExplorerBand::ContextMenuHack(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
483 if (uMsg
== WM_RBUTTONDOWN
)
486 info
.pt
.x
= LOWORD(lParam
);
487 info
.pt
.y
= HIWORD(lParam
);
488 info
.flags
= TVHT_ONITEM
;
491 // Save the current location
492 oldSelected
= TreeView_GetSelection(m_hWnd
);
494 // Move to the item selected by the treeview (don't change right pane)
495 TreeView_HitTest(m_hWnd
, &info
);
497 TreeView_SelectItem(m_hWnd
, info
.hItem
);
500 return FALSE
; /* let the wndproc process the message */
503 LRESULT
CExplorerBand::OnShellEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
509 dest
= (LPITEMIDLIST
*)wParam
;
510 /* TODO: handle shell notifications */
511 switch(lParam
& ~SHCNE_INTERRUPT
)
514 if (!SUCCEEDED(_ReparsePIDL(dest
[0], &clean
)))
516 ERR("Can't reparse PIDL to a valid one\n");
519 NavigateToPIDL(clean
, &pItem
, FALSE
, TRUE
, FALSE
);
525 case SHCNE_RENAMEFOLDER
:
526 if (!SUCCEEDED(_ReparsePIDL(dest
[1], &clean
)))
528 ERR("Can't reparse PIDL to a valid one\n");
531 if (NavigateToPIDL(dest
[0], &pItem
, FALSE
, FALSE
, FALSE
))
532 RenameItem(pItem
, clean
);
535 case SHCNE_UPDATEDIR
:
536 // We don't take care of this message
537 TRACE("Directory updated\n");
540 TRACE("Unhandled message\n");
545 LRESULT
CExplorerBand::OnSetFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
548 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), TRUE
);
553 LRESULT
CExplorerBand::OnKillFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
555 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), FALSE
);
560 // *** Helper functions ***
561 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, IShellFolder
*psfParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
563 TV_INSERTSTRUCT tvInsert
;
564 HTREEITEM htiCreated
;
566 /* Get the attributes of the node */
567 SFGAOF attrs
= SFGAO_STREAM
| SFGAO_HASSUBFOLDER
;
568 HRESULT hr
= psfParent
->GetAttributesOf(1, &pEltRelative
, &attrs
);
569 if (FAILED_UNEXPECTEDLY(hr
))
573 if ((attrs
& SFGAO_STREAM
))
575 TRACE("Ignoring stream\n");
579 /* Get the name of the node */
580 WCHAR wszDisplayName
[MAX_PATH
];
581 if (!ILGetDisplayNameEx(psfParent
, pElt
, wszDisplayName
, ILGDN_INFOLDER
))
583 ERR("Failed to get node name\n");
587 /* Get the icon of the node */
588 INT iIcon
= SHMapPIDLToSystemImageListIndex(psfParent
, pEltRelative
, NULL
);
590 NodeInfo
* pChildInfo
= new NodeInfo
;
593 ERR("Failed to allocate NodeInfo\n");
597 // Store our node info
598 pChildInfo
->absolutePidl
= ILClone(pElt
);
599 pChildInfo
->relativePidl
= ILClone(pEltRelative
);
600 pChildInfo
->expanded
= FALSE
;
602 // Set up our treeview template
603 tvInsert
.hParent
= hParent
;
604 tvInsert
.hInsertAfter
= TVI_LAST
;
605 tvInsert
.item
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
606 tvInsert
.item
.cchTextMax
= MAX_PATH
;
607 tvInsert
.item
.pszText
= wszDisplayName
;
608 tvInsert
.item
.iImage
= tvInsert
.item
.iSelectedImage
= iIcon
;
609 tvInsert
.item
.cChildren
= (attrs
& SFGAO_HASSUBFOLDER
) ? 1 : 0;
610 tvInsert
.item
.lParam
= (LPARAM
)pChildInfo
;
612 htiCreated
= TreeView_InsertItem(m_hWnd
, &tvInsert
);
616 TVSORTCB sortCallback
;
617 sortCallback
.hParent
= hParent
;
618 sortCallback
.lpfnCompare
= CompareTreeItems
;
619 sortCallback
.lParam
= (LPARAM
)this;
620 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
626 /* This is the slow version of the above method */
627 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
629 CComPtr
<IShellFolder
> psfFolder
;
630 HRESULT hr
= SHBindToParent(pElt
, IID_PPV_ARG(IShellFolder
, &psfFolder
), NULL
);
631 if (FAILED_UNEXPECTEDLY(hr
))
634 return InsertItem(hParent
, psfFolder
, pElt
, pEltRelative
, bSort
);
637 BOOL
CExplorerBand::InsertSubitems(HTREEITEM hItem
, NodeInfo
*pNodeInfo
)
639 CComPtr
<IEnumIDList
> pEnumIDList
;
640 LPITEMIDLIST pidlSub
;
646 CComPtr
<IShellFolder
> pFolder
;
647 TVSORTCB sortCallback
;
649 entry
= pNodeInfo
->absolutePidl
;
652 EnumFlags
= SHCONTF_FOLDERS
;
654 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidlSub
);
657 ERR("Can't get desktop PIDL !\n");
661 if (!pDesktop
->CompareIDs(NULL
, pidlSub
, entry
))
663 // We are the desktop, so use pDesktop as pFolder
668 // Get an IShellFolder of our pidl
669 hr
= pDesktop
->BindToObject(entry
, NULL
, IID_PPV_ARG(IShellFolder
, &pFolder
));
673 ERR("Can't bind folder to desktop !\n");
679 // TODO: handle hidden folders according to settings !
680 EnumFlags
|= SHCONTF_INCLUDEHIDDEN
;
682 // Enum through objects
683 hr
= pFolder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
685 // avoid broken IShellFolder implementations that return null pointer with success
686 if (!SUCCEEDED(hr
) || !pEnumIDList
)
688 ERR("Can't enum the folder !\n");
692 /* Don't redraw while we add stuff into the tree */
693 SendMessage(WM_SETREDRAW
, FALSE
, 0);
694 while(SUCCEEDED(pEnumIDList
->Next(1, &pidlSub
, &fetched
)) && pidlSub
&& fetched
)
696 LPITEMIDLIST pidlSubComplete
;
697 pidlSubComplete
= ILCombine(entry
, pidlSub
);
699 if (InsertItem(hItem
, pFolder
, pidlSubComplete
, pidlSub
, FALSE
))
701 ILFree(pidlSubComplete
);
704 pNodeInfo
->expanded
= TRUE
;
705 /* Let's do sorting */
706 sortCallback
.hParent
= hItem
;
707 sortCallback
.lpfnCompare
= CompareTreeItems
;
708 sortCallback
.lParam
= (LPARAM
)this;
709 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
711 /* Now we can redraw */
712 SendMessage(WM_SETREDRAW
, TRUE
, 0);
714 return (uItemCount
> 0) ? TRUE
: FALSE
;
718 * Navigate to a given PIDL in the treeview, and return matching tree item handle
719 * - dest: The absolute PIDL we should navigate in the treeview
720 * - item: Handle of the tree item matching the PIDL
721 * - bExpand: expand collapsed nodes in order to find the right element
722 * - bInsert: insert the element at the right place if we don't find it
723 * - bSelect: select the item after we found it
725 BOOL
CExplorerBand::NavigateToPIDL(LPITEMIDLIST dest
, HTREEITEM
*item
, BOOL bExpand
, BOOL bInsert
,
733 LPITEMIDLIST relativeChild
;
744 nodeData
= GetNodeInfo(current
);
747 ERR("Something has gone wrong, no data associated to node !\n");
751 // If we found our node, give it back
752 if (!pDesktop
->CompareIDs(0, nodeData
->absolutePidl
, dest
))
755 TreeView_SelectItem(m_hWnd
, current
);
760 // Check if we are a parent of the requested item
761 relativeChild
= ILFindChild(nodeData
->absolutePidl
, dest
);
762 if (relativeChild
!= 0)
764 // Notify treeview we have children
765 tvItem
.mask
= TVIF_CHILDREN
;
766 tvItem
.hItem
= current
;
767 tvItem
.cChildren
= 1;
768 TreeView_SetItem(m_hWnd
, &tvItem
);
770 // If we can expand and the node isn't expanded yet, do it
773 if (!nodeData
->expanded
)
774 InsertSubitems(current
, nodeData
);
775 TreeView_Expand(m_hWnd
, current
, TVE_EXPAND
);
778 // Try to get a child
779 tmp
= TreeView_GetChild(m_hWnd
, current
);
782 // We have a child, let's continue with it
788 if (bInsert
&& nodeData
->expanded
)
790 // Happens when we have to create a subchild inside a child
791 current
= InsertItem(current
, dest
, relativeChild
, TRUE
);
793 // We end up here, without any children, so we found nothing
794 // Tell the parent node it has children
795 ZeroMemory(&tvItem
, sizeof(tvItem
));
801 tmp
= TreeView_GetNextSibling(m_hWnd
, current
);
809 current
= InsertItem(parent
, dest
, ILFindLastID(dest
), TRUE
);
819 BOOL
CExplorerBand::NavigateToCurrentFolder()
821 LPITEMIDLIST explorerPidl
;
822 CComPtr
<IBrowserService
> pBrowserService
;
828 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IBrowserService
, &pBrowserService
));
831 ERR("Can't get IBrowserService !\n");
835 hr
= pBrowserService
->GetPidl(&explorerPidl
);
836 if (!SUCCEEDED(hr
) || !explorerPidl
)
838 ERR("Unable to get browser PIDL !\n");
842 /* find PIDL into our explorer */
843 result
= NavigateToPIDL(explorerPidl
, &dummy
, TRUE
, FALSE
, TRUE
);
848 BOOL
CExplorerBand::DeleteItem(LPITEMIDLIST idl
)
852 HTREEITEM parentNode
;
854 if (!NavigateToPIDL(idl
, &toDelete
, FALSE
, FALSE
, FALSE
))
857 // TODO: check that the treeview item is really deleted
859 parentNode
= TreeView_GetParent(m_hWnd
, toDelete
);
860 // Navigate to parent when deleting child item
861 if (!pDesktop
->CompareIDs(0, idl
, pidlCurrent
))
863 TreeView_SelectItem(m_hWnd
, parentNode
);
866 // Remove the child item
867 TreeView_DeleteItem(m_hWnd
, toDelete
);
868 // Probe parent to see if it has children
869 if (!TreeView_GetChild(m_hWnd
, parentNode
))
871 // Decrement parent's child count
872 ZeroMemory(&tvItem
, sizeof(tvItem
));
873 tvItem
.mask
= TVIF_CHILDREN
;
874 tvItem
.hItem
= parentNode
;
875 tvItem
.cChildren
= 0;
876 TreeView_SetItem(m_hWnd
, &tvItem
);
881 BOOL
CExplorerBand::RenameItem(HTREEITEM toRename
, LPITEMIDLIST newPidl
)
883 WCHAR wszDisplayName
[MAX_PATH
];
885 LPCITEMIDLIST relPidl
;
887 TVSORTCB sortCallback
;
890 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
891 itemInfo
.mask
= TVIF_PARAM
;
892 itemInfo
.hItem
= toRename
;
894 // Change PIDL associated to the item
895 relPidl
= ILFindLastID(newPidl
);
896 TreeView_GetItem(m_hWnd
, &itemInfo
);
897 if (!itemInfo
.lParam
)
899 ERR("Unable to fetch lParam\n");
902 SendMessage(WM_SETREDRAW
, FALSE
, 0);
903 treeInfo
= (NodeInfo
*)itemInfo
.lParam
;
904 ILFree(treeInfo
->absolutePidl
);
905 ILFree(treeInfo
->relativePidl
);
906 treeInfo
->absolutePidl
= ILClone(newPidl
);
907 treeInfo
->relativePidl
= ILClone(relPidl
);
909 // Change the display name
910 GetDisplayName(newPidl
, wszDisplayName
, MAX_PATH
, SHGDN_INFOLDER
);
911 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
912 itemInfo
.hItem
= toRename
;
913 itemInfo
.mask
= TVIF_TEXT
;
914 itemInfo
.pszText
= wszDisplayName
;
915 TreeView_SetItem(m_hWnd
, &itemInfo
);
917 if((child
= TreeView_GetChild(m_hWnd
, toRename
)) != NULL
)
919 RefreshTreePidl(child
, newPidl
);
923 sortCallback
.hParent
= TreeView_GetParent(m_hWnd
, toRename
);
924 sortCallback
.lpfnCompare
= CompareTreeItems
;
925 sortCallback
.lParam
= (LPARAM
)this;
926 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
927 SendMessage(WM_SETREDRAW
, TRUE
, 0);
931 BOOL
CExplorerBand::RefreshTreePidl(HTREEITEM tree
, LPITEMIDLIST pidlParent
)
936 // Update our node data
937 pInfo
= GetNodeInfo(tree
);
940 WARN("No tree info !\n");
943 ILFree(pInfo
->absolutePidl
);
944 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
945 if (!pInfo
->absolutePidl
)
947 WARN("PIDL allocation failed\n");
950 // Recursively update children
951 if ((tmp
= TreeView_GetChild(m_hWnd
, tree
)) != NULL
)
953 RefreshTreePidl(tmp
, pInfo
->absolutePidl
);
956 tmp
= TreeView_GetNextSibling(m_hWnd
, tree
);
959 pInfo
= GetNodeInfo(tmp
);
962 WARN("No tree info !\n");
965 ILFree(pInfo
->absolutePidl
);
966 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
967 tmp
= TreeView_GetNextSibling(m_hWnd
, tmp
);
972 // *** Tree item sorting callback ***
973 int CALLBACK
CExplorerBand::CompareTreeItems(LPARAM p1
, LPARAM p2
, LPARAM p3
)
976 * We first sort drive letters (Path root), then PIDLs and then regular folder
978 * This is not how Windows sorts item, but it gives decent results.
982 CExplorerBand
*pThis
;
983 WCHAR wszFolder1
[MAX_PATH
];
984 WCHAR wszFolder2
[MAX_PATH
];
986 info1
= (NodeInfo
*)p1
;
987 info2
= (NodeInfo
*)p2
;
988 pThis
= (CExplorerBand
*)p3
;
990 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_FORPARSING
);
991 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_FORPARSING
);
992 if (PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
994 return lstrcmpiW(wszFolder1
,wszFolder2
);
996 if (PathIsRoot(wszFolder1
) && !PathIsRoot(wszFolder2
))
1000 if (!PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
1004 // Now, we compare non-root folders, grab display name
1005 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_INFOLDER
);
1006 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_INFOLDER
);
1008 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1012 if (!_ILIsSpecialFolder(info1
->relativePidl
) && _ILIsSpecialFolder(info2
->relativePidl
))
1016 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1019 hr
= pThis
->pDesktop
->CompareIDs(0, info1
->absolutePidl
, info2
->absolutePidl
);
1021 return (hr
> 0) ? -1 : 1;
1023 return StrCmpLogicalW(wszFolder1
, wszFolder2
);
1026 // *** IOleWindow methods ***
1027 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetWindow(HWND
*lphwnd
)
1030 return E_INVALIDARG
;
1035 HRESULT STDMETHODCALLTYPE
CExplorerBand::ContextSensitiveHelp(BOOL fEnterMode
)
1042 // *** IDockingWindow methods ***
1043 HRESULT STDMETHODCALLTYPE
CExplorerBand::CloseDW(DWORD dwReserved
)
1045 // We do nothing, we don't have anything to save yet
1046 TRACE("CloseDW called\n");
1050 HRESULT STDMETHODCALLTYPE
CExplorerBand::ResizeBorderDW(const RECT
*prcBorder
, IUnknown
*punkToolbarSite
, BOOL fReserved
)
1052 /* Must return E_NOTIMPL according to MSDN */
1056 HRESULT STDMETHODCALLTYPE
CExplorerBand::ShowDW(BOOL fShow
)
1064 // *** IDeskBand methods ***
1065 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetBandInfo(DWORD dwBandID
, DWORD dwViewMode
, DESKBANDINFO
*pdbi
)
1069 return E_INVALIDARG
;
1071 this->dwBandID
= dwBandID
;
1073 if (pdbi
->dwMask
& DBIM_MINSIZE
)
1075 pdbi
->ptMinSize
.x
= 200;
1076 pdbi
->ptMinSize
.y
= 30;
1079 if (pdbi
->dwMask
& DBIM_MAXSIZE
)
1081 pdbi
->ptMaxSize
.y
= -1;
1084 if (pdbi
->dwMask
& DBIM_INTEGRAL
)
1086 pdbi
->ptIntegral
.y
= 1;
1089 if (pdbi
->dwMask
& DBIM_ACTUAL
)
1091 pdbi
->ptActual
.x
= 200;
1092 pdbi
->ptActual
.y
= 30;
1095 if (pdbi
->dwMask
& DBIM_TITLE
)
1097 if (!LoadStringW(_AtlBaseModule
.GetResourceInstance(), IDS_FOLDERSLABEL
, pdbi
->wszTitle
, _countof(pdbi
->wszTitle
)))
1098 return HRESULT_FROM_WIN32(GetLastError());
1101 if (pdbi
->dwMask
& DBIM_MODEFLAGS
)
1103 pdbi
->dwModeFlags
= DBIMF_NORMAL
| DBIMF_VARIABLEHEIGHT
;
1106 if (pdbi
->dwMask
& DBIM_BKCOLOR
)
1108 pdbi
->dwMask
&= ~DBIM_BKCOLOR
;
1114 // *** IObjectWithSite methods ***
1115 HRESULT STDMETHODCALLTYPE
CExplorerBand::SetSite(IUnknown
*pUnkSite
)
1120 if (pUnkSite
== pSite
)
1123 TRACE("SetSite called \n");
1126 DestroyExplorerBand();
1131 if (pUnkSite
!= pSite
)
1139 hr
= IUnknown_GetWindow(pUnkSite
, &parentWnd
);
1142 ERR("Could not get parent's window ! Status: %08lx\n", hr
);
1143 return E_INVALIDARG
;
1150 // Change its parent
1151 SetParent(parentWnd
);
1155 HWND wnd
= CreateWindow(WC_TREEVIEW
, NULL
,
1156 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
1157 0, 0, 0, 0, parentWnd
, NULL
, _AtlBaseModule
.GetModuleInstance(), NULL
);
1159 // Subclass the window
1160 SubclassWindow(wnd
);
1162 // Initialize our treeview now
1163 InitializeExplorerBand();
1164 RegisterDragDrop(m_hWnd
, dynamic_cast<IDropTarget
*>(this));
1169 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSite(REFIID riid
, void **ppvSite
)
1178 // *** IOleCommandTarget methods ***
1179 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
1185 HRESULT STDMETHODCALLTYPE
CExplorerBand::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
1192 // *** IServiceProvider methods ***
1193 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
1200 // *** IInputObject methods ***
1201 HRESULT STDMETHODCALLTYPE
CExplorerBand::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
1208 // TODO: handle message
1211 TranslateMessage(lpMsg
);
1212 DispatchMessage(lpMsg
);
1217 HRESULT STDMETHODCALLTYPE
CExplorerBand::HasFocusIO()
1219 return bFocused
? S_OK
: S_FALSE
;
1222 HRESULT STDMETHODCALLTYPE
CExplorerBand::TranslateAcceleratorIO(LPMSG lpMsg
)
1224 if (lpMsg
->hwnd
== m_hWnd
)
1226 TranslateMessage(lpMsg
);
1227 DispatchMessage(lpMsg
);
1234 // *** IPersist methods ***
1235 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetClassID(CLSID
*pClassID
)
1239 memcpy(pClassID
, &CLSID_ExplorerBand
, sizeof(CLSID
));
1244 // *** IPersistStream methods ***
1245 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsDirty()
1251 HRESULT STDMETHODCALLTYPE
CExplorerBand::Load(IStream
*pStm
)
1257 HRESULT STDMETHODCALLTYPE
CExplorerBand::Save(IStream
*pStm
, BOOL fClearDirty
)
1263 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
1265 // TODO: calculate max size
1271 // *** IWinEventHandler methods ***
1272 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
1275 if (uMsg
== WM_NOTIFY
)
1277 NMHDR
*pNotifyHeader
= (NMHDR
*)lParam
;
1278 switch (pNotifyHeader
->code
)
1280 case TVN_ITEMEXPANDING
:
1281 *theResult
= OnTreeItemExpanding((LPNMTREEVIEW
)lParam
);
1283 case TVN_SELCHANGED
:
1284 OnSelectionChanged((LPNMTREEVIEW
)lParam
);
1286 case TVN_DELETEITEM
:
1287 OnTreeItemDeleted((LPNMTREEVIEW
)lParam
);
1290 OnContextMenu(WM_CONTEXTMENU
, (WPARAM
)m_hWnd
, GetMessagePos(), bHandled
);
1294 case TVN_BEGINRDRAG
:
1295 OnTreeItemDragging((LPNMTREEVIEW
)lParam
, pNotifyHeader
->code
== TVN_BEGINRDRAG
);
1296 case TVN_BEGINLABELEDITW
:
1298 // TODO: put this in a function ? (mostly copypasta from CDefView)
1299 DWORD dwAttr
= SFGAO_CANRENAME
;
1300 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1301 CComPtr
<IShellFolder
> pParent
;
1302 LPCITEMIDLIST pChild
;
1306 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1309 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pChild
);
1310 if (!SUCCEEDED(hr
) || !pParent
.p
)
1313 hr
= pParent
->GetAttributesOf(1, &pChild
, &dwAttr
);
1314 if (SUCCEEDED(hr
) && (dwAttr
& SFGAO_CANRENAME
))
1318 case TVN_ENDLABELEDITW
:
1320 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1321 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1325 if (dispInfo
->item
.pszText
)
1327 LPITEMIDLIST pidlNew
;
1328 CComPtr
<IShellFolder
> pParent
;
1329 LPCITEMIDLIST pidlChild
;
1331 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pidlChild
);
1332 if (!SUCCEEDED(hr
) || !pParent
.p
)
1335 hr
= pParent
->SetNameOf(0, pidlChild
, dispInfo
->item
.pszText
, SHGDN_INFOLDER
, &pidlNew
);
1336 if(SUCCEEDED(hr
) && pidlNew
)
1338 CComPtr
<IPersistFolder2
> pPersist
;
1339 LPITEMIDLIST pidlParent
, pidlNewAbs
;
1341 hr
= pParent
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &pPersist
));
1345 hr
= pPersist
->GetCurFolder(&pidlParent
);
1348 pidlNewAbs
= ILCombine(pidlParent
, pidlNew
);
1350 // Navigate to our new location
1351 UpdateBrowser(pidlNewAbs
);
1367 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsWindowOwner(HWND hWnd
)
1369 return (hWnd
== m_hWnd
) ? S_OK
: S_FALSE
;
1372 // *** IBandNavigate methods ***
1373 HRESULT STDMETHODCALLTYPE
CExplorerBand::Select(long paramC
)
1379 // *** INamespaceProxy ***
1380 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetNavigateTarget(long paramC
, long param10
, long param14
)
1386 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(long paramC
)
1392 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnSelectionChanged(long paramC
)
1398 HRESULT STDMETHODCALLTYPE
CExplorerBand::RefreshFlags(long paramC
, long param10
, long param14
)
1404 HRESULT STDMETHODCALLTYPE
CExplorerBand::CacheItem(long paramC
)
1410 // *** IDispatch methods ***
1411 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfoCount(UINT
*pctinfo
)
1417 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfo(UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
1423 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetIDsOfNames(REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
1429 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1431 switch (dispIdMember
)
1433 case DISPID_DOWNLOADCOMPLETE
:
1434 case DISPID_NAVIGATECOMPLETE2
:
1435 TRACE("DISPID_NAVIGATECOMPLETE2 received\n");
1436 NavigateToCurrentFolder();
1439 TRACE("Unknown dispid requested: %08x\n", dispIdMember
);
1440 return E_INVALIDARG
;
1443 // *** IDropTarget methods ***
1444 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragEnter(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1446 ERR("Entering drag\n");
1448 oldSelected
= TreeView_GetSelection(m_hWnd
);
1449 return DragOver(glfKeyState
, pt
, pdwEffect
);
1452 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragOver(DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1455 CComPtr
<IShellFolder
> pShellFldr
;
1457 //LPCITEMIDLIST pChild;
1462 info
.flags
= TVHT_ONITEM
;
1464 ScreenToClient(&info
.pt
);
1466 // Move to the item selected by the treeview (don't change right pane)
1467 TreeView_HitTest(m_hWnd
, &info
);
1472 TreeView_SelectItem(m_hWnd
, info
.hItem
);
1473 bNavigating
= FALSE
;
1474 // Delegate to shell folder
1475 if (pDropTarget
&& info
.hItem
!= childTargetNode
)
1479 if (info
.hItem
!= childTargetNode
)
1481 nodeInfo
= GetNodeInfo(info
.hItem
);
1485 hr
= SHBindToParent(nodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pShellFldr
), &pChild
);
1488 hr
= pShellFldr
->GetUIObjectOf(m_hWnd
, 1, &pChild
, IID_IDropTarget
, NULL
, reinterpret_cast<void**>(&pDropTarget
));
1492 if(_ILIsDesktop(nodeInfo
->absolutePidl
))
1493 pShellFldr
= pDesktop
;
1496 hr
= pDesktop
->BindToObject(nodeInfo
->absolutePidl
, 0, IID_PPV_ARG(IShellFolder
, &pShellFldr
));
1499 /* Don't allow dnd since we couldn't get our folder object */
1500 ERR("Can't bind to folder object\n");
1501 *pdwEffect
= DROPEFFECT_NONE
;
1505 hr
= pShellFldr
->CreateViewObject(m_hWnd
, IID_PPV_ARG(IDropTarget
, &pDropTarget
));
1508 /* Don't allow dnd since we couldn't get our drop target */
1509 ERR("Can't get drop target for folder object\n");
1510 *pdwEffect
= DROPEFFECT_NONE
;
1513 hr
= pDropTarget
->DragEnter(pCurObject
, glfKeyState
, pt
, pdwEffect
);
1514 childTargetNode
= info
.hItem
;
1518 hr
= pDropTarget
->DragOver(glfKeyState
, pt
, pdwEffect
);
1523 childTargetNode
= NULL
;
1525 *pdwEffect
= DROPEFFECT_NONE
;
1530 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragLeave()
1533 TreeView_SelectItem(m_hWnd
, oldSelected
);
1534 bNavigating
= FALSE
;
1535 childTargetNode
= NULL
;
1543 HRESULT STDMETHODCALLTYPE
CExplorerBand::Drop(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1547 pDropTarget
->Drop(pObj
, glfKeyState
, pt
, pdwEffect
);
1552 // *** IDropSource methods ***
1553 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
)
1556 return DRAGDROP_S_CANCEL
;
1557 if ((grfKeyState
& MK_LBUTTON
) || (grfKeyState
& MK_RBUTTON
))
1559 return DRAGDROP_S_DROP
;
1562 HRESULT STDMETHODCALLTYPE
CExplorerBand::GiveFeedback(DWORD dwEffect
)
1564 return DRAGDROP_S_USEDEFAULTCURSORS
;