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 This is a Windows hack, because shell event messages in Windows gives an
91 ill-formed PIDL stripped from useful data that parses incorrectly with SHGetFileInfo.
92 So we need to re-enumerate subfolders until we find one with the same name.
94 HRESULT
_ReparsePIDL(LPITEMIDLIST buggyPidl
, LPITEMIDLIST
*cleanPidl
)
97 CComPtr
<IShellFolder
> folder
;
98 CComPtr
<IPersistFolder2
> persist
;
99 CComPtr
<IEnumIDList
> pEnumIDList
;
100 LPITEMIDLIST childPidl
;
101 LPITEMIDLIST correctChild
;
102 LPITEMIDLIST correctParent
;
107 EnumFlags
= SHCONTF_FOLDERS
| SHCONTF_INCLUDEHIDDEN
;
108 hr
= SHBindToParent(buggyPidl
, IID_PPV_ARG(IShellFolder
, &folder
), (LPCITEMIDLIST
*)&childPidl
);
112 ERR("Can't bind to parent folder\n");
115 hr
= folder
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &persist
));
118 ERR("PIDL doesn't belong to the shell namespace, aborting\n");
122 hr
= persist
->GetCurFolder(&correctParent
);
125 ERR("Unable to get current folder\n");
129 hr
= folder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
130 // avoid broken IShellFolder implementations that return null pointer with success
131 if (!SUCCEEDED(hr
) || !pEnumIDList
)
133 ERR("Can't enum the folder !\n");
137 while(SUCCEEDED(pEnumIDList
->Next(1, &correctChild
, &fetched
)) && correctChild
&& fetched
)
139 if (!folder
->CompareIDs(0, childPidl
, correctChild
))
141 *cleanPidl
= ILCombine(correctParent
, correctChild
);
142 ILFree(correctChild
);
145 ILFree(correctChild
);
148 ILFree(correctParent
);
152 CExplorerBand::CExplorerBand() :
153 pSite(NULL
), fVisible(FALSE
), bNavigating(FALSE
), dwBandID(0), pidlCurrent(NULL
)
157 CExplorerBand::~CExplorerBand()
165 void CExplorerBand::InitializeExplorerBand()
167 // Init the treeview here
170 CComPtr
<IWebBrowser2
> browserService
;
171 SHChangeNotifyEntry shcne
;
173 hr
= SHGetDesktopFolder(&pDesktop
);
174 if (FAILED_UNEXPECTEDLY(hr
))
177 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidl
);
178 if (FAILED_UNEXPECTEDLY(hr
))
182 hr
= SHGetImageList(SHIL_SMALL
, IID_PPV_ARG(IImageList
, &piml
));
183 if (FAILED_UNEXPECTEDLY(hr
))
186 TreeView_SetImageList(m_hWnd
, (HIMAGELIST
)piml
, TVSIL_NORMAL
);
188 // Insert the root node
189 hRoot
= InsertItem(0, pDesktop
, pidl
, pidl
, FALSE
);
192 ERR("Failed to create root item\n");
196 NodeInfo
* pNodeInfo
= GetNodeInfo(hRoot
);
198 // Insert child nodes
199 InsertSubitems(hRoot
, pNodeInfo
);
200 TreeView_Expand(m_hWnd
, hRoot
, TVE_EXPAND
);
202 // Navigate to current folder position
203 NavigateToCurrentFolder();
205 // Register shell notification
207 shcne
.fRecursive
= TRUE
;
208 shellRegID
= SHChangeNotifyRegister(
210 SHCNRF_ShellLevel
| SHCNRF_InterruptLevel
| SHCNRF_RecursiveInterrupt
,
211 SHCNE_DISKEVENTS
| SHCNE_RENAMEFOLDER
| SHCNE_RMDIR
| SHCNE_MKDIR
,
217 ERR("Something went wrong, error %08x\n", GetLastError());
219 // Register browser connection endpoint
220 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
221 if (FAILED_UNEXPECTEDLY(hr
))
224 hr
= AtlAdvise(browserService
, dynamic_cast<IDispatch
*>(this), DIID_DWebBrowserEvents
, &adviseCookie
);
225 if (FAILED_UNEXPECTEDLY(hr
))
231 void CExplorerBand::DestroyExplorerBand()
234 CComPtr
<IWebBrowser2
> browserService
;
236 TRACE("Cleaning up explorer band ...\n");
238 hr
= IUnknown_QueryService(pSite
, SID_SWebBrowserApp
, IID_PPV_ARG(IWebBrowser2
, &browserService
));
239 if (FAILED_UNEXPECTEDLY(hr
))
242 hr
= AtlUnadvise(browserService
, DIID_DWebBrowserEvents
, adviseCookie
);
243 /* Remove all items of the treeview */
244 RevokeDragDrop(m_hWnd
);
245 TreeView_DeleteAllItems(m_hWnd
);
248 TRACE("Cleanup done !\n");
251 CExplorerBand::NodeInfo
* CExplorerBand::GetNodeInfo(HTREEITEM hItem
)
255 tvItem
.mask
= TVIF_PARAM
;
256 tvItem
.hItem
= hItem
;
258 if (!TreeView_GetItem(m_hWnd
, &tvItem
))
261 return reinterpret_cast<NodeInfo
*>(tvItem
.lParam
);
264 HRESULT
CExplorerBand::ExecuteCommand(CComPtr
<IContextMenu
>& menu
, UINT nCmd
)
266 CComPtr
<IOleWindow
> pBrowserOleWnd
;
267 CMINVOKECOMMANDINFO cmi
;
271 hr
= IUnknown_QueryService(pSite
, SID_SShellBrowser
, IID_PPV_ARG(IOleWindow
, &pBrowserOleWnd
));
272 if (FAILED_UNEXPECTEDLY(hr
))
275 hr
= pBrowserOleWnd
->GetWindow(&browserWnd
);
276 if (FAILED_UNEXPECTEDLY(hr
))
279 ZeroMemory(&cmi
, sizeof(cmi
));
280 cmi
.cbSize
= sizeof(cmi
);
281 cmi
.lpVerb
= MAKEINTRESOURCEA(nCmd
);
282 cmi
.hwnd
= browserWnd
;
283 if (GetKeyState(VK_SHIFT
) & 0x8000)
284 cmi
.fMask
|= CMIC_MASK_SHIFT_DOWN
;
285 if (GetKeyState(VK_CONTROL
) & 0x8000)
286 cmi
.fMask
|= CMIC_MASK_CONTROL_DOWN
;
288 return menu
->InvokeCommand(&cmi
);
291 HRESULT
CExplorerBand::UpdateBrowser(LPITEMIDLIST pidlGoto
)
293 CComPtr
<IShellBrowser
> pBrowserService
;
296 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IShellBrowser
, &pBrowserService
));
297 if (FAILED_UNEXPECTEDLY(hr
))
300 hr
= pBrowserService
->BrowseObject(pidlGoto
, SBSP_SAMEBROWSER
| SBSP_ABSOLUTE
);
301 if (FAILED_UNEXPECTEDLY(hr
))
307 pidlCurrent
= ILClone(pidlGoto
);
312 // *** notifications handling ***
313 BOOL
CExplorerBand::OnTreeItemExpanding(LPNMTREEVIEW pnmtv
)
317 if (pnmtv
->action
== TVE_COLLAPSE
) {
318 if (pnmtv
->itemNew
.hItem
== hRoot
)
320 // Prenvent root from collapsing
321 pnmtv
->itemNew
.mask
|= TVIF_STATE
;
322 pnmtv
->itemNew
.stateMask
|= TVIS_EXPANDED
;
323 pnmtv
->itemNew
.state
&= ~TVIS_EXPANDED
;
324 pnmtv
->action
= TVE_EXPAND
;
328 if (pnmtv
->action
== TVE_EXPAND
) {
329 // Grab our directory PIDL
330 pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
331 // We have it, let's try
332 if (pNodeInfo
&& !pNodeInfo
->expanded
)
333 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, pNodeInfo
)) {
334 // remove subitem "+" since we failed to add subitems
337 tvItem
.mask
= TVIF_CHILDREN
;
338 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
339 tvItem
.cChildren
= 0;
341 TreeView_SetItem(m_hWnd
, &tvItem
);
347 BOOL
CExplorerBand::OnTreeItemDeleted(LPNMTREEVIEW pnmtv
)
349 /* Destroy memory associated to our node */
350 NodeInfo
* ptr
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
353 ILFree(ptr
->relativePidl
);
354 ILFree(ptr
->absolutePidl
);
360 void CExplorerBand::OnSelectionChanged(LPNMTREEVIEW pnmtv
)
362 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
364 /* Prevents navigation if selection is initiated inside the band */
368 UpdateBrowser(pNodeInfo
->absolutePidl
);
372 //TreeView_Expand(m_hWnd, pnmtv->itemNew.hItem, TVE_EXPAND);
375 void CExplorerBand::OnTreeItemDragging(LPNMTREEVIEW pnmtv
, BOOL isRightClick
)
377 CComPtr
<IShellFolder
> pSrcFolder
;
378 CComPtr
<IDataObject
> pObj
;
384 dwEffect
= DROPEFFECT_COPY
| DROPEFFECT_MOVE
;
385 if (!pnmtv
->itemNew
.lParam
)
387 NodeInfo
* pNodeInfo
= GetNodeInfo(pnmtv
->itemNew
.hItem
);
388 hr
= SHBindToParent(pNodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pSrcFolder
), &pLast
);
391 hr
= pSrcFolder
->GetUIObjectOf(m_hWnd
, 1, &pLast
, IID_IDataObject
, 0, reinterpret_cast<void**>(&pObj
));
394 DoDragDrop(pObj
, this, dwEffect
, &dwEffect2
);
399 // *** ATL event handlers ***
400 LRESULT
CExplorerBand::OnContextMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
407 CComPtr
<IShellFolder
> pFolder
;
408 CComPtr
<IContextMenu
> contextMenu
;
411 LPITEMIDLIST pidlChild
;
414 item
= TreeView_GetSelection(m_hWnd
);
423 if (x
== -1 && y
== -1)
425 // TODO: grab position of tree item and position it correctly
428 info
= GetNodeInfo(item
);
431 ERR("No node data, something has gone wrong !\n");
434 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pFolder
),
435 (LPCITEMIDLIST
*)&pidlChild
);
438 ERR("Can't bind to folder!\n");
441 hr
= pFolder
->GetUIObjectOf(m_hWnd
, 1, (LPCITEMIDLIST
*)&pidlChild
, IID_IContextMenu
,
442 NULL
, reinterpret_cast<void**>(&contextMenu
));
445 ERR("Can't get IContextMenu interface\n");
449 IUnknown_SetSite(contextMenu
, (IDeskBand
*)this);
451 treeMenu
= CreatePopupMenu();
452 hr
= contextMenu
->QueryContextMenu(treeMenu
, 0, FCIDM_SHVIEWFIRST
, FCIDM_SHVIEWLAST
,
456 WARN("Can't get context menu for item\n");
457 DestroyMenu(treeMenu
);
460 uCommand
= TrackPopupMenu(treeMenu
, TPM_LEFTALIGN
| TPM_RETURNCMD
| TPM_LEFTBUTTON
| TPM_RIGHTBUTTON
,
461 x
, y
, 0, m_hWnd
, NULL
);
463 ExecuteCommand(contextMenu
, uCommand
);
467 IUnknown_SetSite(contextMenu
, NULL
);
469 DestroyMenu(treeMenu
);
471 TreeView_SelectItem(m_hWnd
, oldSelected
);
476 LRESULT
CExplorerBand::ContextMenuHack(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
479 if (uMsg
== WM_RBUTTONDOWN
)
482 info
.pt
.x
= LOWORD(lParam
);
483 info
.pt
.y
= HIWORD(lParam
);
484 info
.flags
= TVHT_ONITEM
;
487 // Save the current location
488 oldSelected
= TreeView_GetSelection(m_hWnd
);
490 // Move to the item selected by the treeview (don't change right pane)
491 TreeView_HitTest(m_hWnd
, &info
);
493 TreeView_SelectItem(m_hWnd
, info
.hItem
);
496 return FALSE
; /* let the wndproc process the message */
499 LRESULT
CExplorerBand::OnShellEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
505 dest
= (LPITEMIDLIST
*)wParam
;
506 /* TODO: handle shell notifications */
507 switch(lParam
& ~SHCNE_INTERRUPT
)
510 if (!SUCCEEDED(_ReparsePIDL(dest
[0], &clean
)))
512 ERR("Can't reparse PIDL to a valid one\n");
515 NavigateToPIDL(clean
, &pItem
, FALSE
, TRUE
, FALSE
);
521 case SHCNE_RENAMEFOLDER
:
522 if (!SUCCEEDED(_ReparsePIDL(dest
[1], &clean
)))
524 ERR("Can't reparse PIDL to a valid one\n");
527 if (NavigateToPIDL(dest
[0], &pItem
, FALSE
, FALSE
, FALSE
))
528 RenameItem(pItem
, clean
);
531 case SHCNE_UPDATEDIR
:
532 // We don't take care of this message
533 TRACE("Directory updated\n");
536 TRACE("Unhandled message\n");
541 LRESULT
CExplorerBand::OnSetFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
544 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), TRUE
);
549 LRESULT
CExplorerBand::OnKillFocus(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
551 IUnknown_OnFocusChangeIS(pSite
, reinterpret_cast<IUnknown
*>(this), FALSE
);
556 // *** Helper functions ***
557 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, IShellFolder
*psfParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
559 TV_INSERTSTRUCT tvInsert
;
560 HTREEITEM htiCreated
;
562 /* Get the attributes of the node */
563 SFGAOF attrs
= SFGAO_STREAM
| SFGAO_HASSUBFOLDER
;
564 HRESULT hr
= psfParent
->GetAttributesOf(1, &pEltRelative
, &attrs
);
565 if (FAILED_UNEXPECTEDLY(hr
))
569 if ((attrs
& SFGAO_STREAM
))
571 TRACE("Ignoring stream\n");
575 /* Get the name of the node */
576 WCHAR wszDisplayName
[MAX_PATH
];
578 hr
= psfParent
->GetDisplayNameOf(pEltRelative
, SHGDN_INFOLDER
, &strret
);
579 if (FAILED_UNEXPECTEDLY(hr
))
582 hr
= StrRetToBufW(&strret
, pEltRelative
, wszDisplayName
, MAX_PATH
);
583 if (FAILED_UNEXPECTEDLY(hr
))
586 /* Get the icon of the node */
587 INT iIcon
= SHMapPIDLToSystemImageListIndex(psfParent
, pEltRelative
, NULL
);
589 NodeInfo
* pChildInfo
= new NodeInfo
;
592 ERR("Failed to allocate NodeInfo\n");
596 // Store our node info
597 pChildInfo
->absolutePidl
= ILClone(pElt
);
598 pChildInfo
->relativePidl
= ILClone(pEltRelative
);
599 pChildInfo
->expanded
= FALSE
;
601 // Set up our treeview template
602 tvInsert
.hParent
= hParent
;
603 tvInsert
.hInsertAfter
= TVI_LAST
;
604 tvInsert
.item
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
605 tvInsert
.item
.cchTextMax
= MAX_PATH
;
606 tvInsert
.item
.pszText
= wszDisplayName
;
607 tvInsert
.item
.iImage
= tvInsert
.item
.iSelectedImage
= iIcon
;
608 tvInsert
.item
.cChildren
= (attrs
& SFGAO_HASSUBFOLDER
) ? 1 : 0;
609 tvInsert
.item
.lParam
= (LPARAM
)pChildInfo
;
611 htiCreated
= TreeView_InsertItem(m_hWnd
, &tvInsert
);
615 TVSORTCB sortCallback
;
616 sortCallback
.hParent
= hParent
;
617 sortCallback
.lpfnCompare
= CompareTreeItems
;
618 sortCallback
.lParam
= (LPARAM
)this;
619 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
625 /* This is the slow version of the above method */
626 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
628 CComPtr
<IShellFolder
> psfFolder
;
629 HRESULT hr
= SHBindToParent(pElt
, IID_PPV_ARG(IShellFolder
, &psfFolder
), NULL
);
630 if (FAILED_UNEXPECTEDLY(hr
))
633 return InsertItem(hParent
, psfFolder
, pElt
, pEltRelative
, bSort
);
636 BOOL
CExplorerBand::InsertSubitems(HTREEITEM hItem
, NodeInfo
*pNodeInfo
)
638 CComPtr
<IEnumIDList
> pEnumIDList
;
639 LPITEMIDLIST pidlSub
;
645 CComPtr
<IShellFolder
> pFolder
;
646 TVSORTCB sortCallback
;
648 entry
= pNodeInfo
->absolutePidl
;
651 EnumFlags
= SHCONTF_FOLDERS
;
653 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidlSub
);
656 ERR("Can't get desktop PIDL !\n");
660 if (!pDesktop
->CompareIDs(NULL
, pidlSub
, entry
))
662 // We are the desktop, so use pDesktop as pFolder
667 // Get an IShellFolder of our pidl
668 hr
= pDesktop
->BindToObject(entry
, NULL
, IID_PPV_ARG(IShellFolder
, &pFolder
));
672 ERR("Can't bind folder to desktop !\n");
678 // TODO: handle hidden folders according to settings !
679 EnumFlags
|= SHCONTF_INCLUDEHIDDEN
;
681 // Enum through objects
682 hr
= pFolder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
684 // avoid broken IShellFolder implementations that return null pointer with success
685 if (!SUCCEEDED(hr
) || !pEnumIDList
)
687 ERR("Can't enum the folder !\n");
691 /* Don't redraw while we add stuff into the tree */
692 SendMessage(WM_SETREDRAW
, FALSE
, 0);
693 while(SUCCEEDED(pEnumIDList
->Next(1, &pidlSub
, &fetched
)) && pidlSub
&& fetched
)
695 LPITEMIDLIST pidlSubComplete
;
696 pidlSubComplete
= ILCombine(entry
, pidlSub
);
698 if (InsertItem(hItem
, pFolder
, pidlSubComplete
, pidlSub
, FALSE
))
700 ILFree(pidlSubComplete
);
703 pNodeInfo
->expanded
= TRUE
;
704 /* Let's do sorting */
705 sortCallback
.hParent
= hItem
;
706 sortCallback
.lpfnCompare
= CompareTreeItems
;
707 sortCallback
.lParam
= (LPARAM
)this;
708 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
710 /* Now we can redraw */
711 SendMessage(WM_SETREDRAW
, TRUE
, 0);
713 return (uItemCount
> 0) ? TRUE
: FALSE
;
717 * Navigate to a given PIDL in the treeview, and return matching tree item handle
718 * - dest: The absolute PIDL we should navigate in the treeview
719 * - item: Handle of the tree item matching the PIDL
720 * - bExpand: expand collapsed nodes in order to find the right element
721 * - bInsert: insert the element at the right place if we don't find it
722 * - bSelect: select the item after we found it
724 BOOL
CExplorerBand::NavigateToPIDL(LPITEMIDLIST dest
, HTREEITEM
*item
, BOOL bExpand
, BOOL bInsert
,
732 LPITEMIDLIST relativeChild
;
743 nodeData
= GetNodeInfo(current
);
746 ERR("Something has gone wrong, no data associated to node !\n");
750 // If we found our node, give it back
751 if (!pDesktop
->CompareIDs(0, nodeData
->absolutePidl
, dest
))
754 TreeView_SelectItem(m_hWnd
, current
);
759 // Check if we are a parent of the requested item
760 relativeChild
= ILFindChild(nodeData
->absolutePidl
, dest
);
761 if (relativeChild
!= 0)
763 // Notify treeview we have children
764 tvItem
.mask
= TVIF_CHILDREN
;
765 tvItem
.hItem
= current
;
766 tvItem
.cChildren
= 1;
767 TreeView_SetItem(m_hWnd
, &tvItem
);
769 // If we can expand and the node isn't expanded yet, do it
772 if (!nodeData
->expanded
)
773 InsertSubitems(current
, nodeData
);
774 TreeView_Expand(m_hWnd
, current
, TVE_EXPAND
);
777 // Try to get a child
778 tmp
= TreeView_GetChild(m_hWnd
, current
);
781 // We have a child, let's continue with it
787 if (bInsert
&& nodeData
->expanded
)
789 // Happens when we have to create a subchild inside a child
790 current
= InsertItem(current
, dest
, relativeChild
, TRUE
);
792 // We end up here, without any children, so we found nothing
793 // Tell the parent node it has children
794 ZeroMemory(&tvItem
, sizeof(tvItem
));
800 tmp
= TreeView_GetNextSibling(m_hWnd
, current
);
808 current
= InsertItem(parent
, dest
, ILFindLastID(dest
), TRUE
);
818 BOOL
CExplorerBand::NavigateToCurrentFolder()
820 LPITEMIDLIST explorerPidl
;
821 CComPtr
<IBrowserService
> pBrowserService
;
827 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IBrowserService
, &pBrowserService
));
830 ERR("Can't get IBrowserService !\n");
834 hr
= pBrowserService
->GetPidl(&explorerPidl
);
835 if (!SUCCEEDED(hr
) || !explorerPidl
)
837 ERR("Unable to get browser PIDL !\n");
841 /* find PIDL into our explorer */
842 result
= NavigateToPIDL(explorerPidl
, &dummy
, TRUE
, FALSE
, TRUE
);
847 BOOL
CExplorerBand::DeleteItem(LPITEMIDLIST idl
)
851 HTREEITEM parentNode
;
853 if (!NavigateToPIDL(idl
, &toDelete
, FALSE
, FALSE
, FALSE
))
856 // TODO: check that the treeview item is really deleted
858 parentNode
= TreeView_GetParent(m_hWnd
, toDelete
);
859 // Navigate to parent when deleting child item
860 if (!pDesktop
->CompareIDs(0, idl
, pidlCurrent
))
862 TreeView_SelectItem(m_hWnd
, parentNode
);
865 // Remove the child item
866 TreeView_DeleteItem(m_hWnd
, toDelete
);
867 // Probe parent to see if it has children
868 if (!TreeView_GetChild(m_hWnd
, parentNode
))
870 // Decrement parent's child count
871 ZeroMemory(&tvItem
, sizeof(tvItem
));
872 tvItem
.mask
= TVIF_CHILDREN
;
873 tvItem
.hItem
= parentNode
;
874 tvItem
.cChildren
= 0;
875 TreeView_SetItem(m_hWnd
, &tvItem
);
880 BOOL
CExplorerBand::RenameItem(HTREEITEM toRename
, LPITEMIDLIST newPidl
)
882 WCHAR wszDisplayName
[MAX_PATH
];
884 LPCITEMIDLIST relPidl
;
886 TVSORTCB sortCallback
;
889 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
890 itemInfo
.mask
= TVIF_PARAM
;
891 itemInfo
.hItem
= toRename
;
893 // Change PIDL associated to the item
894 relPidl
= ILFindLastID(newPidl
);
895 TreeView_GetItem(m_hWnd
, &itemInfo
);
896 if (!itemInfo
.lParam
)
898 ERR("Unable to fetch lParam\n");
901 SendMessage(WM_SETREDRAW
, FALSE
, 0);
902 treeInfo
= (NodeInfo
*)itemInfo
.lParam
;
903 ILFree(treeInfo
->absolutePidl
);
904 ILFree(treeInfo
->relativePidl
);
905 treeInfo
->absolutePidl
= ILClone(newPidl
);
906 treeInfo
->relativePidl
= ILClone(relPidl
);
908 // Change the display name
909 GetDisplayName(newPidl
, wszDisplayName
, MAX_PATH
, SHGDN_INFOLDER
);
910 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
911 itemInfo
.hItem
= toRename
;
912 itemInfo
.mask
= TVIF_TEXT
;
913 itemInfo
.pszText
= wszDisplayName
;
914 TreeView_SetItem(m_hWnd
, &itemInfo
);
916 if((child
= TreeView_GetChild(m_hWnd
, toRename
)) != NULL
)
918 RefreshTreePidl(child
, newPidl
);
922 sortCallback
.hParent
= TreeView_GetParent(m_hWnd
, toRename
);
923 sortCallback
.lpfnCompare
= CompareTreeItems
;
924 sortCallback
.lParam
= (LPARAM
)this;
925 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
926 SendMessage(WM_SETREDRAW
, TRUE
, 0);
930 BOOL
CExplorerBand::RefreshTreePidl(HTREEITEM tree
, LPITEMIDLIST pidlParent
)
935 // Update our node data
936 pInfo
= GetNodeInfo(tree
);
939 WARN("No tree info !\n");
942 ILFree(pInfo
->absolutePidl
);
943 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
944 if (!pInfo
->absolutePidl
)
946 WARN("PIDL allocation failed\n");
949 // Recursively update children
950 if ((tmp
= TreeView_GetChild(m_hWnd
, tree
)) != NULL
)
952 RefreshTreePidl(tmp
, pInfo
->absolutePidl
);
955 tmp
= TreeView_GetNextSibling(m_hWnd
, tree
);
958 pInfo
= GetNodeInfo(tmp
);
961 WARN("No tree info !\n");
964 ILFree(pInfo
->absolutePidl
);
965 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
966 tmp
= TreeView_GetNextSibling(m_hWnd
, tmp
);
971 // *** Tree item sorting callback ***
972 int CALLBACK
CExplorerBand::CompareTreeItems(LPARAM p1
, LPARAM p2
, LPARAM p3
)
975 * We first sort drive letters (Path root), then PIDLs and then regular folder
977 * This is not how Windows sorts item, but it gives decent results.
981 CExplorerBand
*pThis
;
982 WCHAR wszFolder1
[MAX_PATH
];
983 WCHAR wszFolder2
[MAX_PATH
];
985 info1
= (NodeInfo
*)p1
;
986 info2
= (NodeInfo
*)p2
;
987 pThis
= (CExplorerBand
*)p3
;
989 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_FORPARSING
);
990 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_FORPARSING
);
991 if (PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
993 return lstrcmpiW(wszFolder1
,wszFolder2
);
995 if (PathIsRoot(wszFolder1
) && !PathIsRoot(wszFolder2
))
999 if (!PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
1003 // Now, we compare non-root folders, grab display name
1004 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_INFOLDER
);
1005 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_INFOLDER
);
1007 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1011 if (!_ILIsSpecialFolder(info1
->relativePidl
) && _ILIsSpecialFolder(info2
->relativePidl
))
1015 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1018 hr
= pThis
->pDesktop
->CompareIDs(0, info1
->absolutePidl
, info2
->absolutePidl
);
1020 return (hr
> 0) ? -1 : 1;
1022 return StrCmpLogicalW(wszFolder1
, wszFolder2
);
1025 // *** IOleWindow methods ***
1026 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetWindow(HWND
*lphwnd
)
1029 return E_INVALIDARG
;
1034 HRESULT STDMETHODCALLTYPE
CExplorerBand::ContextSensitiveHelp(BOOL fEnterMode
)
1041 // *** IDockingWindow methods ***
1042 HRESULT STDMETHODCALLTYPE
CExplorerBand::CloseDW(DWORD dwReserved
)
1044 // We do nothing, we don't have anything to save yet
1045 TRACE("CloseDW called\n");
1049 HRESULT STDMETHODCALLTYPE
CExplorerBand::ResizeBorderDW(const RECT
*prcBorder
, IUnknown
*punkToolbarSite
, BOOL fReserved
)
1051 /* Must return E_NOTIMPL according to MSDN */
1055 HRESULT STDMETHODCALLTYPE
CExplorerBand::ShowDW(BOOL fShow
)
1063 // *** IDeskBand methods ***
1064 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetBandInfo(DWORD dwBandID
, DWORD dwViewMode
, DESKBANDINFO
*pdbi
)
1068 return E_INVALIDARG
;
1070 this->dwBandID
= dwBandID
;
1072 if (pdbi
->dwMask
& DBIM_MINSIZE
)
1074 pdbi
->ptMinSize
.x
= 200;
1075 pdbi
->ptMinSize
.y
= 30;
1078 if (pdbi
->dwMask
& DBIM_MAXSIZE
)
1080 pdbi
->ptMaxSize
.y
= -1;
1083 if (pdbi
->dwMask
& DBIM_INTEGRAL
)
1085 pdbi
->ptIntegral
.y
= 1;
1088 if (pdbi
->dwMask
& DBIM_ACTUAL
)
1090 pdbi
->ptActual
.x
= 200;
1091 pdbi
->ptActual
.y
= 30;
1094 if (pdbi
->dwMask
& DBIM_TITLE
)
1096 if (!LoadStringW(_AtlBaseModule
.GetResourceInstance(), IDS_FOLDERSLABEL
, pdbi
->wszTitle
, _countof(pdbi
->wszTitle
)))
1097 return HRESULT_FROM_WIN32(GetLastError());
1100 if (pdbi
->dwMask
& DBIM_MODEFLAGS
)
1102 pdbi
->dwModeFlags
= DBIMF_NORMAL
| DBIMF_VARIABLEHEIGHT
;
1105 if (pdbi
->dwMask
& DBIM_BKCOLOR
)
1107 pdbi
->dwMask
&= ~DBIM_BKCOLOR
;
1113 // *** IObjectWithSite methods ***
1114 HRESULT STDMETHODCALLTYPE
CExplorerBand::SetSite(IUnknown
*pUnkSite
)
1119 if (pUnkSite
== pSite
)
1122 TRACE("SetSite called \n");
1125 DestroyExplorerBand();
1130 if (pUnkSite
!= pSite
)
1138 hr
= IUnknown_GetWindow(pUnkSite
, &parentWnd
);
1141 ERR("Could not get parent's window ! Status: %08lx\n", hr
);
1142 return E_INVALIDARG
;
1149 // Change its parent
1150 SetParent(parentWnd
);
1154 HWND wnd
= CreateWindow(WC_TREEVIEW
, NULL
,
1155 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
1156 0, 0, 0, 0, parentWnd
, NULL
, _AtlBaseModule
.GetModuleInstance(), NULL
);
1158 // Subclass the window
1159 SubclassWindow(wnd
);
1161 // Initialize our treeview now
1162 InitializeExplorerBand();
1163 RegisterDragDrop(m_hWnd
, dynamic_cast<IDropTarget
*>(this));
1168 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSite(REFIID riid
, void **ppvSite
)
1177 // *** IOleCommandTarget methods ***
1178 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
1184 HRESULT STDMETHODCALLTYPE
CExplorerBand::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
1191 // *** IServiceProvider methods ***
1192 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
1194 /* FIXME: we probably want to handle more services here */
1195 return IUnknown_QueryService(pSite
, SID_SShellBrowser
, riid
, ppvObject
);
1199 // *** IInputObject methods ***
1200 HRESULT STDMETHODCALLTYPE
CExplorerBand::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
1207 // TODO: handle message
1210 TranslateMessage(lpMsg
);
1211 DispatchMessage(lpMsg
);
1216 HRESULT STDMETHODCALLTYPE
CExplorerBand::HasFocusIO()
1218 return bFocused
? S_OK
: S_FALSE
;
1221 HRESULT STDMETHODCALLTYPE
CExplorerBand::TranslateAcceleratorIO(LPMSG lpMsg
)
1223 if (lpMsg
->hwnd
== m_hWnd
)
1225 TranslateMessage(lpMsg
);
1226 DispatchMessage(lpMsg
);
1233 // *** IPersist methods ***
1234 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetClassID(CLSID
*pClassID
)
1238 memcpy(pClassID
, &CLSID_ExplorerBand
, sizeof(CLSID
));
1243 // *** IPersistStream methods ***
1244 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsDirty()
1250 HRESULT STDMETHODCALLTYPE
CExplorerBand::Load(IStream
*pStm
)
1256 HRESULT STDMETHODCALLTYPE
CExplorerBand::Save(IStream
*pStm
, BOOL fClearDirty
)
1262 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
1264 // TODO: calculate max size
1270 // *** IWinEventHandler methods ***
1271 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
1274 if (uMsg
== WM_NOTIFY
)
1276 NMHDR
*pNotifyHeader
= (NMHDR
*)lParam
;
1277 switch (pNotifyHeader
->code
)
1279 case TVN_ITEMEXPANDING
:
1280 *theResult
= OnTreeItemExpanding((LPNMTREEVIEW
)lParam
);
1282 case TVN_SELCHANGED
:
1283 OnSelectionChanged((LPNMTREEVIEW
)lParam
);
1285 case TVN_DELETEITEM
:
1286 OnTreeItemDeleted((LPNMTREEVIEW
)lParam
);
1289 OnContextMenu(WM_CONTEXTMENU
, (WPARAM
)m_hWnd
, GetMessagePos(), bHandled
);
1293 case TVN_BEGINRDRAG
:
1294 OnTreeItemDragging((LPNMTREEVIEW
)lParam
, pNotifyHeader
->code
== TVN_BEGINRDRAG
);
1295 case TVN_BEGINLABELEDITW
:
1297 // TODO: put this in a function ? (mostly copypasta from CDefView)
1298 DWORD dwAttr
= SFGAO_CANRENAME
;
1299 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1300 CComPtr
<IShellFolder
> pParent
;
1301 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
))
1317 case TVN_ENDLABELEDITW
:
1319 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1320 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1324 if (dispInfo
->item
.pszText
)
1326 LPITEMIDLIST pidlNew
;
1327 CComPtr
<IShellFolder
> pParent
;
1328 LPCITEMIDLIST pidlChild
;
1330 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pidlChild
);
1331 if (!SUCCEEDED(hr
) || !pParent
.p
)
1334 hr
= pParent
->SetNameOf(0, pidlChild
, dispInfo
->item
.pszText
, SHGDN_INFOLDER
, &pidlNew
);
1335 if(SUCCEEDED(hr
) && pidlNew
)
1337 CComPtr
<IPersistFolder2
> pPersist
;
1338 LPITEMIDLIST pidlParent
, pidlNewAbs
;
1340 hr
= pParent
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &pPersist
));
1344 hr
= pPersist
->GetCurFolder(&pidlParent
);
1347 pidlNewAbs
= ILCombine(pidlParent
, pidlNew
);
1349 // Navigate to our new location
1350 UpdateBrowser(pidlNewAbs
);
1366 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsWindowOwner(HWND hWnd
)
1368 return (hWnd
== m_hWnd
) ? S_OK
: S_FALSE
;
1371 // *** IBandNavigate methods ***
1372 HRESULT STDMETHODCALLTYPE
CExplorerBand::Select(long paramC
)
1378 // *** INamespaceProxy ***
1379 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetNavigateTarget(long paramC
, long param10
, long param14
)
1385 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(long paramC
)
1391 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnSelectionChanged(long paramC
)
1397 HRESULT STDMETHODCALLTYPE
CExplorerBand::RefreshFlags(long paramC
, long param10
, long param14
)
1403 HRESULT STDMETHODCALLTYPE
CExplorerBand::CacheItem(long paramC
)
1409 // *** IDispatch methods ***
1410 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfoCount(UINT
*pctinfo
)
1416 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfo(UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
1422 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetIDsOfNames(REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
1428 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1430 switch (dispIdMember
)
1432 case DISPID_DOWNLOADCOMPLETE
:
1433 case DISPID_NAVIGATECOMPLETE2
:
1434 TRACE("DISPID_NAVIGATECOMPLETE2 received\n");
1435 NavigateToCurrentFolder();
1438 TRACE("Unknown dispid requested: %08x\n", dispIdMember
);
1439 return E_INVALIDARG
;
1442 // *** IDropTarget methods ***
1443 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragEnter(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1445 ERR("Entering drag\n");
1447 oldSelected
= TreeView_GetSelection(m_hWnd
);
1448 return DragOver(glfKeyState
, pt
, pdwEffect
);
1451 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragOver(DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1454 CComPtr
<IShellFolder
> pShellFldr
;
1456 //LPCITEMIDLIST pChild;
1461 info
.flags
= TVHT_ONITEM
;
1463 ScreenToClient(&info
.pt
);
1465 // Move to the item selected by the treeview (don't change right pane)
1466 TreeView_HitTest(m_hWnd
, &info
);
1471 TreeView_SelectItem(m_hWnd
, info
.hItem
);
1472 bNavigating
= FALSE
;
1473 // Delegate to shell folder
1474 if (pDropTarget
&& info
.hItem
!= childTargetNode
)
1478 if (info
.hItem
!= childTargetNode
)
1480 nodeInfo
= GetNodeInfo(info
.hItem
);
1484 hr
= SHBindToParent(nodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pShellFldr
), &pChild
);
1487 hr
= pShellFldr
->GetUIObjectOf(m_hWnd
, 1, &pChild
, IID_IDropTarget
, NULL
, reinterpret_cast<void**>(&pDropTarget
));
1491 if(_ILIsDesktop(nodeInfo
->absolutePidl
))
1492 pShellFldr
= pDesktop
;
1495 hr
= pDesktop
->BindToObject(nodeInfo
->absolutePidl
, 0, IID_PPV_ARG(IShellFolder
, &pShellFldr
));
1498 /* Don't allow dnd since we couldn't get our folder object */
1499 ERR("Can't bind to folder object\n");
1500 *pdwEffect
= DROPEFFECT_NONE
;
1504 hr
= pShellFldr
->CreateViewObject(m_hWnd
, IID_PPV_ARG(IDropTarget
, &pDropTarget
));
1507 /* Don't allow dnd since we couldn't get our drop target */
1508 ERR("Can't get drop target for folder object\n");
1509 *pdwEffect
= DROPEFFECT_NONE
;
1512 hr
= pDropTarget
->DragEnter(pCurObject
, glfKeyState
, pt
, pdwEffect
);
1513 childTargetNode
= info
.hItem
;
1517 hr
= pDropTarget
->DragOver(glfKeyState
, pt
, pdwEffect
);
1522 childTargetNode
= NULL
;
1524 *pdwEffect
= DROPEFFECT_NONE
;
1529 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragLeave()
1532 TreeView_SelectItem(m_hWnd
, oldSelected
);
1533 bNavigating
= FALSE
;
1534 childTargetNode
= NULL
;
1542 HRESULT STDMETHODCALLTYPE
CExplorerBand::Drop(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1546 pDropTarget
->Drop(pObj
, glfKeyState
, pt
, pdwEffect
);
1551 // *** IDropSource methods ***
1552 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
)
1555 return DRAGDROP_S_CANCEL
;
1556 if ((grfKeyState
& MK_LBUTTON
) || (grfKeyState
& MK_RBUTTON
))
1558 return DRAGDROP_S_DROP
;
1561 HRESULT STDMETHODCALLTYPE
CExplorerBand::GiveFeedback(DWORD dwEffect
)
1563 return DRAGDROP_S_USEDEFAULTCURSORS
;