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 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
)
75 CONTEXT("ShellBrowserChild::Init()");
80 _hWndFrame
= GetParent(pcs
->hwndParent
);
82 ClientRect
rect(_hwnd
);
86 _himlSmall
= (HIMAGELIST
)SHGetFileInfo(TEXT("C:\\"), 0, &sfi
, sizeof(SHFILEINFO
), SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
);
87 // _himlLarge = (HIMAGELIST)SHGetFileInfo(TEXT("C:\\"), 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX|SHGFI_LARGEICON);
90 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORPARSING
);
92 _root
._drive_type
= DRIVE_UNKNOWN
;
93 lstrcpy(_root
._volname
, root_name
); // most of the time "Desktop"
95 lstrcpy(_root
._fs
, TEXT("Desktop"));
97 //@@ _root._entry->read_tree(shell_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME/*_sortOrder*/);
100 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
101 -> see FileChildWindow::FileChildWindow()
103 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
104 _root
._entry
->read_directory();
106 /* already filled by ShellDirectory constructor
107 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
110 // create explorer treeview
111 if (_create_info
._open_mode
& OWM_EXPLORE
)
112 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
113 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_NOTOOLTIPS
|TVS_SHOWSELALWAYS
,
114 0, rect
.top
, _split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
115 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
122 UpdateFolderView(_create_info
._shell_path
.get_folder());
128 void ShellBrowserChild::InitializeTree()
130 CONTEXT("ShellBrowserChild::InitializeTree()");
132 TreeView_SetImageList(_left_hwnd
, _himlSmall
, TVSIL_NORMAL
);
133 TreeView_SetScrollTime(_left_hwnd
, 100);
137 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
138 tvItem
.lParam
= (LPARAM
)_root
._entry
;
139 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
140 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
141 tvItem
.cChildren
= 1;
143 TV_INSERTSTRUCT tvInsert
;
145 tvInsert
.hParent
= 0;
146 tvInsert
.hInsertAfter
= TVI_LAST
;
147 tvInsert
.item
= tvItem
;
149 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
150 TreeView_SelectItem(_left_hwnd
, hItem
);
151 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
155 bool ShellBrowserChild::InitDragDrop()
157 CONTEXT("ShellBrowserChild::InitDragDrop()");
159 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
164 _pDropTarget
->AddRef();
166 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
167 _pDropTarget
->Release(); // free TreeDropTarget
172 _pDropTarget
->Release();
176 ftetc
.dwAspect
= DVASPECT_CONTENT
;
178 ftetc
.tymed
= TYMED_HGLOBAL
;
179 ftetc
.cfFormat
= CF_HDROP
;
181 _pDropTarget
->AddSuportedFormat(ftetc
);
187 void ShellBrowserChild::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
189 CONTEXT("ShellBrowserChild::OnTreeItemRClick()");
193 GetCursorPos(&tvhti
.pt
);
194 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
196 tvhti
.flags
= LVHT_NOWHERE
;
197 TreeView_HitTest(_left_hwnd
, &tvhti
);
199 if (TVHT_ONITEM
& tvhti
.flags
) {
200 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
201 Tree_DoItemMenu(_left_hwnd
, tvhti
.hItem
, &tvhti
.pt
);
205 void ShellBrowserChild::Tree_DoItemMenu(HWND hwndTreeView
, HTREEITEM hItem
, LPPOINT pptScreen
)
207 CONTEXT("ShellBrowserChild::Tree_DoItemMenu()");
209 LPARAM itemData
= TreeView_GetItemData(hwndTreeView
, hItem
);
212 Entry
* entry
= (Entry
*)itemData
;
214 if (entry
->_etype
== ET_SHELL
) {
215 ShellDirectory
* dir
= static_cast<ShellDirectory
*>(entry
->_up
);
216 ShellFolder folder
= dir
? dir
->_folder
: GetDesktopFolder();
217 LPCITEMIDLIST pidl
= static_cast<ShellEntry
*>(entry
)->_pidl
;
219 CHECKERROR(ShellFolderContextMenu(folder
, ::GetParent(hwndTreeView
), 1, &pidl
, pptScreen
->x
, pptScreen
->y
));
221 ShellPath shell_path
= entry
->create_absolute_pidl();
222 LPCITEMIDLIST pidl
= shell_path
;
224 ///@todo use parent folder instead of desktop
225 CHECKERROR(ShellFolderContextMenu(GetDesktopFolder(), _hwnd
, 1, &pidl
, pptScreen
->x
, pptScreen
->y
));
230 void ShellBrowserChild::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
232 CONTEXT("ShellBrowserChild::OnTreeGetDispInfo()");
234 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
235 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
237 if (lpdi
->item
.mask
& TVIF_TEXT
)
238 lpdi
->item
.pszText
= entry
->_display_name
;
240 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
241 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
242 LPCITEMIDLIST pidl
= pidl_abs
;
246 if (lpdi->item.mask & TVIF_TEXT)
247 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
248 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); ///@todo look at cchTextMax if there is enough space available
250 lpdi->item.pszText = entry->_data.cFileName;
252 if (lpdi
->item
.mask
& TVIF_IMAGE
)
253 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_LINKOVERLAY
) == _himlSmall
)
254 lpdi
->item
.iImage
= sfi
.iIcon
;
256 lpdi
->item
.iImage
= -1;
258 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
259 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_OPENICON
) == _himlSmall
)
260 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
262 lpdi
->item
.iSelectedImage
= -1;
266 void ShellBrowserChild::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
268 CONTEXT("ShellBrowserChild::OnTreeItemExpanding()");
270 if (pnmtv
->action
== TVE_COLLAPSE
)
271 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
272 else if (pnmtv
->action
== TVE_EXPAND
) {
273 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
276 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
277 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
279 // remove subitem "+"
282 tvItem
.mask
= TVIF_CHILDREN
;
283 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
284 tvItem
.cChildren
= 0;
286 TreeView_SetItem(_left_hwnd
, &tvItem
);
291 int ShellBrowserChild::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
293 CONTEXT("ShellBrowserChild::InsertSubitems()");
299 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
303 } catch(COMException
& e
) {
304 HandleException(e
, g_Globals
._hMainWnd
);
308 TV_INSERTSTRUCT tvInsert
;
310 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
312 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
315 ZeroMemory(&tvItem
, sizeof(tvItem
));
317 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
318 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
319 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
320 tvItem
.lParam
= (LPARAM
)entry
;
321 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
323 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
324 tvItem
.mask
|= TVIF_STATE
;
325 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
326 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
329 tvInsert
.item
= tvItem
;
330 tvInsert
.hInsertAfter
= TVI_LAST
;
331 tvInsert
.hParent
= hParentItem
;
333 TreeView_InsertItem(_left_hwnd
, &tvInsert
);
339 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
344 void ShellBrowserChild::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
346 CONTEXT("ShellBrowserChild::OnTreeItemSelected()");
348 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
350 _last_sel
= pnmtv
->itemNew
.hItem
;
352 if (entry
->_etype
== ET_SHELL
) {
353 IShellFolder
* folder
;
355 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
356 folder
= static_cast<ShellDirectory
*>(entry
)->_folder
;
358 folder
= entry
->get_parent_folder();
365 UpdateFolderView(folder
);
369 void ShellBrowserChild::UpdateFolderView(IShellFolder
* folder
)
371 CONTEXT("ShellBrowserChild::UpdateFolderView()");
374 IShellView
* pLastShellView
= _pShellView
;
379 pLastShellView
->GetCurrentInfo(&fs
);
381 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
382 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
385 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
392 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
393 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
395 if (pLastShellView
) {
396 pLastShellView
->GetCurrentInfo(&fs
);
397 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
398 pLastShellView
->DestroyViewWindow();
399 pLastShellView
->Release();
401 ClientRect
clnt(_hwnd
);
402 resize_children(clnt
.right
, clnt
.bottom
);
405 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
409 LRESULT
ShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
412 case WM_GETISHELLBROWSER
: // for Registry Explorer Plugin
413 return (LRESULT
)static_cast<IShellBrowser
*>(this);
415 case PM_GET_SHELLBROWSER_PTR
:
416 return (LRESULT
)this;
418 case PM_DISPATCH_COMMAND
: {
419 switch(LOWORD(wparam
)) {
420 case ID_WINDOW_NEW
: {CONTEXT("ShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
421 ShellBrowserChild::create(_create_info
);
427 case ID_BROWSE_FORWARD
:
439 return super::WndProc(nmsg
, wparam
, lparam
);
445 int ShellBrowserChild::Notify(int id
, NMHDR
* pnmh
)
448 case TVN_GETDISPINFO
: OnTreeGetDispInfo(id
, pnmh
); break;
449 case TVN_ITEMEXPANDING
: OnTreeItemExpanding(id
, (LPNMTREEVIEW
)pnmh
); break;
450 case TVN_SELCHANGED
: OnTreeItemSelected(id
, (LPNMTREEVIEW
)pnmh
); break;
451 case NM_RCLICK
: OnTreeItemRClick(id
, pnmh
); break;
452 default: return super::Notify(id
, pnmh
);
459 HRESULT
ShellBrowserChild::OnDefaultCommand(LPIDA pida
)
461 CONTEXT("ShellBrowserChild::OnDefaultCommand()");
463 if (pida
->cidl
>= 1) {
464 if (_left_hwnd
) { // explorer mode
466 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
470 parent
->smart_scan();
471 } catch(COMException
& e
) {
475 UINT firstOffset
= pida
->aoffset
[1];
476 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
478 Entry
* entry
= parent
->find_entry(pidl
);
480 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
481 if (entry
->_etype
== ET_SHELL
)
482 if (expand_folder(static_cast<ShellDirectory
*>(entry
)))
486 } else { // no tree control
487 if (MainFrame::OpenShellFolders(pida
, _hWndFrame
))
490 /* create new Frame Window
491 if (MainFrame::OpenShellFolders(pida, 0))
501 bool ShellBrowserChild::expand_folder(ShellDirectory
* entry
)
503 CONTEXT("ShellBrowserChild::expand_folder()");
505 //HTREEITEM hitem_sel = TreeView_GetSelection(_left_hwnd);
509 if (!TreeView_Expand(_left_hwnd
, _last_sel
, TVE_EXPAND
))
512 for(HTREEITEM hitem
=TreeView_GetChild(_left_hwnd
,_last_sel
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
513 if ((ShellDirectory
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
514 if (TreeView_SelectItem(_left_hwnd
, hitem
) &&
515 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
526 void ShellBrowserChild::jump_to(LPCTSTR path
)
528 ///@todo implement "file://", ... parsing
529 jump_to(ShellPath(path
));
533 void ShellBrowserChild::jump_to(LPCITEMIDLIST pidl
)