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 HRESULT
GetDisplayName(LPCITEMIDLIST pidlDirectory
,TCHAR
*szDisplayName
,UINT cchMax
,DWORD uFlags
)
59 IShellFolder
*pShellFolder
= NULL
;
60 LPCITEMIDLIST pidlRelative
= NULL
;
64 if (pidlDirectory
== NULL
|| szDisplayName
== NULL
)
69 hr
= SHBindToParent(pidlDirectory
, IID_PPV_ARG(IShellFolder
, &pShellFolder
), &pidlRelative
);
73 hr
= pShellFolder
->GetDisplayNameOf(pidlRelative
,uFlags
,&str
);
76 hr
= StrRetToBuf(&str
,pidlDirectory
,szDisplayName
,cchMax
);
78 pShellFolder
->Release();
84 This is a Windows hack, because shell event messages in Windows gives an
85 ill-formed PIDL stripped from useful data that parses incorrectly with SHGetFileInfo.
86 So we need to re-enumerate subfolders until we find one with the same name.
88 HRESULT
_ReparsePIDL(LPITEMIDLIST buggyPidl
, LPITEMIDLIST
*cleanPidl
)
91 CComPtr
<IShellFolder
> folder
;
92 CComPtr
<IPersistFolder2
> persist
;
93 CComPtr
<IEnumIDList
> pEnumIDList
;
94 LPITEMIDLIST childPidl
;
95 LPITEMIDLIST correctChild
;
96 LPITEMIDLIST correctParent
;
101 EnumFlags
= SHCONTF_FOLDERS
| SHCONTF_INCLUDEHIDDEN
;
102 hr
= SHBindToParent(buggyPidl
, IID_PPV_ARG(IShellFolder
, &folder
), (LPCITEMIDLIST
*)&childPidl
);
106 ERR("Can't bind to parent folder\n");
109 hr
= folder
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &persist
));
112 ERR("PIDL doesn't belong to the shell namespace, aborting\n");
116 hr
= persist
->GetCurFolder(&correctParent
);
119 ERR("Unable to get current folder\n");
123 hr
= folder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
124 // avoid broken IShellFolder implementations that return null pointer with success
125 if (!SUCCEEDED(hr
) || !pEnumIDList
)
127 ERR("Can't enum the folder !\n");
131 while(SUCCEEDED(pEnumIDList
->Next(1, &correctChild
, &fetched
)) && correctChild
&& fetched
)
133 if (!folder
->CompareIDs(0, childPidl
, correctChild
))
135 *cleanPidl
= ILCombine(correctParent
, correctChild
);
136 ILFree(correctChild
);
139 ILFree(correctChild
);
142 ILFree(correctParent
);
146 CExplorerBand::CExplorerBand() :
147 pSite(NULL
), fVisible(FALSE
), bNavigating(FALSE
), dwBandID(0), pidlCurrent(NULL
)
151 CExplorerBand::~CExplorerBand()
159 void CExplorerBand::InitializeExplorerBand()
161 // Init the treeview here
164 CComPtr
<IWebBrowser2
> browserService
;
165 SHChangeNotifyEntry shcne
;
167 hr
= SHGetDesktopFolder(&pDesktop
);
168 if (FAILED_UNEXPECTEDLY(hr
))
171 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidl
);
172 if (FAILED_UNEXPECTEDLY(hr
))
176 hr
= SHGetImageList(SHIL_SMALL
, IID_PPV_ARG(IImageList
, &piml
));
177 if (FAILED_UNEXPECTEDLY(hr
))
180 TreeView_SetImageList(m_hWnd
, (HIMAGELIST
)piml
, TVSIL_NORMAL
);
182 // Insert the root node
183 hRoot
= InsertItem(0, pDesktop
, pidl
, pidl
, FALSE
);
186 ERR("Failed to create root item\n");
190 NodeInfo
* pNodeInfo
= GetNodeInfo(hRoot
);
192 // Insert child nodes
193 InsertSubitems(hRoot
, pNodeInfo
);
194 TreeView_Expand(m_hWnd
, hRoot
, TVE_EXPAND
);
196 // Navigate to current folder position
197 NavigateToCurrentFolder();
199 // Register shell notification
201 shcne
.fRecursive
= TRUE
;
202 shellRegID
= SHChangeNotifyRegister(
204 SHCNRF_ShellLevel
| SHCNRF_InterruptLevel
| SHCNRF_RecursiveInterrupt
,
205 SHCNE_DISKEVENTS
| SHCNE_RENAMEFOLDER
| SHCNE_RMDIR
| SHCNE_MKDIR
,
211 ERR("Something went wrong, error %08x\n", GetLastError());
213 // Register browser connection endpoint
214 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
215 if (FAILED_UNEXPECTEDLY(hr
))
218 hr
= AtlAdvise(browserService
, dynamic_cast<IDispatch
*>(this), DIID_DWebBrowserEvents
, &adviseCookie
);
219 if (FAILED_UNEXPECTEDLY(hr
))
225 void CExplorerBand::DestroyExplorerBand()
228 CComPtr
<IWebBrowser2
> browserService
;
230 TRACE("Cleaning up explorer band ...\n");
232 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
233 if (FAILED_UNEXPECTEDLY(hr
))
236 hr
= AtlUnadvise(browserService
, DIID_DWebBrowserEvents
, adviseCookie
);
237 /* Remove all items of the treeview */
238 RevokeDragDrop(m_hWnd
);
239 TreeView_DeleteAllItems(m_hWnd
);
242 TRACE("Cleanup done !\n");
245 CExplorerBand::NodeInfo
* CExplorerBand::GetNodeInfo(HTREEITEM hItem
)
249 tvItem
.mask
= TVIF_PARAM
;
250 tvItem
.hItem
= hItem
;
252 if (!TreeView_GetItem(m_hWnd
, &tvItem
))
255 return reinterpret_cast<NodeInfo
*>(tvItem
.lParam
);
258 HRESULT
CExplorerBand::ExecuteCommand(CComPtr
<IContextMenu
>& menu
, UINT nCmd
)
260 CComPtr
<IOleWindow
> pBrowserOleWnd
;
261 CMINVOKECOMMANDINFO cmi
;
265 hr
= IUnknown_QueryService(pSite
, SID_SShellBrowser
, IID_PPV_ARG(IOleWindow
, &pBrowserOleWnd
));
266 if (FAILED_UNEXPECTEDLY(hr
))
269 hr
= pBrowserOleWnd
->GetWindow(&browserWnd
);
270 if (FAILED_UNEXPECTEDLY(hr
))
273 ZeroMemory(&cmi
, sizeof(cmi
));
274 cmi
.cbSize
= sizeof(cmi
);
275 cmi
.lpVerb
= MAKEINTRESOURCEA(nCmd
);
276 cmi
.hwnd
= browserWnd
;
277 if (GetKeyState(VK_SHIFT
) & 0x8000)
278 cmi
.fMask
|= CMIC_MASK_SHIFT_DOWN
;
279 if (GetKeyState(VK_CONTROL
) & 0x8000)
280 cmi
.fMask
|= CMIC_MASK_CONTROL_DOWN
;
282 return menu
->InvokeCommand(&cmi
);
285 HRESULT
CExplorerBand::UpdateBrowser(LPITEMIDLIST pidlGoto
)
287 CComPtr
<IShellBrowser
> pBrowserService
;
290 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IShellBrowser
, &pBrowserService
));
291 if (FAILED_UNEXPECTEDLY(hr
))
294 hr
= pBrowserService
->BrowseObject(pidlGoto
, SBSP_SAMEBROWSER
| SBSP_ABSOLUTE
);
295 if (FAILED_UNEXPECTEDLY(hr
))
301 pidlCurrent
= ILClone(pidlGoto
);
306 // *** notifications handling ***
307 BOOL
CExplorerBand::OnTreeItemExpanding(LPNMTREEVIEW pnmtv
)
311 if (pnmtv
->action
== TVE_COLLAPSE
) {
312 if (pnmtv
->itemNew
.hItem
== hRoot
)
314 // Prenvent root from collapsing
315 pnmtv
->itemNew
.mask
|= TVIF_STATE
;
316 pnmtv
->itemNew
.stateMask
|= TVIS_EXPANDED
;
317 pnmtv
->itemNew
.state
&= ~TVIS_EXPANDED
;
318 pnmtv
->action
= TVE_EXPAND
;
322 if (pnmtv
->action
== TVE_EXPAND
) {
323 // Grab our directory PIDL
324 pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
325 // We have it, let's try
326 if (pNodeInfo
&& !pNodeInfo
->expanded
)
327 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, pNodeInfo
)) {
328 // remove subitem "+" since we failed to add subitems
331 tvItem
.mask
= TVIF_CHILDREN
;
332 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
333 tvItem
.cChildren
= 0;
335 TreeView_SetItem(m_hWnd
, &tvItem
);
341 BOOL
CExplorerBand::OnTreeItemDeleted(LPNMTREEVIEW pnmtv
)
343 /* Destroy memory associated to our node */
344 NodeInfo
* ptr
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
347 ILFree(ptr
->relativePidl
);
348 ILFree(ptr
->absolutePidl
);
354 void CExplorerBand::OnSelectionChanged(LPNMTREEVIEW pnmtv
)
356 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
358 /* Prevents navigation if selection is initiated inside the band */
362 UpdateBrowser(pNodeInfo
->absolutePidl
);
366 //TreeView_Expand(m_hWnd, pnmtv->itemNew.hItem, TVE_EXPAND);
369 void CExplorerBand::OnTreeItemDragging(LPNMTREEVIEW pnmtv
, BOOL isRightClick
)
371 CComPtr
<IShellFolder
> pSrcFolder
;
372 CComPtr
<IDataObject
> pObj
;
378 dwEffect
= DROPEFFECT_COPY
| DROPEFFECT_MOVE
;
379 if (!pnmtv
->itemNew
.lParam
)
381 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
382 hr
= SHBindToParent(pNodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pSrcFolder
), &pLast
);
385 hr
= pSrcFolder
->GetUIObjectOf(m_hWnd
, 1, &pLast
, IID_IDataObject
, 0, reinterpret_cast<void**>(&pObj
));
388 DoDragDrop(pObj
, this, dwEffect
, &dwEffect2
);
393 // *** ATL event handlers ***
394 LRESULT
CExplorerBand::OnContextMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
401 CComPtr
<IShellFolder
> pFolder
;
402 CComPtr
<IContextMenu
> contextMenu
;
405 LPITEMIDLIST pidlChild
;
408 item
= TreeView_GetSelection(m_hWnd
);
417 if (x
== -1 && y
== -1)
419 // TODO: grab position of tree item and position it correctly
422 info
= GetNodeInfo(item
);
425 ERR("No node data, something has gone wrong !\n");
428 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pFolder
),
429 (LPCITEMIDLIST
*)&pidlChild
);
432 ERR("Can't bind to folder!\n");
435 hr
= pFolder
->GetUIObjectOf(m_hWnd
, 1, (LPCITEMIDLIST
*)&pidlChild
, IID_IContextMenu
,
436 NULL
, reinterpret_cast<void**>(&contextMenu
));
439 ERR("Can't get IContextMenu interface\n");
443 IUnknown_SetSite(contextMenu
, (IDeskBand
*)this);
445 treeMenu
= CreatePopupMenu();
446 hr
= contextMenu
->QueryContextMenu(treeMenu
, 0, FCIDM_SHVIEWFIRST
, FCIDM_SHVIEWLAST
,
450 WARN("Can't get context menu for item\n");
451 DestroyMenu(treeMenu
);
454 uCommand
= TrackPopupMenu(treeMenu
, TPM_LEFTALIGN
| TPM_RETURNCMD
| TPM_LEFTBUTTON
| TPM_RIGHTBUTTON
,
455 x
, y
, 0, m_hWnd
, NULL
);
457 ExecuteCommand(contextMenu
, uCommand
);
461 IUnknown_SetSite(contextMenu
, NULL
);
463 DestroyMenu(treeMenu
);
465 TreeView_SelectItem(m_hWnd
, oldSelected
);
470 LRESULT
CExplorerBand::ContextMenuHack(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
473 if (uMsg
== WM_RBUTTONDOWN
)
476 info
.pt
.x
= LOWORD(lParam
);
477 info
.pt
.y
= HIWORD(lParam
);
478 info
.flags
= TVHT_ONITEM
;
481 // Save the current location
482 oldSelected
= TreeView_GetSelection(m_hWnd
);
484 // Move to the item selected by the treeview (don't change right pane)
485 TreeView_HitTest(m_hWnd
, &info
);
487 TreeView_SelectItem(m_hWnd
, info
.hItem
);
490 return FALSE
; /* let the wndproc process the message */
493 LRESULT
CExplorerBand::OnShellEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
499 dest
= (LPITEMIDLIST
*)wParam
;
500 /* TODO: handle shell notifications */
501 switch(lParam
& ~SHCNE_INTERRUPT
)
504 if (!SUCCEEDED(_ReparsePIDL(dest
[0], &clean
)))
506 ERR("Can't reparse PIDL to a valid one\n");
509 NavigateToPIDL(clean
, &pItem
, FALSE
, TRUE
, FALSE
);
515 case SHCNE_RENAMEFOLDER
:
516 if (!SUCCEEDED(_ReparsePIDL(dest
[1], &clean
)))
518 ERR("Can't reparse PIDL to a valid one\n");
521 if (NavigateToPIDL(dest
[0], &pItem
, FALSE
, FALSE
, FALSE
))
522 RenameItem(pItem
, clean
);
525 case SHCNE_UPDATEDIR
:
526 // We don't take care of this message
527 TRACE("Directory updated\n");
530 TRACE("Unhandled message\n");
535 LRESULT
CExplorerBand::OnSetFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
538 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), TRUE
);
543 LRESULT
CExplorerBand::OnKillFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
545 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), FALSE
);
550 // *** Helper functions ***
551 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, IShellFolder
*psfParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
553 TV_INSERTSTRUCT tvInsert
;
554 HTREEITEM htiCreated
;
556 /* Get the attributes of the node */
557 SFGAOF attrs
= SFGAO_STREAM
| SFGAO_HASSUBFOLDER
;
558 HRESULT hr
= psfParent
->GetAttributesOf(1, &pEltRelative
, &attrs
);
559 if (FAILED_UNEXPECTEDLY(hr
))
563 if ((attrs
& SFGAO_STREAM
))
565 TRACE("Ignoring stream\n");
569 /* Get the name of the node */
570 WCHAR wszDisplayName
[MAX_PATH
];
572 hr
= psfParent
->GetDisplayNameOf(pEltRelative
, SHGDN_INFOLDER
, &strret
);
573 if (FAILED_UNEXPECTEDLY(hr
))
576 hr
= StrRetToBufW(&strret
, pEltRelative
, wszDisplayName
, MAX_PATH
);
577 if (FAILED_UNEXPECTEDLY(hr
))
580 /* Get the icon of the node */
581 INT iIcon
= SHMapPIDLToSystemImageListIndex(psfParent
, pEltRelative
, NULL
);
583 NodeInfo
* pChildInfo
= new NodeInfo
;
586 ERR("Failed to allocate NodeInfo\n");
590 // Store our node info
591 pChildInfo
->absolutePidl
= ILClone(pElt
);
592 pChildInfo
->relativePidl
= ILClone(pEltRelative
);
593 pChildInfo
->expanded
= FALSE
;
595 // Set up our treeview template
596 tvInsert
.hParent
= hParent
;
597 tvInsert
.hInsertAfter
= TVI_LAST
;
598 tvInsert
.item
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
599 tvInsert
.item
.cchTextMax
= MAX_PATH
;
600 tvInsert
.item
.pszText
= wszDisplayName
;
601 tvInsert
.item
.iImage
= tvInsert
.item
.iSelectedImage
= iIcon
;
602 tvInsert
.item
.cChildren
= (attrs
& SFGAO_HASSUBFOLDER
) ? 1 : 0;
603 tvInsert
.item
.lParam
= (LPARAM
)pChildInfo
;
605 htiCreated
= TreeView_InsertItem(m_hWnd
, &tvInsert
);
609 TVSORTCB sortCallback
;
610 sortCallback
.hParent
= hParent
;
611 sortCallback
.lpfnCompare
= CompareTreeItems
;
612 sortCallback
.lParam
= (LPARAM
)this;
613 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
619 /* This is the slow version of the above method */
620 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
622 CComPtr
<IShellFolder
> psfFolder
;
623 HRESULT hr
= SHBindToParent(pElt
, IID_PPV_ARG(IShellFolder
, &psfFolder
), NULL
);
624 if (FAILED_UNEXPECTEDLY(hr
))
627 return InsertItem(hParent
, psfFolder
, pElt
, pEltRelative
, bSort
);
630 BOOL
CExplorerBand::InsertSubitems(HTREEITEM hItem
, NodeInfo
*pNodeInfo
)
632 CComPtr
<IEnumIDList
> pEnumIDList
;
633 LPITEMIDLIST pidlSub
;
639 CComPtr
<IShellFolder
> pFolder
;
640 TVSORTCB sortCallback
;
642 entry
= pNodeInfo
->absolutePidl
;
645 EnumFlags
= SHCONTF_FOLDERS
;
647 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidlSub
);
650 ERR("Can't get desktop PIDL !\n");
654 if (!pDesktop
->CompareIDs(NULL
, pidlSub
, entry
))
656 // We are the desktop, so use pDesktop as pFolder
661 // Get an IShellFolder of our pidl
662 hr
= pDesktop
->BindToObject(entry
, NULL
, IID_PPV_ARG(IShellFolder
, &pFolder
));
666 ERR("Can't bind folder to desktop !\n");
672 // TODO: handle hidden folders according to settings !
673 EnumFlags
|= SHCONTF_INCLUDEHIDDEN
;
675 // Enum through objects
676 hr
= pFolder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
678 // avoid broken IShellFolder implementations that return null pointer with success
679 if (!SUCCEEDED(hr
) || !pEnumIDList
)
681 ERR("Can't enum the folder !\n");
685 /* Don't redraw while we add stuff into the tree */
686 SendMessage(WM_SETREDRAW
, FALSE
, 0);
687 while(SUCCEEDED(pEnumIDList
->Next(1, &pidlSub
, &fetched
)) && pidlSub
&& fetched
)
689 LPITEMIDLIST pidlSubComplete
;
690 pidlSubComplete
= ILCombine(entry
, pidlSub
);
692 if (InsertItem(hItem
, pFolder
, pidlSubComplete
, pidlSub
, FALSE
))
694 ILFree(pidlSubComplete
);
697 pNodeInfo
->expanded
= TRUE
;
698 /* Let's do sorting */
699 sortCallback
.hParent
= hItem
;
700 sortCallback
.lpfnCompare
= CompareTreeItems
;
701 sortCallback
.lParam
= (LPARAM
)this;
702 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
704 /* Now we can redraw */
705 SendMessage(WM_SETREDRAW
, TRUE
, 0);
707 return (uItemCount
> 0) ? TRUE
: FALSE
;
711 * Navigate to a given PIDL in the treeview, and return matching tree item handle
712 * - dest: The absolute PIDL we should navigate in the treeview
713 * - item: Handle of the tree item matching the PIDL
714 * - bExpand: expand collapsed nodes in order to find the right element
715 * - bInsert: insert the element at the right place if we don't find it
716 * - bSelect: select the item after we found it
718 BOOL
CExplorerBand::NavigateToPIDL(LPITEMIDLIST dest
, HTREEITEM
*item
, BOOL bExpand
, BOOL bInsert
,
726 LPITEMIDLIST relativeChild
;
737 nodeData
= GetNodeInfo(current
);
740 ERR("Something has gone wrong, no data associated to node !\n");
744 // If we found our node, give it back
745 if (!pDesktop
->CompareIDs(0, nodeData
->absolutePidl
, dest
))
748 TreeView_SelectItem(m_hWnd
, current
);
753 // Check if we are a parent of the requested item
754 relativeChild
= ILFindChild(nodeData
->absolutePidl
, dest
);
755 if (relativeChild
!= 0)
757 // Notify treeview we have children
758 tvItem
.mask
= TVIF_CHILDREN
;
759 tvItem
.hItem
= current
;
760 tvItem
.cChildren
= 1;
761 TreeView_SetItem(m_hWnd
, &tvItem
);
763 // If we can expand and the node isn't expanded yet, do it
766 if (!nodeData
->expanded
)
767 InsertSubitems(current
, nodeData
);
768 TreeView_Expand(m_hWnd
, current
, TVE_EXPAND
);
771 // Try to get a child
772 tmp
= TreeView_GetChild(m_hWnd
, current
);
775 // We have a child, let's continue with it
781 if (bInsert
&& nodeData
->expanded
)
783 // Happens when we have to create a subchild inside a child
784 current
= InsertItem(current
, dest
, relativeChild
, TRUE
);
786 // We end up here, without any children, so we found nothing
787 // Tell the parent node it has children
788 ZeroMemory(&tvItem
, sizeof(tvItem
));
794 tmp
= TreeView_GetNextSibling(m_hWnd
, current
);
802 current
= InsertItem(parent
, dest
, ILFindLastID(dest
), TRUE
);
812 BOOL
CExplorerBand::NavigateToCurrentFolder()
814 LPITEMIDLIST explorerPidl
;
815 CComPtr
<IBrowserService
> pBrowserService
;
821 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IBrowserService
, &pBrowserService
));
824 ERR("Can't get IBrowserService !\n");
828 hr
= pBrowserService
->GetPidl(&explorerPidl
);
829 if (!SUCCEEDED(hr
) || !explorerPidl
)
831 ERR("Unable to get browser PIDL !\n");
835 /* find PIDL into our explorer */
836 result
= NavigateToPIDL(explorerPidl
, &dummy
, TRUE
, FALSE
, TRUE
);
841 BOOL
CExplorerBand::DeleteItem(LPITEMIDLIST idl
)
845 HTREEITEM parentNode
;
847 if (!NavigateToPIDL(idl
, &toDelete
, FALSE
, FALSE
, FALSE
))
850 // TODO: check that the treeview item is really deleted
852 parentNode
= TreeView_GetParent(m_hWnd
, toDelete
);
853 // Navigate to parent when deleting child item
854 if (!pDesktop
->CompareIDs(0, idl
, pidlCurrent
))
856 TreeView_SelectItem(m_hWnd
, parentNode
);
859 // Remove the child item
860 TreeView_DeleteItem(m_hWnd
, toDelete
);
861 // Probe parent to see if it has children
862 if (!TreeView_GetChild(m_hWnd
, parentNode
))
864 // Decrement parent's child count
865 ZeroMemory(&tvItem
, sizeof(tvItem
));
866 tvItem
.mask
= TVIF_CHILDREN
;
867 tvItem
.hItem
= parentNode
;
868 tvItem
.cChildren
= 0;
869 TreeView_SetItem(m_hWnd
, &tvItem
);
874 BOOL
CExplorerBand::RenameItem(HTREEITEM toRename
, LPITEMIDLIST newPidl
)
876 WCHAR wszDisplayName
[MAX_PATH
];
878 LPCITEMIDLIST relPidl
;
880 TVSORTCB sortCallback
;
883 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
884 itemInfo
.mask
= TVIF_PARAM
;
885 itemInfo
.hItem
= toRename
;
887 // Change PIDL associated to the item
888 relPidl
= ILFindLastID(newPidl
);
889 TreeView_GetItem(m_hWnd
, &itemInfo
);
890 if (!itemInfo
.lParam
)
892 ERR("Unable to fetch lParam\n");
895 SendMessage(WM_SETREDRAW
, FALSE
, 0);
896 treeInfo
= (NodeInfo
*)itemInfo
.lParam
;
897 ILFree(treeInfo
->absolutePidl
);
898 ILFree(treeInfo
->relativePidl
);
899 treeInfo
->absolutePidl
= ILClone(newPidl
);
900 treeInfo
->relativePidl
= ILClone(relPidl
);
902 // Change the display name
903 GetDisplayName(newPidl
, wszDisplayName
, MAX_PATH
, SHGDN_INFOLDER
);
904 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
905 itemInfo
.hItem
= toRename
;
906 itemInfo
.mask
= TVIF_TEXT
;
907 itemInfo
.pszText
= wszDisplayName
;
908 TreeView_SetItem(m_hWnd
, &itemInfo
);
910 if((child
= TreeView_GetChild(m_hWnd
, toRename
)) != NULL
)
912 RefreshTreePidl(child
, newPidl
);
916 sortCallback
.hParent
= TreeView_GetParent(m_hWnd
, toRename
);
917 sortCallback
.lpfnCompare
= CompareTreeItems
;
918 sortCallback
.lParam
= (LPARAM
)this;
919 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
920 SendMessage(WM_SETREDRAW
, TRUE
, 0);
924 BOOL
CExplorerBand::RefreshTreePidl(HTREEITEM tree
, LPITEMIDLIST pidlParent
)
929 // Update our node data
930 pInfo
= GetNodeInfo(tree
);
933 WARN("No tree info !\n");
936 ILFree(pInfo
->absolutePidl
);
937 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
938 if (!pInfo
->absolutePidl
)
940 WARN("PIDL allocation failed\n");
943 // Recursively update children
944 if ((tmp
= TreeView_GetChild(m_hWnd
, tree
)) != NULL
)
946 RefreshTreePidl(tmp
, pInfo
->absolutePidl
);
949 tmp
= TreeView_GetNextSibling(m_hWnd
, tree
);
952 pInfo
= GetNodeInfo(tmp
);
955 WARN("No tree info !\n");
958 ILFree(pInfo
->absolutePidl
);
959 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
960 tmp
= TreeView_GetNextSibling(m_hWnd
, tmp
);
965 // *** Tree item sorting callback ***
966 int CALLBACK
CExplorerBand::CompareTreeItems(LPARAM p1
, LPARAM p2
, LPARAM p3
)
969 * We first sort drive letters (Path root), then PIDLs and then regular folder
971 * This is not how Windows sorts item, but it gives decent results.
975 CExplorerBand
*pThis
;
976 WCHAR wszFolder1
[MAX_PATH
];
977 WCHAR wszFolder2
[MAX_PATH
];
979 info1
= (NodeInfo
*)p1
;
980 info2
= (NodeInfo
*)p2
;
981 pThis
= (CExplorerBand
*)p3
;
983 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_FORPARSING
);
984 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_FORPARSING
);
985 if (PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
987 return lstrcmpiW(wszFolder1
,wszFolder2
);
989 if (PathIsRoot(wszFolder1
) && !PathIsRoot(wszFolder2
))
993 if (!PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
997 // Now, we compare non-root folders, grab display name
998 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_INFOLDER
);
999 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_INFOLDER
);
1001 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1005 if (!_ILIsSpecialFolder(info1
->relativePidl
) && _ILIsSpecialFolder(info2
->relativePidl
))
1009 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1012 hr
= pThis
->pDesktop
->CompareIDs(0, info1
->absolutePidl
, info2
->absolutePidl
);
1014 return (hr
> 0) ? -1 : 1;
1016 return StrCmpLogicalW(wszFolder1
, wszFolder2
);
1019 // *** IOleWindow methods ***
1020 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetWindow(HWND
*lphwnd
)
1023 return E_INVALIDARG
;
1028 HRESULT STDMETHODCALLTYPE
CExplorerBand::ContextSensitiveHelp(BOOL fEnterMode
)
1035 // *** IDockingWindow methods ***
1036 HRESULT STDMETHODCALLTYPE
CExplorerBand::CloseDW(DWORD dwReserved
)
1038 // We do nothing, we don't have anything to save yet
1039 TRACE("CloseDW called\n");
1043 HRESULT STDMETHODCALLTYPE
CExplorerBand::ResizeBorderDW(const RECT
*prcBorder
, IUnknown
*punkToolbarSite
, BOOL fReserved
)
1045 /* Must return E_NOTIMPL according to MSDN */
1049 HRESULT STDMETHODCALLTYPE
CExplorerBand::ShowDW(BOOL fShow
)
1057 // *** IDeskBand methods ***
1058 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetBandInfo(DWORD dwBandID
, DWORD dwViewMode
, DESKBANDINFO
*pdbi
)
1062 return E_INVALIDARG
;
1064 this->dwBandID
= dwBandID
;
1066 if (pdbi
->dwMask
& DBIM_MINSIZE
)
1068 pdbi
->ptMinSize
.x
= 200;
1069 pdbi
->ptMinSize
.y
= 30;
1072 if (pdbi
->dwMask
& DBIM_MAXSIZE
)
1074 pdbi
->ptMaxSize
.y
= -1;
1077 if (pdbi
->dwMask
& DBIM_INTEGRAL
)
1079 pdbi
->ptIntegral
.y
= 1;
1082 if (pdbi
->dwMask
& DBIM_ACTUAL
)
1084 pdbi
->ptActual
.x
= 200;
1085 pdbi
->ptActual
.y
= 30;
1088 if (pdbi
->dwMask
& DBIM_TITLE
)
1090 if (!LoadStringW(_AtlBaseModule
.GetResourceInstance(), IDS_FOLDERSLABEL
, pdbi
->wszTitle
, _countof(pdbi
->wszTitle
)))
1091 return HRESULT_FROM_WIN32(GetLastError());
1094 if (pdbi
->dwMask
& DBIM_MODEFLAGS
)
1096 pdbi
->dwModeFlags
= DBIMF_NORMAL
| DBIMF_VARIABLEHEIGHT
;
1099 if (pdbi
->dwMask
& DBIM_BKCOLOR
)
1101 pdbi
->dwMask
&= ~DBIM_BKCOLOR
;
1107 // *** IObjectWithSite methods ***
1108 HRESULT STDMETHODCALLTYPE
CExplorerBand::SetSite(IUnknown
*pUnkSite
)
1113 if (pUnkSite
== pSite
)
1116 TRACE("SetSite called \n");
1119 DestroyExplorerBand();
1124 if (pUnkSite
!= pSite
)
1132 hr
= IUnknown_GetWindow(pUnkSite
, &parentWnd
);
1135 ERR("Could not get parent's window ! Status: %08lx\n", hr
);
1136 return E_INVALIDARG
;
1143 // Change its parent
1144 SetParent(parentWnd
);
1148 HWND wnd
= CreateWindow(WC_TREEVIEW
, NULL
,
1149 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
1150 0, 0, 0, 0, parentWnd
, NULL
, _AtlBaseModule
.GetModuleInstance(), NULL
);
1152 // Subclass the window
1153 SubclassWindow(wnd
);
1155 // Initialize our treeview now
1156 InitializeExplorerBand();
1157 RegisterDragDrop(m_hWnd
, dynamic_cast<IDropTarget
*>(this));
1162 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSite(REFIID riid
, void **ppvSite
)
1171 // *** IOleCommandTarget methods ***
1172 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
1178 HRESULT STDMETHODCALLTYPE
CExplorerBand::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
1185 // *** IServiceProvider methods ***
1186 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
1188 /* FIXME: we probably want to handle more services here */
1189 return IUnknown_QueryService(pSite
, SID_SShellBrowser
, riid
, ppvObject
);
1193 // *** IInputObject methods ***
1194 HRESULT STDMETHODCALLTYPE
CExplorerBand::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
1201 // TODO: handle message
1204 TranslateMessage(lpMsg
);
1205 DispatchMessage(lpMsg
);
1210 HRESULT STDMETHODCALLTYPE
CExplorerBand::HasFocusIO()
1212 return bFocused
? S_OK
: S_FALSE
;
1215 HRESULT STDMETHODCALLTYPE
CExplorerBand::TranslateAcceleratorIO(LPMSG lpMsg
)
1217 if (lpMsg
->hwnd
== m_hWnd
)
1219 TranslateMessage(lpMsg
);
1220 DispatchMessage(lpMsg
);
1227 // *** IPersist methods ***
1228 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetClassID(CLSID
*pClassID
)
1232 memcpy(pClassID
, &CLSID_ExplorerBand
, sizeof(CLSID
));
1237 // *** IPersistStream methods ***
1238 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsDirty()
1244 HRESULT STDMETHODCALLTYPE
CExplorerBand::Load(IStream
*pStm
)
1250 HRESULT STDMETHODCALLTYPE
CExplorerBand::Save(IStream
*pStm
, BOOL fClearDirty
)
1256 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
1258 // TODO: calculate max size
1264 // *** IWinEventHandler methods ***
1265 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
1270 if (uMsg
== WM_NOTIFY
)
1272 NMHDR
*pNotifyHeader
= (NMHDR
*)lParam
;
1273 switch (pNotifyHeader
->code
)
1275 case TVN_ITEMEXPANDING
:
1276 result
= OnTreeItemExpanding((LPNMTREEVIEW
)lParam
);
1278 *theResult
= result
;
1280 case TVN_SELCHANGED
:
1281 OnSelectionChanged((LPNMTREEVIEW
)lParam
);
1283 case TVN_DELETEITEM
:
1284 OnTreeItemDeleted((LPNMTREEVIEW
)lParam
);
1287 OnContextMenu(WM_CONTEXTMENU
, (WPARAM
)m_hWnd
, GetMessagePos(), bHandled
);
1292 case TVN_BEGINRDRAG
:
1293 OnTreeItemDragging((LPNMTREEVIEW
)lParam
, pNotifyHeader
->code
== TVN_BEGINRDRAG
);
1294 case TVN_BEGINLABELEDITW
:
1296 // TODO: put this in a function ? (mostly copypasta from CDefView)
1297 DWORD dwAttr
= SFGAO_CANRENAME
;
1298 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1299 CComPtr
<IShellFolder
> pParent
;
1300 LPCITEMIDLIST pChild
;
1305 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1308 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pChild
);
1309 if (!SUCCEEDED(hr
) || !pParent
.p
)
1312 hr
= pParent
->GetAttributesOf(1, &pChild
, &dwAttr
);
1313 if (SUCCEEDED(hr
) && (dwAttr
& SFGAO_CANRENAME
) && theResult
)
1317 case TVN_ENDLABELEDITW
:
1319 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1320 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
);
1368 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsWindowOwner(HWND hWnd
)
1370 return (hWnd
== m_hWnd
) ? S_OK
: S_FALSE
;
1373 // *** IBandNavigate methods ***
1374 HRESULT STDMETHODCALLTYPE
CExplorerBand::Select(long paramC
)
1380 // *** INamespaceProxy ***
1381 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetNavigateTarget(long paramC
, long param10
, long param14
)
1387 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(long paramC
)
1393 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnSelectionChanged(long paramC
)
1399 HRESULT STDMETHODCALLTYPE
CExplorerBand::RefreshFlags(long paramC
, long param10
, long param14
)
1405 HRESULT STDMETHODCALLTYPE
CExplorerBand::CacheItem(long paramC
)
1411 // *** IDispatch methods ***
1412 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfoCount(UINT
*pctinfo
)
1418 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfo(UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
1424 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetIDsOfNames(REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
1430 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1432 switch (dispIdMember
)
1434 case DISPID_DOWNLOADCOMPLETE
:
1435 case DISPID_NAVIGATECOMPLETE2
:
1436 TRACE("DISPID_NAVIGATECOMPLETE2 received\n");
1437 NavigateToCurrentFolder();
1440 TRACE("Unknown dispid requested: %08x\n", dispIdMember
);
1441 return E_INVALIDARG
;
1444 // *** IDropTarget methods ***
1445 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragEnter(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1447 ERR("Entering drag\n");
1449 oldSelected
= TreeView_GetSelection(m_hWnd
);
1450 return DragOver(glfKeyState
, pt
, pdwEffect
);
1453 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragOver(DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1456 CComPtr
<IShellFolder
> pShellFldr
;
1458 //LPCITEMIDLIST pChild;
1463 info
.flags
= TVHT_ONITEM
;
1465 ScreenToClient(&info
.pt
);
1467 // Move to the item selected by the treeview (don't change right pane)
1468 TreeView_HitTest(m_hWnd
, &info
);
1473 TreeView_SelectItem(m_hWnd
, info
.hItem
);
1474 bNavigating
= FALSE
;
1475 // Delegate to shell folder
1476 if (pDropTarget
&& info
.hItem
!= childTargetNode
)
1480 if (info
.hItem
!= childTargetNode
)
1482 nodeInfo
= GetNodeInfo(info
.hItem
);
1486 hr
= SHBindToParent(nodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pShellFldr
), &pChild
);
1489 hr
= pShellFldr
->GetUIObjectOf(m_hWnd
, 1, &pChild
, IID_IDropTarget
, NULL
, reinterpret_cast<void**>(&pDropTarget
));
1493 if(_ILIsDesktop(nodeInfo
->absolutePidl
))
1494 pShellFldr
= pDesktop
;
1497 hr
= pDesktop
->BindToObject(nodeInfo
->absolutePidl
, 0, IID_PPV_ARG(IShellFolder
, &pShellFldr
));
1500 /* Don't allow dnd since we couldn't get our folder object */
1501 ERR("Can't bind to folder object\n");
1502 *pdwEffect
= DROPEFFECT_NONE
;
1506 hr
= pShellFldr
->CreateViewObject(m_hWnd
, IID_PPV_ARG(IDropTarget
, &pDropTarget
));
1509 /* Don't allow dnd since we couldn't get our drop target */
1510 ERR("Can't get drop target for folder object\n");
1511 *pdwEffect
= DROPEFFECT_NONE
;
1514 hr
= pDropTarget
->DragEnter(pCurObject
, glfKeyState
, pt
, pdwEffect
);
1515 childTargetNode
= info
.hItem
;
1519 hr
= pDropTarget
->DragOver(glfKeyState
, pt
, pdwEffect
);
1524 childTargetNode
= NULL
;
1526 *pdwEffect
= DROPEFFECT_NONE
;
1531 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragLeave()
1534 TreeView_SelectItem(m_hWnd
, oldSelected
);
1535 bNavigating
= FALSE
;
1536 childTargetNode
= NULL
;
1544 HRESULT STDMETHODCALLTYPE
CExplorerBand::Drop(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1548 pDropTarget
->Drop(pObj
, glfKeyState
, pt
, pdwEffect
);
1553 // *** IDropSource methods ***
1554 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
)
1557 return DRAGDROP_S_CANCEL
;
1558 if ((grfKeyState
& MK_LBUTTON
) || (grfKeyState
& MK_RBUTTON
))
1560 return DRAGDROP_S_DROP
;
1563 HRESULT STDMETHODCALLTYPE
CExplorerBand::GiveFeedback(DWORD dwEffect
)
1565 return DRAGDROP_S_USEDEFAULTCURSORS
;