2 * Copyright 2003 Martin Fuchs
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Martin Fuchs, 23.07.2003
29 #include "../utility/utility.h"
31 #include "../explorer.h"
32 #include "../globals.h"
34 #include "../explorer_intres.h"
37 static LPARAM
TreeView_GetItemData(HWND hwndTreeView
, HTREEITEM hItem
)
41 tvItem
.mask
= TVIF_PARAM
;
44 if (!TreeView_GetItem(hwndTreeView
, &tvItem
))
51 ShellBrowserChild::ShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
61 ShellBrowserChild::~ShellBrowserChild()
64 _pShellView
->Release();
67 _pDropTarget
->Release();
73 LRESULT
ShellBrowserChild::Init(LPCREATESTRUCT pcs
)
78 _hWndFrame
= GetParent(pcs
->hwndParent
);
80 ClientRect
rect(_hwnd
);
84 _himlSmall
= (HIMAGELIST
)SHGetFileInfo(TEXT("C:\\"), 0, &sfi
, sizeof(SHFILEINFO
), SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
);
85 // _himlLarge = (HIMAGELIST)SHGetFileInfo(TEXT("C:\\"), 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_LARGEICON);
88 // create explorer treeview
89 if (_create_info
._open_mode
& OWM_EXPLORE
)
90 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
91 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_NOTOOLTIPS
|TVS_SHOWSELALWAYS
,
92 0, rect
.top
, _split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
93 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
100 UpdateFolderView(_create_info
._shell_path
.get_folder());
106 void ShellBrowserChild::InitializeTree()
108 TreeView_SetImageList(_left_hwnd
, _himlSmall
, TVSIL_NORMAL
);
109 TreeView_SetScrollTime(_left_hwnd
, 100);
111 const String
& root_name
= Desktop().get_name(_create_info
._root_shell_path
);
113 _root
._drive_type
= DRIVE_UNKNOWN
;
114 lstrcpy(_root
._volname
, root_name
); // most of the time "Desktop"
116 lstrcpy(_root
._fs
, TEXT("Shell"));
118 //@@ _root._entry->read_tree(shell_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME/*_sortOrder*/);
121 we should call read_tree() here to iterate through the hierarchy and open all folders from shell_info._root_shell_path to shell_info._shell_path
122 -> see FileChildWindow::FileChildWindow()
124 _root
._entry
= new ShellDirectory(Desktop(), _create_info
._root_shell_path
, _hwnd
);
125 _root
._entry
->read_directory();
127 /* already filled by ShellDirectory constructor
128 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
133 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
134 tvItem
.lParam
= (LPARAM
)_root
._entry
;
135 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
136 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
137 tvItem
.cChildren
= 1;
139 TV_INSERTSTRUCT tvInsert
;
141 tvInsert
.hParent
= 0;
142 tvInsert
.hInsertAfter
= TVI_LAST
;
143 tvInsert
.item
= tvItem
;
145 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
146 TreeView_SelectItem(_left_hwnd
, hItem
);
147 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
151 bool ShellBrowserChild::InitDragDrop()
153 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
158 _pDropTarget
->AddRef();
160 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
161 _pDropTarget
->Release(); // free TreeDropTarget
166 _pDropTarget
->Release();
170 ftetc
.dwAspect
= DVASPECT_CONTENT
;
172 ftetc
.tymed
= TYMED_HGLOBAL
;
173 ftetc
.cfFormat
= CF_HDROP
;
175 _pDropTarget
->AddSuportedFormat(ftetc
);
181 void ShellBrowserChild::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
185 GetCursorPos(&tvhti
.pt
);
186 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
188 tvhti
.flags
= LVHT_NOWHERE
;
189 TreeView_HitTest(_left_hwnd
, &tvhti
);
191 if (TVHT_ONITEM
& tvhti
.flags
) {
192 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
193 Tree_DoItemMenu(_left_hwnd
, tvhti
.hItem
, &tvhti
.pt
);
197 void ShellBrowserChild::Tree_DoItemMenu(HWND hwndTreeView
, HTREEITEM hItem
, LPPOINT pptScreen
)
199 LPARAM itemData
= TreeView_GetItemData(hwndTreeView
, hItem
);
202 HWND hwndParent
= ::GetParent(hwndTreeView
);
203 Entry
* entry
= (Entry
*)itemData
;
205 IShellFolder
* shell_folder
;
208 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
209 dir
= static_cast<ShellDirectory
*>(entry
);
210 shell_folder
= dir
->_folder
;
212 dir
= static_cast<ShellDirectory
*>(entry
->_up
);
213 shell_folder
= dir
? dir
->_folder
: Desktop();
216 shell_folder
->AddRef();
219 LPCITEMIDLIST pidl
= static_cast<ShellEntry
*>(entry
)->_pidl
;
223 HRESULT hr
= shell_folder
->GetUIObjectOf(hwndParent
, 1, &pidl
, IID_IContextMenu
, NULL
, (LPVOID
*)&pcm
);
224 // HRESULT hr = CDefFolderMenu_Create2(dir?dir->_pidl:DesktopFolder(), hwndParent, 1, &pidl, shell_folder, NULL, 0, NULL, &pcm);
227 HMENU hPopup
= CreatePopupMenu();
230 hr
= pcm
->QueryContextMenu(hPopup
, 0, 1, 0x7fff, CMF_NORMAL
|CMF_EXPLORE
);
235 pcm
->QueryInterface(IID_IContextMenu2
, (LPVOID
*)&pcm2
);
237 UINT idCmd
= TrackPopupMenu(hPopup
,
238 TPM_LEFTALIGN
| TPM_RETURNCMD
| TPM_RIGHTBUTTON
,
251 CMINVOKECOMMANDINFO cmi
;
252 cmi
.cbSize
= sizeof(CMINVOKECOMMANDINFO
);
254 cmi
.hwnd
= hwndParent
;
255 cmi
.lpVerb
= (LPCSTR
)(INT_PTR
)(idCmd
- 1);
256 cmi
.lpParameters
= NULL
;
257 cmi
.lpDirectory
= NULL
;
258 cmi
.nShow
= SW_SHOWNORMAL
;
261 hr
= pcm
->InvokeCommand(&cmi
);
269 shell_folder
->Release();
274 void ShellBrowserChild::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
276 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
277 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
279 if (lpdi
->item
.mask
& TVIF_TEXT
)
280 lpdi
->item
.pszText
= entry
->_display_name
;
282 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
283 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
284 LPCITEMIDLIST pidl
= pidl_abs
;
288 if (lpdi->item.mask & TVIF_TEXT)
289 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
290 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); //TODO: look at cchTextMax if there is enough space available
292 lpdi->item.pszText = entry->_data.cFileName;
294 if (lpdi
->item
.mask
& TVIF_IMAGE
)
295 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_LINKOVERLAY
) == _himlSmall
)
296 lpdi
->item
.iImage
= sfi
.iIcon
;
298 lpdi
->item
.iImage
= -1;
300 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
301 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_OPENICON
) == _himlSmall
)
302 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
304 lpdi
->item
.iSelectedImage
= -1;
308 void ShellBrowserChild::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
310 if (pnmtv
->action
== TVE_COLLAPSE
)
311 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
312 else if (pnmtv
->action
== TVE_EXPAND
) {
313 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
316 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
317 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
319 // remove subitem "+"
322 tvItem
.mask
= TVIF_CHILDREN
;
323 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
324 tvItem
.cChildren
= 0;
326 TreeView_SetItem(_left_hwnd
, &tvItem
);
331 int ShellBrowserChild::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
337 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
341 } catch(COMException
& e
) {
342 HandleException(e
, g_Globals
._hMainWnd
);
346 TV_INSERTSTRUCT tvInsert
;
348 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
350 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
353 ZeroMemory(&tvItem
, sizeof(tvItem
));
355 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
356 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
357 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
358 tvItem
.lParam
= (LPARAM
)entry
;
359 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
361 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
362 tvItem
.mask
|= TVIF_STATE
;
363 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
364 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
367 tvInsert
.item
= tvItem
;
368 tvInsert
.hInsertAfter
= TVI_LAST
;
369 tvInsert
.hParent
= hParentItem
;
371 TreeView_InsertItem(_left_hwnd
, &tvInsert
);
377 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
382 void ShellBrowserChild::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
384 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
386 _last_sel
= pnmtv
->itemNew
.hItem
;
388 IShellFolder
* folder
;
390 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
391 folder
= static_cast<ShellDirectory
*>(entry
)->_folder
;
393 folder
= entry
->get_parent_folder();
400 UpdateFolderView(folder
);
403 void ShellBrowserChild::UpdateFolderView(IShellFolder
* folder
)
406 IShellView
* pLastShellView
= _pShellView
;
411 pLastShellView
->GetCurrentInfo(&fs
);
413 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
414 fs
.fFlags
= FWF_NOCLIENTEDGE
;
417 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
424 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
425 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
427 if (pLastShellView
) {
428 pLastShellView
->GetCurrentInfo(&fs
);
429 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
430 pLastShellView
->DestroyViewWindow();
431 pLastShellView
->Release();
433 ClientRect
clnt(_hwnd
);
434 resize_children(clnt
.right
, clnt
.bottom
);
437 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
441 LRESULT
ShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
444 case WM_GETISHELLBROWSER
: // for Registry Explorer Plugin
445 return (LRESULT
)static_cast<IShellBrowser
*>(this);
448 return super::WndProc(nmsg
, wparam
, lparam
);
454 int ShellBrowserChild::Notify(int id
, NMHDR
* pnmh
)
457 case TVN_GETDISPINFO
: OnTreeGetDispInfo(id
, pnmh
); break;
458 case TVN_ITEMEXPANDING
: OnTreeItemExpanding(id
, (LPNMTREEVIEW
)pnmh
); break;
459 case TVN_SELCHANGED
: OnTreeItemSelected(id
, (LPNMTREEVIEW
)pnmh
); break;
460 case NM_RCLICK
: OnTreeItemRClick(id
, pnmh
); break;
461 default: return super::Notify(id
, pnmh
);
468 HRESULT
ShellBrowserChild::OnDefaultCommand(LPIDA pIDList
)
470 if (pIDList
->cidl
>=1) {
471 if (_left_hwnd
) { // explorer mode
473 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
477 parent
->smart_scan();
478 } catch(COMException
& e
) {
482 UINT firstOffset
= pIDList
->aoffset
[1];
483 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pIDList
+firstOffset
);
485 Entry
* entry
= parent
->find_entry(pidl
);
487 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
488 if (expand_folder(static_cast<ShellDirectory
*>(entry
)))
492 } else { // no tree control
493 if (MainFrame::OpenShellFolders(pIDList
, _hWndFrame
))
496 /* create new Frame Window
497 if (MainFrame::OpenShellFolders(pIDList, 0))
507 bool ShellBrowserChild::expand_folder(ShellDirectory
* entry
)
509 //HTREEITEM hitem_sel = TreeView_GetSelection(_left_hwnd);
513 if (!TreeView_Expand(_left_hwnd
, _last_sel
, TVE_EXPAND
))
516 for(HTREEITEM hitem
=TreeView_GetChild(_left_hwnd
,_last_sel
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
517 if ((ShellDirectory
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
518 if (TreeView_SelectItem(_left_hwnd
, hitem
) &&
519 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))