2 * Copyright 2003, 2004 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 ShellBrowserChild::ShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
46 // store path into history
47 if (info
._path
&& *info
._path
)
48 _url_history
.push(info
._path
);
51 ShellBrowserChild::~ShellBrowserChild()
54 _pShellView
->Release();
57 _pDropTarget
->Release();
63 LRESULT
ShellBrowserChild::Init(LPCREATESTRUCT pcs
)
65 CONTEXT("ShellBrowserChild::Init()");
70 _hWndFrame
= GetParent(pcs
->hwndParent
);
72 ClientRect
rect(_hwnd
);
76 _himlSmall
= (HIMAGELIST
)SHGetFileInfo(TEXT("C:\\"), 0, &sfi
, sizeof(SHFILEINFO
), SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
);
77 // _himlLarge = (HIMAGELIST)SHGetFileInfo(TEXT("C:\\"), 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_LARGEICON);
80 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORPARSING
);
82 _root
._drive_type
= DRIVE_UNKNOWN
;
83 lstrcpy(_root
._volname
, root_name
); // most of the time "Desktop"
85 lstrcpy(_root
._fs
, TEXT("Desktop"));
87 //@@ _root._entry->read_tree(shell_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME/*_sortOrder*/);
90 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
91 -> see FileChildWindow::FileChildWindow()
93 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
94 _root
._entry
->read_directory();
96 /* already filled by ShellDirectory constructor
97 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
100 // create explorer treeview
101 if (_create_info
._open_mode
& OWM_EXPLORE
)
102 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
103 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
104 0, rect
.top
, _split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
105 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
112 UpdateFolderView(_create_info
._shell_path
.get_folder());
118 void ShellBrowserChild::InitializeTree()
120 CONTEXT("ShellBrowserChild::InitializeTree()");
122 TreeView_SetImageList(_left_hwnd
, _himlSmall
, TVSIL_NORMAL
);
123 TreeView_SetScrollTime(_left_hwnd
, 100);
125 TV_INSERTSTRUCT tvInsert
;
127 tvInsert
.hParent
= 0;
128 tvInsert
.hInsertAfter
= TVI_LAST
;
130 TV_ITEM
& tvItem
= tvInsert
.item
;
131 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
132 tvItem
.lParam
= (LPARAM
)_root
._entry
;
133 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
134 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
135 tvItem
.cChildren
= 1;
137 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
138 TreeView_SelectItem(_left_hwnd
, hItem
);
139 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
143 bool ShellBrowserChild::InitDragDrop()
145 CONTEXT("ShellBrowserChild::InitDragDrop()");
147 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
152 _pDropTarget
->AddRef();
154 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
155 _pDropTarget
->Release(); // free TreeDropTarget
160 _pDropTarget
->Release();
164 ftetc
.dwAspect
= DVASPECT_CONTENT
;
166 ftetc
.tymed
= TYMED_HGLOBAL
;
167 ftetc
.cfFormat
= CF_HDROP
;
169 _pDropTarget
->AddSuportedFormat(ftetc
);
175 void ShellBrowserChild::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
177 CONTEXT("ShellBrowserChild::OnTreeItemRClick()");
181 GetCursorPos(&tvhti
.pt
);
182 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
184 tvhti
.flags
= LVHT_NOWHERE
;
185 TreeView_HitTest(_left_hwnd
, &tvhti
);
187 if (TVHT_ONITEM
& tvhti
.flags
) {
188 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
189 Tree_DoItemMenu(_left_hwnd
, tvhti
.hItem
, &tvhti
.pt
);
193 void ShellBrowserChild::Tree_DoItemMenu(HWND hwndTreeView
, HTREEITEM hItem
, LPPOINT pptScreen
)
195 CONTEXT("ShellBrowserChild::Tree_DoItemMenu()");
197 LPARAM itemData
= TreeView_GetItemData(hwndTreeView
, hItem
);
200 Entry
* entry
= (Entry
*)itemData
;
202 if (entry
->_etype
== ET_SHELL
) {
203 ShellDirectory
* dir
= static_cast<ShellDirectory
*>(entry
->_up
);
204 ShellFolder folder
= dir
? dir
->_folder
: GetDesktopFolder();
205 LPCITEMIDLIST pidl
= static_cast<ShellEntry
*>(entry
)->_pidl
;
207 CHECKERROR(ShellFolderContextMenu(folder
, ::GetParent(hwndTreeView
), 1, &pidl
, pptScreen
->x
, pptScreen
->y
));
209 ShellPath shell_path
= entry
->create_absolute_pidl();
210 LPCITEMIDLIST pidl
= shell_path
;
212 ///@todo use parent folder instead of desktop
213 CHECKERROR(ShellFolderContextMenu(GetDesktopFolder(), _hwnd
, 1, &pidl
, pptScreen
->x
, pptScreen
->y
));
218 void ShellBrowserChild::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
220 CONTEXT("ShellBrowserChild::OnTreeGetDispInfo()");
222 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
223 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
225 if (lpdi
->item
.mask
& TVIF_TEXT
)
226 lpdi
->item
.pszText
= entry
->_display_name
;
228 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
229 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
230 LPCITEMIDLIST pidl
= pidl_abs
;
234 if (lpdi->item.mask & TVIF_TEXT)
235 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
236 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); ///@todo look at cchTextMax if there is enough space available
238 lpdi->item.pszText = entry->_data.cFileName;
240 if (lpdi
->item
.mask
& TVIF_IMAGE
)
241 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_LINKOVERLAY
) == _himlSmall
)
242 lpdi
->item
.iImage
= sfi
.iIcon
;
244 lpdi
->item
.iImage
= -1;
246 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
247 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_OPENICON
) == _himlSmall
)
248 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
250 lpdi
->item
.iSelectedImage
= -1;
254 void ShellBrowserChild::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
256 CONTEXT("ShellBrowserChild::OnTreeItemExpanding()");
258 if (pnmtv
->action
== TVE_COLLAPSE
)
259 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
260 else if (pnmtv
->action
== TVE_EXPAND
) {
261 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
264 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
265 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
267 // remove subitem "+"
270 tvItem
.mask
= TVIF_CHILDREN
;
271 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
272 tvItem
.cChildren
= 0;
274 TreeView_SetItem(_left_hwnd
, &tvItem
);
279 int ShellBrowserChild::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
281 CONTEXT("ShellBrowserChild::InsertSubitems()");
287 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
291 } catch(COMException
& e
) {
292 HandleException(e
, g_Globals
._hMainWnd
);
296 TV_INSERTSTRUCT tvInsert
;
298 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
300 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
303 ZeroMemory(&tvItem
, sizeof(tvItem
));
305 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
306 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
307 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
308 tvItem
.lParam
= (LPARAM
)entry
;
309 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
311 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
312 tvItem
.mask
|= TVIF_STATE
;
313 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
314 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
317 tvInsert
.item
= tvItem
;
318 tvInsert
.hInsertAfter
= TVI_LAST
;
319 tvInsert
.hParent
= hParentItem
;
321 TreeView_InsertItem(_left_hwnd
, &tvInsert
);
327 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
332 void ShellBrowserChild::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
334 CONTEXT("ShellBrowserChild::OnTreeItemSelected()");
336 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
338 _last_sel
= pnmtv
->itemNew
.hItem
;
340 if (entry
->_etype
== ET_SHELL
) {
341 IShellFolder
* folder
;
343 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
344 folder
= static_cast<ShellDirectory
*>(entry
)->_folder
;
346 folder
= entry
->get_parent_folder();
353 TCHAR path
[MAX_PATH
];
355 if (entry
->get_path(path
)) {
359 url
.printf(TEXT("shell://%s"), path
);
361 url
.printf(TEXT("file://%s"), path
);
366 UpdateFolderView(folder
);
370 void ShellBrowserChild::UpdateFolderView(IShellFolder
* folder
)
372 CONTEXT("ShellBrowserChild::UpdateFolderView()");
375 IShellView
* pLastShellView
= _pShellView
;
380 pLastShellView
->GetCurrentInfo(&fs
);
382 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
383 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
386 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
393 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
394 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
396 if (pLastShellView
) {
397 pLastShellView
->GetCurrentInfo(&fs
);
398 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
399 pLastShellView
->DestroyViewWindow();
400 pLastShellView
->Release();
402 ClientRect
clnt(_hwnd
);
403 resize_children(clnt
.right
, clnt
.bottom
);
406 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
410 LRESULT
ShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
413 case WM_GETISHELLBROWSER
: // for Registry Explorer Plugin
414 return (LRESULT
)static_cast<IShellBrowser
*>(this);
416 case PM_GET_SHELLBROWSER_PTR
:
417 return (LRESULT
)this;
419 case PM_DISPATCH_COMMAND
: {
420 switch(LOWORD(wparam
)) {
421 case ID_WINDOW_NEW
: {CONTEXT("ShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
422 ShellBrowserChild::create(_create_info
);
426 //@todo refresh shell child
430 return super::WndProc(nmsg
, wparam
, lparam
);
435 return super::WndProc(nmsg
, wparam
, lparam
);
441 int ShellBrowserChild::Notify(int id
, NMHDR
* pnmh
)
444 case TVN_GETDISPINFO
: OnTreeGetDispInfo(id
, pnmh
); break;
445 case TVN_SELCHANGED
: OnTreeItemSelected(id
, (LPNMTREEVIEW
)pnmh
); break;
446 case TVN_ITEMEXPANDING
: OnTreeItemExpanding(id
, (LPNMTREEVIEW
)pnmh
); break;
447 case NM_RCLICK
: OnTreeItemRClick(id
, pnmh
); break;
448 default: return super::Notify(id
, pnmh
);
455 HRESULT
ShellBrowserChild::OnDefaultCommand(LPIDA pida
)
457 CONTEXT("ShellBrowserChild::OnDefaultCommand()");
459 if (pida
->cidl
>= 1) {
460 if (_left_hwnd
) { // explorer mode
462 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
466 parent
->smart_scan();
467 } catch(COMException
& e
) {
471 UINT firstOffset
= pida
->aoffset
[1];
472 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
474 Entry
* entry
= parent
->find_entry(pidl
);
476 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
477 if (entry
->_etype
== ET_SHELL
)
478 if (expand_folder(static_cast<ShellDirectory
*>(entry
)))
482 } else { // no tree control
483 if (MainFrame::OpenShellFolders(pida
, _hWndFrame
))
486 /* create new Frame Window
487 if (MainFrame::OpenShellFolders(pida, 0))
497 bool ShellBrowserChild::expand_folder(ShellDirectory
* entry
)
499 CONTEXT("ShellBrowserChild::expand_folder()");
501 //HTREEITEM hitem_sel = TreeView_GetSelection(_left_hwnd);
505 if (!TreeView_Expand(_left_hwnd
, _last_sel
, TVE_EXPAND
))
508 for(HTREEITEM hitem
=TreeView_GetChild(_left_hwnd
,_last_sel
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
509 if ((ShellDirectory
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
510 if (TreeView_SelectItem(_left_hwnd
, hitem
) &&
511 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
522 String
ShellBrowserChild::jump_to_int(LPCTSTR url
)
526 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
527 if (jump_to_pidl(ShellPath(url
+8)))
531 if (SplitFileSysURL(url
, dir
, fname
))
532 if (jump_to_pidl(ShellPath(dir
)))
533 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
539 bool ShellBrowserChild::jump_to_pidl(LPCITEMIDLIST pidl
)
543 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
544 -> see FileChildWindow::FileChildWindow()