2 * Copyright 2003, 2004, 2005, 2006 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 "../resource.h"
34 // work around GCC's wide string constant bug
36 const LPCTSTR C_DRIVE
= C_DRIVE_STR
;
40 ShellBrowser::ShellBrowser(HWND hwnd
, HWND hwndFrame
, HWND left_hwnd
, WindowHandle
& right_hwnd
, ShellPathInfo
& create_info
,
41 BrowserCallback
* cb
, CtxMenuInterfaces
& cm_ifs
)
42 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
43 : super(IID_IShellFolderViewCB
),
48 _hwndFrame(hwndFrame
),
49 _left_hwnd(left_hwnd
),
50 _right_hwnd(right_hwnd
),
51 _create_info(create_info
),
61 _himl
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_MASK
|ILC_COLOR24
, 2, 0);
62 ImageList_SetBkColor(_himl
, GetSysColor(COLOR_WINDOW
));
65 ShellBrowser::~ShellBrowser()
67 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
68 ImageList_Destroy(_himl
);
71 _pShellView
->Release();
74 _pDropTarget
->Release();
79 DestroyWindow(_right_hwnd
);
85 void ShellBrowser::Init()
87 CONTEXT("ShellBrowser::Init()");
89 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORADDRESSBAR
);
91 _root
._drive_type
= DRIVE_UNKNOWN
;
92 lstrcpy(_root
._volname
, root_name
);
94 lstrcpy(_root
._fs
, TEXT("Desktop"));
96 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
98 _root
._entry
->read_directory(SCAN_DONT_ACCESS
|SCAN_NO_FILESYSTEM
); // avoid to handle desktop root folder as file system directory
105 jump_to(_create_info
._shell_path
);
107 /* already filled by ShellDirectory constructor
108 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
111 void ShellBrowser::jump_to(LPCITEMIDLIST pidl
)
116 _cur_dir
= static_cast<ShellDirectory
*>(_root
._entry
);
118 //LOG(FmtString(TEXT("ShellBrowser::jump_to(): pidl=%s"), (LPCTSTR)FileSysShellPath(pidl)));
120 // We could call read_tree() here to iterate through the hierarchy and open all folders
121 // from _create_info._root_shell_path (_cur_dir) to _create_info._shell_path (pidl).
122 // To make it easier we just use ILFindChild() instead.
124 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
128 LPCITEMIDLIST child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
129 if (!child_pidl
|| !child_pidl
->mkid
.cb
)
132 _cur_dir
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
134 entry
= _cur_dir
->find_entry(child_pidl
);
138 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
139 _callback
->entry_selected(entry
);
142 _cur_dir
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
144 entry
= _cur_dir
->find_entry(pidl
); // This is not correct in the common case, but works on the desktop level.
147 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
148 _callback
->entry_selected(entry
);
153 // If not already called, now directly call UpdateFolderView() using pidl
155 UpdateFolderView(ShellFolder(pidl
));
159 void ShellBrowser::InitializeTree()
161 CONTEXT("ShellBrowserChild::InitializeTree()");
163 _himl_old
= TreeView_SetImageList(_left_hwnd
, _himl
, TVSIL_NORMAL
);
164 TreeView_SetScrollTime(_left_hwnd
, 100);
166 TV_INSERTSTRUCT tvInsert
;
167 TV_ITEM
& tvItem
= tvInsert
.item
;
169 tvInsert
.hParent
= 0;
170 tvInsert
.hInsertAfter
= TVI_LAST
;
172 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
173 tvItem
.lParam
= (LPARAM
)_root
._entry
;
174 tvItem
.pszText
= _root
._volname
; //LPSTR_TEXTCALLBACK;
175 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
176 tvItem
.cChildren
= 1;
178 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
179 TreeView_SelectItem(_left_hwnd
, hItem
);
180 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
183 bool ShellBrowser::InitDragDrop()
185 CONTEXT("ShellBrowser::InitDragDrop()");
187 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
192 _pDropTarget
->AddRef();
194 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
195 _pDropTarget
->Release(); // free TreeDropTarget
199 _pDropTarget
->Release();
203 ftetc
.dwAspect
= DVASPECT_CONTENT
;
205 ftetc
.tymed
= TYMED_HGLOBAL
;
206 ftetc
.cfFormat
= CF_HDROP
;
208 _pDropTarget
->AddSuportedFormat(ftetc
);
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_IMAGE
|TVIF_SELECTEDIMAGE
)) {
226 if (lpdi
->item
.mask
& TVIF_IMAGE
)
227 lpdi
->item
.iImage
= get_image_idx(
228 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
)));
230 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
231 lpdi
->item
.iSelectedImage
= get_image_idx(
232 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
|ICF_OPEN
)));
237 int ShellBrowser::get_image_idx(int icon_id
)
239 if (icon_id
!= ICID_NONE
) {
240 map
<int,int>::const_iterator found
= _image_map
.find(icon_id
);
242 if (found
!= _image_map
.end())
243 return found
->second
;
245 int idx
= ImageList_AddIcon(_himl
, g_Globals
._icon_cache
.get_icon(icon_id
).get_hicon());
247 _image_map
[icon_id
] = idx
;
254 void ShellBrowser::invalidate_cache()
256 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
257 ImageList_Destroy(_himl
);
259 _himl
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_MASK
|ILC_COLOR24
, 2, 0);
260 ImageList_SetBkColor(_himl
, GetSysColor(COLOR_WINDOW
));
262 _himl_old
= TreeView_SetImageList(_left_hwnd
, _himl
, TVSIL_NORMAL
);
264 for(map
<int,int>::const_iterator it
=_image_map
.begin(); it
!=_image_map
.end(); ++it
)
265 g_Globals
._icon_cache
.free_icon(it
->first
);
271 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
273 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
275 if (pnmtv
->action
== TVE_COLLAPSE
)
276 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
277 else if (pnmtv
->action
== TVE_EXPAND
) {
278 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
281 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
282 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
284 // remove subitem "+"
287 tvItem
.mask
= TVIF_CHILDREN
;
288 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
289 tvItem
.cChildren
= 0;
291 TreeView_SetItem(_left_hwnd
, &tvItem
);
296 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
298 CONTEXT("ShellBrowser::InsertSubitems()");
304 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
307 entry
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
308 } catch(COMException
& e
) {
309 HandleException(e
, g_Globals
._hMainWnd
);
312 // remove old children items
313 HTREEITEM hchild
, hnext
;
315 hnext
= TreeView_GetChild(_left_hwnd
, hParentItem
);
317 while((hchild
=hnext
) != 0) {
318 hnext
= TreeView_GetNextSibling(_left_hwnd
, hchild
);
319 TreeView_DeleteItem(_left_hwnd
, hchild
);
323 TV_INSERTSTRUCT tvInsert
;
325 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
327 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
330 // ignore hidden directories
331 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
)
334 // ignore directory entries "." and ".."
335 if (entry
->_data
.cFileName
[0]==TEXT('.') &&
336 (entry
->_data
.cFileName
[1]==TEXT('\0') ||
337 (entry
->_data
.cFileName
[1]==TEXT('.') && entry
->_data
.cFileName
[2]==TEXT('\0'))))
340 ZeroMemory(&tvItem
, sizeof(tvItem
));
342 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
343 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
344 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
345 tvItem
.lParam
= (LPARAM
)entry
;
346 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
348 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
349 tvItem
.mask
|= TVIF_STATE
;
350 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
351 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
354 tvInsert
.item
= tvItem
;
355 tvInsert
.hInsertAfter
= TVI_LAST
;
356 tvInsert
.hParent
= hParentItem
;
358 (void)TreeView_InsertItem(_left_hwnd
, &tvInsert
);
364 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
370 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
372 CONTEXT("ShellBrowser::OnTreeItemSelected()");
374 Entry
* entry
= (Entry
*)pnmtv
->itemNew
.lParam
;
376 _last_sel
= pnmtv
->itemNew
.hItem
;
379 _callback
->entry_selected(entry
);
383 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
385 CONTEXT("ShellBrowser::OnTreeItemRClick()");
389 GetCursorPos(&tvhti
.pt
);
390 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
392 tvhti
.flags
= LVHT_NOWHERE
;
393 (void)TreeView_HitTest(_left_hwnd
, &tvhti
);
395 if (TVHT_ONITEM
& tvhti
.flags
) {
396 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
399 Entry
* entry
= (Entry
*)itemData
;
400 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
402 HRESULT hr
= entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
);
413 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
415 CONTEXT("ShellBrowser::UpdateFolderView()");
418 IShellView
* pLastShellView
= _pShellView
;
423 pLastShellView
->GetCurrentInfo(&fs
);
425 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
426 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
429 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
430 SFV_CREATE sfv_create
;
432 sfv_create
.cbSize
= sizeof(SFV_CREATE
);
433 sfv_create
.pshf
= folder
;
434 sfv_create
.psvOuter
= NULL
;
435 sfv_create
.psfvcb
= this;
437 HRESULT hr
= SHCreateShellFolderView(&sfv_create
, &_pShellView
);
439 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
447 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
448 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
450 if (pLastShellView
) {
451 pLastShellView
->GetCurrentInfo(&fs
);
452 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
453 pLastShellView
->DestroyViewWindow();
454 pLastShellView
->Release();
457 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
461 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
463 /// shell view callback
464 HRESULT STDMETHODCALLTYPE
ShellBrowser::MessageSFVCB(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
466 if (uMsg
== SFVM_INITMENUPOPUP
) {
467 ///@todo never reached
468 InsertMenu((HMENU
)lParam
, 0, MF_BYPOSITION
, 12345, TEXT("TEST ENTRY"));
478 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
480 CONTEXT("ShellBrowser::OnDefaultCommand()");
482 if (pida
->cidl
>= 1) {
483 if (_left_hwnd
) { // explorer mode
485 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
489 parent
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
490 } catch(COMException
& e
) {
494 UINT firstOffset
= pida
->aoffset
[1];
495 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
497 Entry
* entry
= parent
->find_entry(pidl
);
499 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
500 if (entry
->_etype
== ET_SHELL
)
501 if (_last_sel
&& select_entry(_last_sel
, entry
))
504 ///@todo look for hidden or new subfolders and refresh/add new entry instead of opening a new window
508 } else { // no tree control
509 if (MainFrameBase::OpenShellFolders(pida
, _hwndFrame
))
512 /* create new Frame Window
513 if (MainFrame::OpenShellFolders(pida, 0))
523 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
525 CONTEXT("ShellBrowser::select_entry()");
527 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
530 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
531 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
532 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
534 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
547 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
552 // iterate through the hierarchy and open all folders to reach pidl
555 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
556 Entry
* entry
= _root
._entry
;
558 for(const void*p
=pidl
;;) {
562 if (!entry
|| !hitem
)
565 entry
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
567 Entry
* found
= entry
->find_entry(p
);
568 p
= entry
->get_next_path_component(p
);
571 hitem
= select_entry(hitem
, found
);
580 bool ShellBrowser::select_folder(Entry
* entry
, bool expand
)
582 CONTEXT("ShellBrowser::expand_folder()");
587 if (!TreeView_Expand(_left_hwnd
, _last_sel
, TVE_EXPAND
))
590 for(HTREEITEM hitem
=TreeView_GetChild(_left_hwnd
,_last_sel
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
591 if ((ShellDirectory
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
592 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
594 if (!TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
608 void ShellBrowser::refresh()
616 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
619 _shellpath_info(info
) //@@ copies info -> no reference to _create_info !
621 /**todo Conversion of shell path into path string -> store into URL history
622 const String& path = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORADDRESSBAR);
623 const String& parsingpath = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORPARSING);
625 // store path into history
626 if (info._path && *info._path)
627 _url_history.push(info._path);
632 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
634 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
635 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
637 return static_cast<MDIShellBrowserChild
*>(child
);
641 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
643 CONTEXT("MDIShellBrowserChild::Init()");
645 if (super::Init(pcs
))
648 update_shell_browser();
654 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
657 case PM_DISPATCH_COMMAND
: {
658 switch(LOWORD(wparam
)) {
659 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
660 MDIShellBrowserChild::create(_create_info
);
664 ///@todo refresh shell child
665 _shellBrowser
->invalidate_cache();
669 MainFrameBase::Create(ExplorerCmd(_url
, false));
673 return super::WndProc(nmsg
, wparam
, lparam
);
678 return super::WndProc(nmsg
, wparam
, lparam
);
684 void MDIShellBrowserChild::update_shell_browser()
686 int split_pos
= DEFAULT_SPLIT_POS
;
688 if (_shellBrowser
.get()) {
689 split_pos
= _split_pos
;
690 delete _shellBrowser
.release();
693 // create explorer treeview
694 if (_create_info
._open_mode
& OWM_EXPLORE
) {
696 ClientRect
rect(_hwnd
);
698 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
699 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
700 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
701 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
705 DestroyWindow(_left_hwnd
);
710 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _hwndFrame
, _left_hwnd
, _right_hwnd
,
711 _shellpath_info
, this, _cm_ifs
));
713 _shellBrowser
->Init();
717 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
721 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
722 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
726 if (SplitFileSysURL(url
, dir
, fname
)) {
730 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
731 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
738 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
741 _shellBrowser
->select_folder(entry
, false);
743 _shellBrowser
->UpdateFolderView(entry
->get_shell_folder());
745 // set size of new created shell view windows
746 ClientRect
rt(_hwnd
);
747 resize_children(rt
.right
, rt
.bottom
);
750 TCHAR path
[MAX_PATH
];
752 if (entry
->get_path(path
, COUNTOF(path
))) {
756 url
.printf(TEXT("shell://%s"), path
);
758 url
.printf(TEXT("file://%s"), path
);