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
31 #include "../explorer_intres.h"
34 ShellBrowser::ShellBrowser(HWND hwnd
, HWND left_hwnd
, WindowHandle
& right_hwnd
, ShellPathInfo
& create_info
,
35 HIMAGELIST himl
, BrowserCallback
* cb
, CtxMenuInterfaces
& cm_ifs
)
37 _left_hwnd(left_hwnd
),
38 _right_hwnd(right_hwnd
),
39 _create_info(create_info
),
51 ShellBrowser::~ShellBrowser()
54 _pShellView
->Release();
57 _pDropTarget
->Release();
62 DestroyWindow(_right_hwnd
);
68 LRESULT
ShellBrowser::Init(HWND hWndFrame
)
70 CONTEXT("ShellBrowser::Init()");
72 _hWndFrame
= hWndFrame
;
74 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORPARSING
);
76 _root
._drive_type
= DRIVE_UNKNOWN
;
77 lstrcpy(_root
._volname
, root_name
);
79 lstrcpy(_root
._fs
, TEXT("Desktop"));
81 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
83 jump_to(_create_info
._shell_path
);
86 _root
._entry
->read_directory();
88 /* already filled by ShellDirectory constructor
89 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
94 void ShellBrowser::jump_to(LPCITEMIDLIST pidl
)
100 _cur_dir
= static_cast<ShellDirectory
*>(_root
._entry
);
103 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
104 _root._entry->read_tree(shell_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME);
105 -> see FileChildWindow::FileChildWindow()
109 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
111 LPCITEMIDLIST child_pidl
;
114 child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
116 child_pidl
= pidl
; // This is not correct in the common case, but works on the desktop level.
119 _cur_dir
->smart_scan();
121 entry
= _cur_dir
->find_entry(child_pidl
);
124 _callback
->entry_selected(entry
);
128 //@@ work around as long as we don't iterate correctly through the ShellEntry tree
130 UpdateFolderView(ShellFolder(pidl
));
134 void ShellBrowser::InitializeTree(HIMAGELIST himl
)
136 CONTEXT("ShellBrowserChild::InitializeTree()");
138 TreeView_SetImageList(_left_hwnd
, himl
, TVSIL_NORMAL
);
139 TreeView_SetScrollTime(_left_hwnd
, 100);
141 TV_INSERTSTRUCT tvInsert
;
143 tvInsert
.hParent
= 0;
144 tvInsert
.hInsertAfter
= TVI_LAST
;
146 TV_ITEM
& tvItem
= tvInsert
.item
;
147 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
148 tvItem
.lParam
= (LPARAM
)_root
._entry
;
149 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
150 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
151 tvItem
.cChildren
= 1;
153 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
154 TreeView_SelectItem(_left_hwnd
, hItem
);
155 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
158 bool ShellBrowser::InitDragDrop()
160 CONTEXT("ShellBrowser::InitDragDrop()");
162 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
167 _pDropTarget
->AddRef();
169 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
170 _pDropTarget
->Release(); // free TreeDropTarget
175 _pDropTarget
->Release();
179 ftetc
.dwAspect
= DVASPECT_CONTENT
;
181 ftetc
.tymed
= TYMED_HGLOBAL
;
182 ftetc
.cfFormat
= CF_HDROP
;
184 _pDropTarget
->AddSuportedFormat(ftetc
);
190 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
192 CONTEXT("ShellBrowser::OnTreeItemRClick()");
196 GetCursorPos(&tvhti
.pt
);
197 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
199 tvhti
.flags
= LVHT_NOWHERE
;
200 TreeView_HitTest(_left_hwnd
, &tvhti
);
202 if (TVHT_ONITEM
& tvhti
.flags
) {
203 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
206 Entry
* entry
= (Entry
*)itemData
;
207 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
209 CHECKERROR(entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
));
214 void ShellBrowser::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
216 CONTEXT("ShellBrowser::OnTreeGetDispInfo()");
218 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
219 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
222 if (lpdi
->item
.mask
& TVIF_TEXT
)
223 lpdi
->item
.pszText
= entry
->_display_name
;
225 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
226 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
227 LPCITEMIDLIST pidl
= pidl_abs
;
231 if (lpdi->item.mask & TVIF_TEXT)
232 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
233 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); ///@todo look at cchTextMax if there is enough space available
235 lpdi->item.pszText = entry->_data.cFileName;
237 if (lpdi
->item
.mask
& TVIF_IMAGE
)
238 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_LINKOVERLAY
) == _himl
)
239 lpdi
->item
.iImage
= sfi
.iIcon
;
241 lpdi
->item
.iImage
= -1;
243 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
244 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_OPENICON
) == _himl
)
245 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
247 lpdi
->item
.iSelectedImage
= -1;
252 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
254 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
256 if (pnmtv
->action
== TVE_COLLAPSE
)
257 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
258 else if (pnmtv
->action
== TVE_EXPAND
) {
259 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
262 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
263 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
265 // remove subitem "+"
268 tvItem
.mask
= TVIF_CHILDREN
;
269 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
270 tvItem
.cChildren
= 0;
272 TreeView_SetItem(_left_hwnd
, &tvItem
);
277 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
279 CONTEXT("ShellBrowser::InsertSubitems()");
285 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
289 } catch(COMException
& e
) {
290 HandleException(e
, g_Globals
._hMainWnd
);
294 TV_INSERTSTRUCT tvInsert
;
296 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
298 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
301 ZeroMemory(&tvItem
, sizeof(tvItem
));
303 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
304 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
305 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
306 tvItem
.lParam
= (LPARAM
)entry
;
307 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
309 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
310 tvItem
.mask
|= TVIF_STATE
;
311 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
312 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
315 tvInsert
.item
= tvItem
;
316 tvInsert
.hInsertAfter
= TVI_LAST
;
317 tvInsert
.hParent
= hParentItem
;
319 TreeView_InsertItem(_left_hwnd
, &tvInsert
);
325 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
330 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
332 CONTEXT("ShellBrowser::OnTreeItemSelected()");
334 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
336 _last_sel
= pnmtv
->itemNew
.hItem
;
339 _callback
->entry_selected(entry
);
342 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
344 CONTEXT("ShellBrowser::UpdateFolderView()");
347 IShellView
* pLastShellView
= _pShellView
;
352 pLastShellView
->GetCurrentInfo(&fs
);
354 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
355 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
358 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
365 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
366 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
368 if (pLastShellView
) {
369 pLastShellView
->GetCurrentInfo(&fs
);
370 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
371 pLastShellView
->DestroyViewWindow();
372 pLastShellView
->Release();
375 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
379 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
381 CONTEXT("ShellBrowser::OnDefaultCommand()");
383 if (pida
->cidl
>= 1) {
384 if (_left_hwnd
) { // explorer mode
386 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
390 parent
->smart_scan();
391 } catch(COMException
& e
) {
395 UINT firstOffset
= pida
->aoffset
[1];
396 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
398 Entry
* entry
= parent
->find_entry(pidl
);
400 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
401 if (entry
->_etype
== ET_SHELL
)
402 if (_last_sel
&& select_entry(_last_sel
, entry
))
406 } else { // no tree control
407 if (MainFrameBase::OpenShellFolders(pida
, _hWndFrame
))
410 /* create new Frame Window
411 if (MainFrame::OpenShellFolders(pida, 0))
421 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
423 CONTEXT("ShellBrowser::select_entry()");
425 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
428 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
429 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
430 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
432 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
445 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
450 // iterate through the hierarchy and open all folders to reach pidl
453 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
454 Entry
* entry
= _root
._entry
;
456 for(const void*p
=pidl
;;) {
460 if (!entry
|| !hitem
)
463 entry
->smart_scan(SORT_NAME
);
465 Entry
* found
= entry
->find_entry(p
);
466 p
= entry
->get_next_path_component(p
);
469 hitem
= select_entry(hitem
, found
);
480 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
483 _shellpath_info(info
) //@@ copies info -> no referenz to _create_info !
485 /**todo Conversion of shell path into path string -> store into URL history
486 // store path into history
487 if (info._path && *info._path)
488 _url_history.push(info._path);
493 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
495 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
496 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
498 return static_cast<MDIShellBrowserChild
*>(child
);
502 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
504 CONTEXT("MDIShellBrowserChild::Init()");
506 if (super::Init(pcs
))
511 update_shell_browser();
515 _shellBrowser
->Init(_himlSmall
);
517 _shellBrowser
->UpdateFolderView(_create_info
._shell_path
.get_folder());
523 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
526 case PM_DISPATCH_COMMAND
: {
527 switch(LOWORD(wparam
)) {
528 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
529 MDIShellBrowserChild::create(_create_info
);
533 //@todo refresh shell child
537 MainFrameBase::Create(_url
, false);
541 return super::WndProc(nmsg
, wparam
, lparam
);
546 return super::WndProc(nmsg
, wparam
, lparam
);
552 void MDIShellBrowserChild::update_shell_browser()
554 int split_pos
= DEFAULT_SPLIT_POS
;
556 if (_shellBrowser
.get()) {
557 split_pos
= _split_pos
;
558 delete _shellBrowser
.release();
561 // create explorer treeview
562 if (_create_info
._open_mode
& OWM_EXPLORE
) {
564 ClientRect
rect(_hwnd
);
566 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
567 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
568 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
569 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
573 DestroyWindow(_left_hwnd
);
578 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _left_hwnd
, _right_hwnd
,
579 _shellpath_info
, _himlSmall
, this, _cm_ifs
));
581 _shellBrowser
->Init(_hwndFrame
);
585 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
589 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
590 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
594 if (SplitFileSysURL(url
, dir
, fname
)) {
598 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
599 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
606 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
608 if (entry
->_etype
== ET_SHELL
) {
609 ShellEntry
* shell_entry
= static_cast<ShellEntry
*>(entry
);
610 IShellFolder
* folder
;
612 if (shell_entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
613 folder
= static_cast<ShellDirectory
*>(shell_entry
)->_folder
;
615 folder
= shell_entry
->get_parent_folder();
622 TCHAR path
[MAX_PATH
];
624 if (shell_entry
->get_path(path
)) {
628 url
.printf(TEXT("shell://%s"), path
);
630 url
.printf(TEXT("file://%s"), path
);
635 _shellBrowser
->UpdateFolderView(folder
);
637 // set size of new created shell view windows
638 ClientRect
rt(_hwnd
);
639 resize_children(rt
.right
, rt
.bottom
);