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
];
577 if (!ILGetDisplayNameEx(psfParent
, pElt
, wszDisplayName
, ILGDN_INFOLDER
))
579 ERR("Failed to get node name\n");
583 /* Get the icon of the node */
584 INT iIcon
= SHMapPIDLToSystemImageListIndex(psfParent
, pEltRelative
, NULL
);
586 NodeInfo
* pChildInfo
= new NodeInfo
;
589 ERR("Failed to allocate NodeInfo\n");
593 // Store our node info
594 pChildInfo
->absolutePidl
= ILClone(pElt
);
595 pChildInfo
->relativePidl
= ILClone(pEltRelative
);
596 pChildInfo
->expanded
= FALSE
;
598 // Set up our treeview template
599 tvInsert
.hParent
= hParent
;
600 tvInsert
.hInsertAfter
= TVI_LAST
;
601 tvInsert
.item
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
602 tvInsert
.item
.cchTextMax
= MAX_PATH
;
603 tvInsert
.item
.pszText
= wszDisplayName
;
604 tvInsert
.item
.iImage
= tvInsert
.item
.iSelectedImage
= iIcon
;
605 tvInsert
.item
.cChildren
= (attrs
& SFGAO_HASSUBFOLDER
) ? 1 : 0;
606 tvInsert
.item
.lParam
= (LPARAM
)pChildInfo
;
608 htiCreated
= TreeView_InsertItem(m_hWnd
, &tvInsert
);
612 TVSORTCB sortCallback
;
613 sortCallback
.hParent
= hParent
;
614 sortCallback
.lpfnCompare
= CompareTreeItems
;
615 sortCallback
.lParam
= (LPARAM
)this;
616 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
622 /* This is the slow version of the above method */
623 HTREEITEM
CExplorerBand::InsertItem(HTREEITEM hParent
, LPITEMIDLIST pElt
, LPITEMIDLIST pEltRelative
, BOOL bSort
)
625 CComPtr
<IShellFolder
> psfFolder
;
626 HRESULT hr
= SHBindToParent(pElt
, IID_PPV_ARG(IShellFolder
, &psfFolder
), NULL
);
627 if (FAILED_UNEXPECTEDLY(hr
))
630 return InsertItem(hParent
, psfFolder
, pElt
, pEltRelative
, bSort
);
633 BOOL
CExplorerBand::InsertSubitems(HTREEITEM hItem
, NodeInfo
*pNodeInfo
)
635 CComPtr
<IEnumIDList
> pEnumIDList
;
636 LPITEMIDLIST pidlSub
;
642 CComPtr
<IShellFolder
> pFolder
;
643 TVSORTCB sortCallback
;
645 entry
= pNodeInfo
->absolutePidl
;
648 EnumFlags
= SHCONTF_FOLDERS
;
650 hr
= SHGetFolderLocation(m_hWnd
, CSIDL_DESKTOP
, NULL
, 0, &pidlSub
);
653 ERR("Can't get desktop PIDL !\n");
657 if (!pDesktop
->CompareIDs(NULL
, pidlSub
, entry
))
659 // We are the desktop, so use pDesktop as pFolder
664 // Get an IShellFolder of our pidl
665 hr
= pDesktop
->BindToObject(entry
, NULL
, IID_PPV_ARG(IShellFolder
, &pFolder
));
669 ERR("Can't bind folder to desktop !\n");
675 // TODO: handle hidden folders according to settings !
676 EnumFlags
|= SHCONTF_INCLUDEHIDDEN
;
678 // Enum through objects
679 hr
= pFolder
->EnumObjects(NULL
,EnumFlags
,&pEnumIDList
);
681 // avoid broken IShellFolder implementations that return null pointer with success
682 if (!SUCCEEDED(hr
) || !pEnumIDList
)
684 ERR("Can't enum the folder !\n");
688 /* Don't redraw while we add stuff into the tree */
689 SendMessage(WM_SETREDRAW
, FALSE
, 0);
690 while(SUCCEEDED(pEnumIDList
->Next(1, &pidlSub
, &fetched
)) && pidlSub
&& fetched
)
692 LPITEMIDLIST pidlSubComplete
;
693 pidlSubComplete
= ILCombine(entry
, pidlSub
);
695 if (InsertItem(hItem
, pFolder
, pidlSubComplete
, pidlSub
, FALSE
))
697 ILFree(pidlSubComplete
);
700 pNodeInfo
->expanded
= TRUE
;
701 /* Let's do sorting */
702 sortCallback
.hParent
= hItem
;
703 sortCallback
.lpfnCompare
= CompareTreeItems
;
704 sortCallback
.lParam
= (LPARAM
)this;
705 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
707 /* Now we can redraw */
708 SendMessage(WM_SETREDRAW
, TRUE
, 0);
710 return (uItemCount
> 0) ? TRUE
: FALSE
;
714 * Navigate to a given PIDL in the treeview, and return matching tree item handle
715 * - dest: The absolute PIDL we should navigate in the treeview
716 * - item: Handle of the tree item matching the PIDL
717 * - bExpand: expand collapsed nodes in order to find the right element
718 * - bInsert: insert the element at the right place if we don't find it
719 * - bSelect: select the item after we found it
721 BOOL
CExplorerBand::NavigateToPIDL(LPITEMIDLIST dest
, HTREEITEM
*item
, BOOL bExpand
, BOOL bInsert
,
729 LPITEMIDLIST relativeChild
;
740 nodeData
= GetNodeInfo(current
);
743 ERR("Something has gone wrong, no data associated to node !\n");
747 // If we found our node, give it back
748 if (!pDesktop
->CompareIDs(0, nodeData
->absolutePidl
, dest
))
751 TreeView_SelectItem(m_hWnd
, current
);
756 // Check if we are a parent of the requested item
757 relativeChild
= ILFindChild(nodeData
->absolutePidl
, dest
);
758 if (relativeChild
!= 0)
760 // Notify treeview we have children
761 tvItem
.mask
= TVIF_CHILDREN
;
762 tvItem
.hItem
= current
;
763 tvItem
.cChildren
= 1;
764 TreeView_SetItem(m_hWnd
, &tvItem
);
766 // If we can expand and the node isn't expanded yet, do it
769 if (!nodeData
->expanded
)
770 InsertSubitems(current
, nodeData
);
771 TreeView_Expand(m_hWnd
, current
, TVE_EXPAND
);
774 // Try to get a child
775 tmp
= TreeView_GetChild(m_hWnd
, current
);
778 // We have a child, let's continue with it
784 if (bInsert
&& nodeData
->expanded
)
786 // Happens when we have to create a subchild inside a child
787 current
= InsertItem(current
, dest
, relativeChild
, TRUE
);
789 // We end up here, without any children, so we found nothing
790 // Tell the parent node it has children
791 ZeroMemory(&tvItem
, sizeof(tvItem
));
797 tmp
= TreeView_GetNextSibling(m_hWnd
, current
);
805 current
= InsertItem(parent
, dest
, ILFindLastID(dest
), TRUE
);
815 BOOL
CExplorerBand::NavigateToCurrentFolder()
817 LPITEMIDLIST explorerPidl
;
818 CComPtr
<IBrowserService
> pBrowserService
;
824 hr
= IUnknown_QueryService(pSite
, SID_STopLevelBrowser
, IID_PPV_ARG(IBrowserService
, &pBrowserService
));
827 ERR("Can't get IBrowserService !\n");
831 hr
= pBrowserService
->GetPidl(&explorerPidl
);
832 if (!SUCCEEDED(hr
) || !explorerPidl
)
834 ERR("Unable to get browser PIDL !\n");
838 /* find PIDL into our explorer */
839 result
= NavigateToPIDL(explorerPidl
, &dummy
, TRUE
, FALSE
, TRUE
);
844 BOOL
CExplorerBand::DeleteItem(LPITEMIDLIST idl
)
848 HTREEITEM parentNode
;
850 if (!NavigateToPIDL(idl
, &toDelete
, FALSE
, FALSE
, FALSE
))
853 // TODO: check that the treeview item is really deleted
855 parentNode
= TreeView_GetParent(m_hWnd
, toDelete
);
856 // Navigate to parent when deleting child item
857 if (!pDesktop
->CompareIDs(0, idl
, pidlCurrent
))
859 TreeView_SelectItem(m_hWnd
, parentNode
);
862 // Remove the child item
863 TreeView_DeleteItem(m_hWnd
, toDelete
);
864 // Probe parent to see if it has children
865 if (!TreeView_GetChild(m_hWnd
, parentNode
))
867 // Decrement parent's child count
868 ZeroMemory(&tvItem
, sizeof(tvItem
));
869 tvItem
.mask
= TVIF_CHILDREN
;
870 tvItem
.hItem
= parentNode
;
871 tvItem
.cChildren
= 0;
872 TreeView_SetItem(m_hWnd
, &tvItem
);
877 BOOL
CExplorerBand::RenameItem(HTREEITEM toRename
, LPITEMIDLIST newPidl
)
879 WCHAR wszDisplayName
[MAX_PATH
];
881 LPCITEMIDLIST relPidl
;
883 TVSORTCB sortCallback
;
886 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
887 itemInfo
.mask
= TVIF_PARAM
;
888 itemInfo
.hItem
= toRename
;
890 // Change PIDL associated to the item
891 relPidl
= ILFindLastID(newPidl
);
892 TreeView_GetItem(m_hWnd
, &itemInfo
);
893 if (!itemInfo
.lParam
)
895 ERR("Unable to fetch lParam\n");
898 SendMessage(WM_SETREDRAW
, FALSE
, 0);
899 treeInfo
= (NodeInfo
*)itemInfo
.lParam
;
900 ILFree(treeInfo
->absolutePidl
);
901 ILFree(treeInfo
->relativePidl
);
902 treeInfo
->absolutePidl
= ILClone(newPidl
);
903 treeInfo
->relativePidl
= ILClone(relPidl
);
905 // Change the display name
906 GetDisplayName(newPidl
, wszDisplayName
, MAX_PATH
, SHGDN_INFOLDER
);
907 ZeroMemory(&itemInfo
, sizeof(itemInfo
));
908 itemInfo
.hItem
= toRename
;
909 itemInfo
.mask
= TVIF_TEXT
;
910 itemInfo
.pszText
= wszDisplayName
;
911 TreeView_SetItem(m_hWnd
, &itemInfo
);
913 if((child
= TreeView_GetChild(m_hWnd
, toRename
)) != NULL
)
915 RefreshTreePidl(child
, newPidl
);
919 sortCallback
.hParent
= TreeView_GetParent(m_hWnd
, toRename
);
920 sortCallback
.lpfnCompare
= CompareTreeItems
;
921 sortCallback
.lParam
= (LPARAM
)this;
922 SendMessage(TVM_SORTCHILDRENCB
, 0, (LPARAM
)&sortCallback
);
923 SendMessage(WM_SETREDRAW
, TRUE
, 0);
927 BOOL
CExplorerBand::RefreshTreePidl(HTREEITEM tree
, LPITEMIDLIST pidlParent
)
932 // Update our node data
933 pInfo
= GetNodeInfo(tree
);
936 WARN("No tree info !\n");
939 ILFree(pInfo
->absolutePidl
);
940 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
941 if (!pInfo
->absolutePidl
)
943 WARN("PIDL allocation failed\n");
946 // Recursively update children
947 if ((tmp
= TreeView_GetChild(m_hWnd
, tree
)) != NULL
)
949 RefreshTreePidl(tmp
, pInfo
->absolutePidl
);
952 tmp
= TreeView_GetNextSibling(m_hWnd
, tree
);
955 pInfo
= GetNodeInfo(tmp
);
958 WARN("No tree info !\n");
961 ILFree(pInfo
->absolutePidl
);
962 pInfo
->absolutePidl
= ILCombine(pidlParent
, pInfo
->relativePidl
);
963 tmp
= TreeView_GetNextSibling(m_hWnd
, tmp
);
968 // *** Tree item sorting callback ***
969 int CALLBACK
CExplorerBand::CompareTreeItems(LPARAM p1
, LPARAM p2
, LPARAM p3
)
972 * We first sort drive letters (Path root), then PIDLs and then regular folder
974 * This is not how Windows sorts item, but it gives decent results.
978 CExplorerBand
*pThis
;
979 WCHAR wszFolder1
[MAX_PATH
];
980 WCHAR wszFolder2
[MAX_PATH
];
982 info1
= (NodeInfo
*)p1
;
983 info2
= (NodeInfo
*)p2
;
984 pThis
= (CExplorerBand
*)p3
;
986 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_FORPARSING
);
987 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_FORPARSING
);
988 if (PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
990 return lstrcmpiW(wszFolder1
,wszFolder2
);
992 if (PathIsRoot(wszFolder1
) && !PathIsRoot(wszFolder2
))
996 if (!PathIsRoot(wszFolder1
) && PathIsRoot(wszFolder2
))
1000 // Now, we compare non-root folders, grab display name
1001 GetDisplayName(info1
->absolutePidl
, wszFolder1
, MAX_PATH
, SHGDN_INFOLDER
);
1002 GetDisplayName(info2
->absolutePidl
, wszFolder2
, MAX_PATH
, SHGDN_INFOLDER
);
1004 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1008 if (!_ILIsSpecialFolder(info1
->relativePidl
) && _ILIsSpecialFolder(info2
->relativePidl
))
1012 if (_ILIsSpecialFolder(info1
->relativePidl
) && !_ILIsSpecialFolder(info2
->relativePidl
))
1015 hr
= pThis
->pDesktop
->CompareIDs(0, info1
->absolutePidl
, info2
->absolutePidl
);
1017 return (hr
> 0) ? -1 : 1;
1019 return StrCmpLogicalW(wszFolder1
, wszFolder2
);
1022 // *** IOleWindow methods ***
1023 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetWindow(HWND
*lphwnd
)
1026 return E_INVALIDARG
;
1031 HRESULT STDMETHODCALLTYPE
CExplorerBand::ContextSensitiveHelp(BOOL fEnterMode
)
1038 // *** IDockingWindow methods ***
1039 HRESULT STDMETHODCALLTYPE
CExplorerBand::CloseDW(DWORD dwReserved
)
1041 // We do nothing, we don't have anything to save yet
1042 TRACE("CloseDW called\n");
1046 HRESULT STDMETHODCALLTYPE
CExplorerBand::ResizeBorderDW(const RECT
*prcBorder
, IUnknown
*punkToolbarSite
, BOOL fReserved
)
1048 /* Must return E_NOTIMPL according to MSDN */
1052 HRESULT STDMETHODCALLTYPE
CExplorerBand::ShowDW(BOOL fShow
)
1060 // *** IDeskBand methods ***
1061 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetBandInfo(DWORD dwBandID
, DWORD dwViewMode
, DESKBANDINFO
*pdbi
)
1065 return E_INVALIDARG
;
1067 this->dwBandID
= dwBandID
;
1069 if (pdbi
->dwMask
& DBIM_MINSIZE
)
1071 pdbi
->ptMinSize
.x
= 200;
1072 pdbi
->ptMinSize
.y
= 30;
1075 if (pdbi
->dwMask
& DBIM_MAXSIZE
)
1077 pdbi
->ptMaxSize
.y
= -1;
1080 if (pdbi
->dwMask
& DBIM_INTEGRAL
)
1082 pdbi
->ptIntegral
.y
= 1;
1085 if (pdbi
->dwMask
& DBIM_ACTUAL
)
1087 pdbi
->ptActual
.x
= 200;
1088 pdbi
->ptActual
.y
= 30;
1091 if (pdbi
->dwMask
& DBIM_TITLE
)
1093 if (!LoadStringW(_AtlBaseModule
.GetResourceInstance(), IDS_FOLDERSLABEL
, pdbi
->wszTitle
, _countof(pdbi
->wszTitle
)))
1094 return HRESULT_FROM_WIN32(GetLastError());
1097 if (pdbi
->dwMask
& DBIM_MODEFLAGS
)
1099 pdbi
->dwModeFlags
= DBIMF_NORMAL
| DBIMF_VARIABLEHEIGHT
;
1102 if (pdbi
->dwMask
& DBIM_BKCOLOR
)
1104 pdbi
->dwMask
&= ~DBIM_BKCOLOR
;
1110 // *** IObjectWithSite methods ***
1111 HRESULT STDMETHODCALLTYPE
CExplorerBand::SetSite(IUnknown
*pUnkSite
)
1116 if (pUnkSite
== pSite
)
1119 TRACE("SetSite called \n");
1122 DestroyExplorerBand();
1127 if (pUnkSite
!= pSite
)
1135 hr
= IUnknown_GetWindow(pUnkSite
, &parentWnd
);
1138 ERR("Could not get parent's window ! Status: %08lx\n", hr
);
1139 return E_INVALIDARG
;
1146 // Change its parent
1147 SetParent(parentWnd
);
1151 HWND wnd
= CreateWindow(WC_TREEVIEW
, NULL
,
1152 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
1153 0, 0, 0, 0, parentWnd
, NULL
, _AtlBaseModule
.GetModuleInstance(), NULL
);
1155 // Subclass the window
1156 SubclassWindow(wnd
);
1158 // Initialize our treeview now
1159 InitializeExplorerBand();
1160 RegisterDragDrop(m_hWnd
, dynamic_cast<IDropTarget
*>(this));
1165 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSite(REFIID riid
, void **ppvSite
)
1174 // *** IOleCommandTarget methods ***
1175 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
1181 HRESULT STDMETHODCALLTYPE
CExplorerBand::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
1188 // *** IServiceProvider methods ***
1189 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryService(REFGUID guidService
, REFIID riid
, void **ppvObject
)
1191 /* FIXME: we probably want to handle more services here */
1192 return IUnknown_QueryService(pSite
, SID_SShellBrowser
, riid
, ppvObject
);
1196 // *** IInputObject methods ***
1197 HRESULT STDMETHODCALLTYPE
CExplorerBand::UIActivateIO(BOOL fActivate
, LPMSG lpMsg
)
1204 // TODO: handle message
1207 TranslateMessage(lpMsg
);
1208 DispatchMessage(lpMsg
);
1213 HRESULT STDMETHODCALLTYPE
CExplorerBand::HasFocusIO()
1215 return bFocused
? S_OK
: S_FALSE
;
1218 HRESULT STDMETHODCALLTYPE
CExplorerBand::TranslateAcceleratorIO(LPMSG lpMsg
)
1220 if (lpMsg
->hwnd
== m_hWnd
)
1222 TranslateMessage(lpMsg
);
1223 DispatchMessage(lpMsg
);
1230 // *** IPersist methods ***
1231 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetClassID(CLSID
*pClassID
)
1235 memcpy(pClassID
, &CLSID_ExplorerBand
, sizeof(CLSID
));
1240 // *** IPersistStream methods ***
1241 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsDirty()
1247 HRESULT STDMETHODCALLTYPE
CExplorerBand::Load(IStream
*pStm
)
1253 HRESULT STDMETHODCALLTYPE
CExplorerBand::Save(IStream
*pStm
, BOOL fClearDirty
)
1259 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetSizeMax(ULARGE_INTEGER
*pcbSize
)
1261 // TODO: calculate max size
1267 // *** IWinEventHandler methods ***
1268 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnWinEvent(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*theResult
)
1271 if (uMsg
== WM_NOTIFY
)
1273 NMHDR
*pNotifyHeader
= (NMHDR
*)lParam
;
1274 switch (pNotifyHeader
->code
)
1276 case TVN_ITEMEXPANDING
:
1277 *theResult
= OnTreeItemExpanding((LPNMTREEVIEW
)lParam
);
1279 case TVN_SELCHANGED
:
1280 OnSelectionChanged((LPNMTREEVIEW
)lParam
);
1282 case TVN_DELETEITEM
:
1283 OnTreeItemDeleted((LPNMTREEVIEW
)lParam
);
1286 OnContextMenu(WM_CONTEXTMENU
, (WPARAM
)m_hWnd
, GetMessagePos(), bHandled
);
1290 case TVN_BEGINRDRAG
:
1291 OnTreeItemDragging((LPNMTREEVIEW
)lParam
, pNotifyHeader
->code
== TVN_BEGINRDRAG
);
1292 case TVN_BEGINLABELEDITW
:
1294 // TODO: put this in a function ? (mostly copypasta from CDefView)
1295 DWORD dwAttr
= SFGAO_CANRENAME
;
1296 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1297 CComPtr
<IShellFolder
> pParent
;
1298 LPCITEMIDLIST pChild
;
1302 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1305 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pChild
);
1306 if (!SUCCEEDED(hr
) || !pParent
.p
)
1309 hr
= pParent
->GetAttributesOf(1, &pChild
, &dwAttr
);
1310 if (SUCCEEDED(hr
) && (dwAttr
& SFGAO_CANRENAME
))
1314 case TVN_ENDLABELEDITW
:
1316 LPNMTVDISPINFO dispInfo
= (LPNMTVDISPINFO
)lParam
;
1317 NodeInfo
*info
= GetNodeInfo(dispInfo
->item
.hItem
);
1321 if (dispInfo
->item
.pszText
)
1323 LPITEMIDLIST pidlNew
;
1324 CComPtr
<IShellFolder
> pParent
;
1325 LPCITEMIDLIST pidlChild
;
1327 hr
= SHBindToParent(info
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pParent
), &pidlChild
);
1328 if (!SUCCEEDED(hr
) || !pParent
.p
)
1331 hr
= pParent
->SetNameOf(0, pidlChild
, dispInfo
->item
.pszText
, SHGDN_INFOLDER
, &pidlNew
);
1332 if(SUCCEEDED(hr
) && pidlNew
)
1334 CComPtr
<IPersistFolder2
> pPersist
;
1335 LPITEMIDLIST pidlParent
, pidlNewAbs
;
1337 hr
= pParent
->QueryInterface(IID_PPV_ARG(IPersistFolder2
, &pPersist
));
1341 hr
= pPersist
->GetCurFolder(&pidlParent
);
1344 pidlNewAbs
= ILCombine(pidlParent
, pidlNew
);
1346 // Navigate to our new location
1347 UpdateBrowser(pidlNewAbs
);
1363 HRESULT STDMETHODCALLTYPE
CExplorerBand::IsWindowOwner(HWND hWnd
)
1365 return (hWnd
== m_hWnd
) ? S_OK
: S_FALSE
;
1368 // *** IBandNavigate methods ***
1369 HRESULT STDMETHODCALLTYPE
CExplorerBand::Select(long paramC
)
1375 // *** INamespaceProxy ***
1376 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetNavigateTarget(long paramC
, long param10
, long param14
)
1382 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(long paramC
)
1388 HRESULT STDMETHODCALLTYPE
CExplorerBand::OnSelectionChanged(long paramC
)
1394 HRESULT STDMETHODCALLTYPE
CExplorerBand::RefreshFlags(long paramC
, long param10
, long param14
)
1400 HRESULT STDMETHODCALLTYPE
CExplorerBand::CacheItem(long paramC
)
1406 // *** IDispatch methods ***
1407 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfoCount(UINT
*pctinfo
)
1413 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetTypeInfo(UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
1419 HRESULT STDMETHODCALLTYPE
CExplorerBand::GetIDsOfNames(REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
1425 HRESULT STDMETHODCALLTYPE
CExplorerBand::Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1427 switch (dispIdMember
)
1429 case DISPID_DOWNLOADCOMPLETE
:
1430 case DISPID_NAVIGATECOMPLETE2
:
1431 TRACE("DISPID_NAVIGATECOMPLETE2 received\n");
1432 NavigateToCurrentFolder();
1435 TRACE("Unknown dispid requested: %08x\n", dispIdMember
);
1436 return E_INVALIDARG
;
1439 // *** IDropTarget methods ***
1440 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragEnter(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1442 ERR("Entering drag\n");
1444 oldSelected
= TreeView_GetSelection(m_hWnd
);
1445 return DragOver(glfKeyState
, pt
, pdwEffect
);
1448 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragOver(DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1451 CComPtr
<IShellFolder
> pShellFldr
;
1453 //LPCITEMIDLIST pChild;
1458 info
.flags
= TVHT_ONITEM
;
1460 ScreenToClient(&info
.pt
);
1462 // Move to the item selected by the treeview (don't change right pane)
1463 TreeView_HitTest(m_hWnd
, &info
);
1468 TreeView_SelectItem(m_hWnd
, info
.hItem
);
1469 bNavigating
= FALSE
;
1470 // Delegate to shell folder
1471 if (pDropTarget
&& info
.hItem
!= childTargetNode
)
1475 if (info
.hItem
!= childTargetNode
)
1477 nodeInfo
= GetNodeInfo(info
.hItem
);
1481 hr
= SHBindToParent(nodeInfo
->absolutePidl
, IID_PPV_ARG(IShellFolder
, &pShellFldr
), &pChild
);
1484 hr
= pShellFldr
->GetUIObjectOf(m_hWnd
, 1, &pChild
, IID_IDropTarget
, NULL
, reinterpret_cast<void**>(&pDropTarget
));
1488 if(_ILIsDesktop(nodeInfo
->absolutePidl
))
1489 pShellFldr
= pDesktop
;
1492 hr
= pDesktop
->BindToObject(nodeInfo
->absolutePidl
, 0, IID_PPV_ARG(IShellFolder
, &pShellFldr
));
1495 /* Don't allow dnd since we couldn't get our folder object */
1496 ERR("Can't bind to folder object\n");
1497 *pdwEffect
= DROPEFFECT_NONE
;
1501 hr
= pShellFldr
->CreateViewObject(m_hWnd
, IID_PPV_ARG(IDropTarget
, &pDropTarget
));
1504 /* Don't allow dnd since we couldn't get our drop target */
1505 ERR("Can't get drop target for folder object\n");
1506 *pdwEffect
= DROPEFFECT_NONE
;
1509 hr
= pDropTarget
->DragEnter(pCurObject
, glfKeyState
, pt
, pdwEffect
);
1510 childTargetNode
= info
.hItem
;
1514 hr
= pDropTarget
->DragOver(glfKeyState
, pt
, pdwEffect
);
1519 childTargetNode
= NULL
;
1521 *pdwEffect
= DROPEFFECT_NONE
;
1526 HRESULT STDMETHODCALLTYPE
CExplorerBand::DragLeave()
1529 TreeView_SelectItem(m_hWnd
, oldSelected
);
1530 bNavigating
= FALSE
;
1531 childTargetNode
= NULL
;
1539 HRESULT STDMETHODCALLTYPE
CExplorerBand::Drop(IDataObject
*pObj
, DWORD glfKeyState
, POINTL pt
, DWORD
*pdwEffect
)
1543 pDropTarget
->Drop(pObj
, glfKeyState
, pt
, pdwEffect
);
1548 // *** IDropSource methods ***
1549 HRESULT STDMETHODCALLTYPE
CExplorerBand::QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
)
1552 return DRAGDROP_S_CANCEL
;
1553 if ((grfKeyState
& MK_LBUTTON
) || (grfKeyState
& MK_RBUTTON
))
1555 return DRAGDROP_S_DROP
;
1558 HRESULT STDMETHODCALLTYPE
CExplorerBand::GiveFeedback(DWORD dwEffect
)
1560 return DRAGDROP_S_USEDEFAULTCURSORS
;