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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 HDC hDC
= GetDC(NULL
);
64 INT bpp
= GetDeviceCaps(hDC
, BITSPIXEL
);
83 _himl
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ilMask
, 2, 0);
84 ImageList_SetBkColor(_himl
, GetSysColor(COLOR_WINDOW
));
88 ShellBrowser::~ShellBrowser()
90 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
91 ImageList_Destroy(_himl
);
94 _pShellView
->Release();
97 _pDropTarget
->Release();
102 DestroyWindow(_right_hwnd
);
108 void ShellBrowser::Init()
110 CONTEXT("ShellBrowser::Init()");
112 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORADDRESSBAR
);
114 _root
._drive_type
= DRIVE_UNKNOWN
;
115 lstrcpy(_root
._volname
, root_name
);
117 lstrcpy(_root
._fs
, TEXT("Desktop"));
119 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
121 _root
._entry
->read_directory(SCAN_DONT_ACCESS
|SCAN_NO_FILESYSTEM
); // avoid to handle desktop root folder as file system directory
128 jump_to(_create_info
._shell_path
);
130 /* already filled by ShellDirectory constructor
131 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
134 void ShellBrowser::jump_to(LPCITEMIDLIST pidl
)
139 _cur_dir
= static_cast<ShellDirectory
*>(_root
._entry
);
141 //LOG(FmtString(TEXT("ShellBrowser::jump_to(): pidl=%s"), (LPCTSTR)FileSysShellPath(pidl)));
143 // We could call read_tree() here to iterate through the hierarchy and open all folders
144 // from _create_info._root_shell_path (_cur_dir) to _create_info._shell_path (pidl).
145 // To make it easier we just use ILFindChild() instead.
147 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
151 LPCITEMIDLIST child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
152 if (!child_pidl
|| !child_pidl
->mkid
.cb
)
155 _cur_dir
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
157 entry
= _cur_dir
->find_entry(child_pidl
);
161 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
162 _callback
->entry_selected(entry
);
165 _cur_dir
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
167 entry
= _cur_dir
->find_entry(pidl
); // This is not correct in the common case, but works on the desktop level.
170 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
171 _callback
->entry_selected(entry
);
176 // If not already called, now directly call UpdateFolderView() using pidl
178 UpdateFolderView(ShellFolder(pidl
));
182 void ShellBrowser::InitializeTree()
184 CONTEXT("ShellBrowserChild::InitializeTree()");
186 _himl_old
= TreeView_SetImageList(_left_hwnd
, _himl
, TVSIL_NORMAL
);
187 TreeView_SetScrollTime(_left_hwnd
, 100);
189 TV_INSERTSTRUCT tvInsert
;
190 TV_ITEM
& tvItem
= tvInsert
.item
;
192 tvInsert
.hParent
= 0;
193 tvInsert
.hInsertAfter
= TVI_LAST
;
195 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
196 tvItem
.lParam
= (LPARAM
)_root
._entry
;
197 tvItem
.pszText
= _root
._volname
; //LPSTR_TEXTCALLBACK;
198 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
199 tvItem
.cChildren
= 1;
201 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
202 TreeView_SelectItem(_left_hwnd
, hItem
);
203 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
206 bool ShellBrowser::InitDragDrop()
208 CONTEXT("ShellBrowser::InitDragDrop()");
210 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
215 _pDropTarget
->AddRef();
217 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
218 _pDropTarget
->Release(); // free TreeDropTarget
222 _pDropTarget
->Release();
226 ftetc
.dwAspect
= DVASPECT_CONTENT
;
228 ftetc
.tymed
= TYMED_HGLOBAL
;
229 ftetc
.cfFormat
= CF_HDROP
;
231 _pDropTarget
->AddSuportedFormat(ftetc
);
237 void ShellBrowser::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
239 CONTEXT("ShellBrowser::OnTreeGetDispInfo()");
241 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
242 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
245 if (lpdi
->item
.mask
& TVIF_TEXT
)
246 lpdi
->item
.pszText
= entry
->_display_name
;
248 if (lpdi
->item
.mask
& (TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
249 if (lpdi
->item
.mask
& TVIF_IMAGE
)
250 lpdi
->item
.iImage
= get_image_idx(
251 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
)));
253 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
254 lpdi
->item
.iSelectedImage
= get_image_idx(
255 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
|ICF_OPEN
)));
260 int ShellBrowser::get_image_idx(int icon_id
)
262 if (icon_id
!= ICID_NONE
) {
263 map
<int,int>::const_iterator found
= _image_map
.find(icon_id
);
265 if (found
!= _image_map
.end())
266 return found
->second
;
268 int idx
= ImageList_AddIcon(_himl
, g_Globals
._icon_cache
.get_icon(icon_id
).get_hicon());
270 _image_map
[icon_id
] = idx
;
277 void ShellBrowser::invalidate_cache()
279 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
280 ImageList_Destroy(_himl
);
282 _himl
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_MASK
|ILC_COLOR24
, 2, 0);
283 ImageList_SetBkColor(_himl
, GetSysColor(COLOR_WINDOW
));
285 _himl_old
= TreeView_SetImageList(_left_hwnd
, _himl
, TVSIL_NORMAL
);
287 for(map
<int,int>::const_iterator it
=_image_map
.begin(); it
!=_image_map
.end(); ++it
)
288 g_Globals
._icon_cache
.free_icon(it
->first
);
294 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
296 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
298 if (pnmtv
->action
== TVE_COLLAPSE
)
299 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
300 else if (pnmtv
->action
== TVE_EXPAND
) {
301 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
304 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
305 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
307 // remove subitem "+"
310 tvItem
.mask
= TVIF_CHILDREN
;
311 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
312 tvItem
.cChildren
= 0;
314 TreeView_SetItem(_left_hwnd
, &tvItem
);
319 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
321 CONTEXT("ShellBrowser::InsertSubitems()");
327 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
330 entry
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
331 } catch(COMException
& e
) {
332 HandleException(e
, g_Globals
._hMainWnd
);
335 // remove old children items
336 HTREEITEM hchild
, hnext
;
338 hnext
= TreeView_GetChild(_left_hwnd
, hParentItem
);
340 while((hchild
=hnext
) != 0) {
341 hnext
= TreeView_GetNextSibling(_left_hwnd
, hchild
);
342 TreeView_DeleteItem(_left_hwnd
, hchild
);
346 TV_INSERTSTRUCT tvInsert
;
348 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
350 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
353 // ignore hidden directories
354 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
)
357 // ignore directory entries "." and ".."
358 if (entry
->_data
.cFileName
[0]==TEXT('.') &&
359 (entry
->_data
.cFileName
[1]==TEXT('\0') ||
360 (entry
->_data
.cFileName
[1]==TEXT('.') && entry
->_data
.cFileName
[2]==TEXT('\0'))))
363 ZeroMemory(&tvItem
, sizeof(tvItem
));
365 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
366 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
367 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
368 tvItem
.lParam
= (LPARAM
)entry
;
369 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
371 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
372 tvItem
.mask
|= TVIF_STATE
;
373 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
374 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
377 tvInsert
.item
= tvItem
;
378 tvInsert
.hInsertAfter
= TVI_LAST
;
379 tvInsert
.hParent
= hParentItem
;
381 (void)TreeView_InsertItem(_left_hwnd
, &tvInsert
);
387 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
393 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
395 CONTEXT("ShellBrowser::OnTreeItemSelected()");
397 Entry
* entry
= (Entry
*)pnmtv
->itemNew
.lParam
;
399 _last_sel
= pnmtv
->itemNew
.hItem
;
402 _callback
->entry_selected(entry
);
406 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
408 CONTEXT("ShellBrowser::OnTreeItemRClick()");
412 GetCursorPos(&tvhti
.pt
);
413 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
415 tvhti
.flags
= LVHT_NOWHERE
;
416 (void)TreeView_HitTest(_left_hwnd
, &tvhti
);
418 if (TVHT_ONITEM
& tvhti
.flags
) {
419 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
422 Entry
* entry
= (Entry
*)itemData
;
423 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
425 HRESULT hr
= entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
);
436 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
438 CONTEXT("ShellBrowser::UpdateFolderView()");
441 IShellView
* pLastShellView
= _pShellView
;
446 pLastShellView
->GetCurrentInfo(&fs
);
448 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
449 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
452 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
453 SFV_CREATE sfv_create
;
455 sfv_create
.cbSize
= sizeof(SFV_CREATE
);
456 sfv_create
.pshf
= folder
;
457 sfv_create
.psvOuter
= NULL
;
458 sfv_create
.psfvcb
= this;
460 HRESULT hr
= SHCreateShellFolderView(&sfv_create
, &_pShellView
);
462 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
470 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
471 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
473 if (pLastShellView
) {
474 pLastShellView
->GetCurrentInfo(&fs
);
475 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
476 pLastShellView
->DestroyViewWindow();
477 pLastShellView
->Release();
480 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
484 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
486 /// shell view callback
487 HRESULT STDMETHODCALLTYPE
ShellBrowser::MessageSFVCB(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
489 if (uMsg
== SFVM_INITMENUPOPUP
) {
490 ///@todo never reached
491 InsertMenu((HMENU
)lParam
, 0, MF_BYPOSITION
, 12345, TEXT("TEST ENTRY"));
501 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
503 CONTEXT("ShellBrowser::OnDefaultCommand()");
505 if (pida
->cidl
>= 1) {
506 if (_left_hwnd
) { // explorer mode
508 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
512 parent
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
513 } catch(COMException
& e
) {
517 UINT firstOffset
= pida
->aoffset
[1];
518 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
520 Entry
* entry
= parent
->find_entry(pidl
);
522 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
523 if (entry
->_etype
== ET_SHELL
)
524 if (_last_sel
&& select_entry(_last_sel
, entry
))
527 ///@todo look for hidden or new subfolders and refresh/add new entry instead of opening a new window
531 } else { // no tree control
532 if (MainFrameBase::OpenShellFolders(pida
, _hwndFrame
))
535 /* create new Frame Window
536 if (MainFrame::OpenShellFolders(pida, 0))
546 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
548 CONTEXT("ShellBrowser::select_entry()");
550 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
553 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
554 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
555 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
557 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
570 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
575 // iterate through the hierarchy and open all folders to reach pidl
578 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
579 Entry
* entry
= _root
._entry
;
581 for(const void*p
=pidl
;;) {
585 if (!entry
|| !hitem
)
588 entry
->smart_scan(SORT_NAME
, SCAN_DONT_ACCESS
);
590 Entry
* found
= entry
->find_entry(p
);
591 p
= entry
->get_next_path_component(p
);
594 hitem
= select_entry(hitem
, found
);
603 bool ShellBrowser::select_folder(Entry
* entry
, bool expand
)
605 CONTEXT("ShellBrowser::expand_folder()");
610 if (!TreeView_Expand(_left_hwnd
, _last_sel
, TVE_EXPAND
))
613 for(HTREEITEM hitem
=TreeView_GetChild(_left_hwnd
,_last_sel
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
614 if ((ShellDirectory
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
615 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
617 if (!TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
631 void ShellBrowser::refresh()
639 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
642 _shellpath_info(info
) //@@ copies info -> no reference to _create_info !
644 /**todo Conversion of shell path into path string -> store into URL history
645 const String& path = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORADDRESSBAR);
646 const String& parsingpath = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORPARSING);
648 // store path into history
649 if (info._path && *info._path)
650 _url_history.push(info._path);
655 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
657 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
658 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
660 return static_cast<MDIShellBrowserChild
*>(child
);
664 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
666 CONTEXT("MDIShellBrowserChild::Init()");
668 if (super::Init(pcs
))
671 update_shell_browser();
677 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
680 case PM_DISPATCH_COMMAND
: {
681 switch(LOWORD(wparam
)) {
682 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
683 MDIShellBrowserChild::create(_create_info
);
687 ///@todo refresh shell child
688 _shellBrowser
->invalidate_cache();
692 MainFrameBase::Create(ExplorerCmd(_url
, false));
696 return super::WndProc(nmsg
, wparam
, lparam
);
701 return super::WndProc(nmsg
, wparam
, lparam
);
707 void MDIShellBrowserChild::update_shell_browser()
709 int split_pos
= DEFAULT_SPLIT_POS
;
711 if (_shellBrowser
.get()) {
712 split_pos
= _split_pos
;
713 delete _shellBrowser
.release();
716 // create explorer treeview
717 if (_create_info
._open_mode
& OWM_EXPLORE
) {
719 ClientRect
rect(_hwnd
);
721 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
722 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
723 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
724 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
728 DestroyWindow(_left_hwnd
);
733 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _hwndFrame
, _left_hwnd
, _right_hwnd
,
734 _shellpath_info
, this, _cm_ifs
));
736 _shellBrowser
->Init();
740 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
744 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
745 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
749 if (SplitFileSysURL(url
, dir
, fname
)) {
753 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
754 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
761 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
764 _shellBrowser
->select_folder(entry
, false);
766 _shellBrowser
->UpdateFolderView(entry
->get_shell_folder());
768 // set size of new created shell view windows
769 ClientRect
rt(_hwnd
);
770 resize_children(rt
.right
, rt
.bottom
);
773 TCHAR path
[MAX_PATH
];
775 if (entry
->get_path(path
, COUNTOF(path
))) {
779 url
.printf(TEXT("shell://%s"), path
);
781 url
.printf(TEXT("file://%s"), path
);