2 * Copyright 2003, 2004, 2005 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 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 _left_hwnd(left_hwnd
),
49 _right_hwnd(right_hwnd
),
50 _create_info(create_info
),
60 _himl
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_MASK
|ILC_COLOR24
, 2, 0);
61 ImageList_SetBkColor(_himl
, GetSysColor(COLOR_WINDOW
));
64 ShellBrowser::~ShellBrowser()
66 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
67 ImageList_Destroy(_himl
);
70 _pShellView
->Release();
73 _pDropTarget
->Release();
78 DestroyWindow(_right_hwnd
);
84 LRESULT
ShellBrowser::Init(HWND hWndFrame
)
86 CONTEXT("ShellBrowser::Init()");
88 _hWndFrame
= hWndFrame
;
90 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORADDRESSBAR
);
92 _root
._drive_type
= DRIVE_UNKNOWN
;
93 lstrcpy(_root
._volname
, root_name
);
95 lstrcpy(_root
._fs
, TEXT("Desktop"));
97 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
99 jump_to(_create_info
._shell_path
);
102 _root
._entry
->read_directory();
104 /* already filled by ShellDirectory constructor
105 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
110 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)));
121 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
124 we should call read_tree() here to iterate through the hierarchy and open all folders from _create_info._root_shell_path (_cur_dir) to _create_info._shell_path (pidl)
125 _root._entry->read_tree(_create_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME);
126 -> see FileChildWindow::FileChildWindow()_create_info._shell_path
129 LPCITEMIDLIST child_pidl
;
132 child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
134 child_pidl
= pidl
; // This is not correct in the common case, but works on the desktop level.
137 _cur_dir
->smart_scan();
139 entry
= _cur_dir
->find_entry(child_pidl
);
142 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
143 _callback
->entry_selected(entry
);
148 //@@ work around as long as we don't iterate correctly through the ShellEntry tree
150 UpdateFolderView(ShellFolder(pidl
));
154 void ShellBrowser::InitializeTree()
156 CONTEXT("ShellBrowserChild::InitializeTree()");
158 _himl_old
= TreeView_SetImageList(_left_hwnd
, _himl
, TVSIL_NORMAL
);
159 TreeView_SetScrollTime(_left_hwnd
, 100);
161 TV_INSERTSTRUCT tvInsert
;
162 TV_ITEM
& tvItem
= tvInsert
.item
;
164 tvInsert
.hParent
= 0;
165 tvInsert
.hInsertAfter
= TVI_LAST
;
167 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
168 tvItem
.lParam
= (LPARAM
)_root
._entry
;
169 tvItem
.pszText
= _root
._volname
; //LPSTR_TEXTCALLBACK;
170 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
171 tvItem
.cChildren
= 1;
173 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
174 TreeView_SelectItem(_left_hwnd
, hItem
);
175 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
178 bool ShellBrowser::InitDragDrop()
180 CONTEXT("ShellBrowser::InitDragDrop()");
182 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
187 _pDropTarget
->AddRef();
189 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
190 _pDropTarget
->Release(); // free TreeDropTarget
195 _pDropTarget
->Release();
199 ftetc
.dwAspect
= DVASPECT_CONTENT
;
201 ftetc
.tymed
= TYMED_HGLOBAL
;
202 ftetc
.cfFormat
= CF_HDROP
;
204 _pDropTarget
->AddSuportedFormat(ftetc
);
210 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
212 CONTEXT("ShellBrowser::OnTreeItemRClick()");
216 GetCursorPos(&tvhti
.pt
);
217 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
219 tvhti
.flags
= LVHT_NOWHERE
;
220 (void)TreeView_HitTest(_left_hwnd
, &tvhti
);
222 if (TVHT_ONITEM
& tvhti
.flags
) {
223 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
226 Entry
* entry
= (Entry
*)itemData
;
227 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
229 CHECKERROR(entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
));
234 void ShellBrowser::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
236 CONTEXT("ShellBrowser::OnTreeGetDispInfo()");
238 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
239 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
242 if (lpdi
->item
.mask
& TVIF_TEXT
)
243 lpdi
->item
.pszText
= entry
->_display_name
;
245 if (lpdi
->item
.mask
& (TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
246 if (lpdi
->item
.mask
& TVIF_IMAGE
)
247 lpdi
->item
.iImage
= get_image_idx(
248 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
)));
250 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
251 lpdi
->item
.iSelectedImage
= get_image_idx(
252 entry
->safe_extract_icon((ICONCACHE_FLAGS
)(ICF_HICON
|ICF_OVERLAYS
|ICF_OPEN
)));
257 int ShellBrowser::get_image_idx(int icon_id
)
259 if (icon_id
!= ICID_NONE
) {
260 map
<int,int>::const_iterator found
= _image_map
.find(icon_id
);
262 if (found
!= _image_map
.end())
263 return found
->second
;
265 int idx
= ImageList_AddIcon(_himl
, g_Globals
._icon_cache
.get_icon(icon_id
).get_hicon());
267 _image_map
[icon_id
] = idx
;
274 void ShellBrowser::invalidate_cache()
276 for(map
<int,int>::const_iterator it
=_image_map
.begin(); it
!=_image_map
.end(); ++it
)
277 g_Globals
._icon_cache
.free_icon(it
->first
);
282 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
284 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
286 if (pnmtv
->action
== TVE_COLLAPSE
)
287 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
288 else if (pnmtv
->action
== TVE_EXPAND
) {
289 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
292 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
293 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
295 // remove subitem "+"
298 tvItem
.mask
= TVIF_CHILDREN
;
299 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
300 tvItem
.cChildren
= 0;
302 TreeView_SetItem(_left_hwnd
, &tvItem
);
307 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
309 CONTEXT("ShellBrowser::InsertSubitems()");
315 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
319 } catch(COMException
& e
) {
320 HandleException(e
, g_Globals
._hMainWnd
);
324 TV_INSERTSTRUCT tvInsert
;
326 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
328 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
331 ZeroMemory(&tvItem
, sizeof(tvItem
));
333 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
334 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
335 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
336 tvItem
.lParam
= (LPARAM
)entry
;
337 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
339 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
340 tvItem
.mask
|= TVIF_STATE
;
341 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
342 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
345 tvInsert
.item
= tvItem
;
346 tvInsert
.hInsertAfter
= TVI_LAST
;
347 tvInsert
.hParent
= hParentItem
;
349 (void)TreeView_InsertItem(_left_hwnd
, &tvInsert
);
355 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
360 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
362 CONTEXT("ShellBrowser::OnTreeItemSelected()");
364 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
366 _last_sel
= pnmtv
->itemNew
.hItem
;
369 _callback
->entry_selected(entry
);
372 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
374 CONTEXT("ShellBrowser::UpdateFolderView()");
377 IShellView
* pLastShellView
= _pShellView
;
382 pLastShellView
->GetCurrentInfo(&fs
);
384 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
385 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
388 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
389 SFV_CREATE sfv_create
;
391 sfv_create
.cbSize
= sizeof(SFV_CREATE
);
392 sfv_create
.pshf
= folder
;
393 sfv_create
.psvOuter
= NULL
;
394 sfv_create
.psfvcb
= this;
396 HRESULT hr
= SHCreateShellFolderView(&sfv_create
, &_pShellView
);
398 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
406 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
407 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
409 if (pLastShellView
) {
410 pLastShellView
->GetCurrentInfo(&fs
);
411 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
412 pLastShellView
->DestroyViewWindow();
413 pLastShellView
->Release();
416 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
420 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
422 /// shell view callback
423 HRESULT STDMETHODCALLTYPE
ShellBrowser::MessageSFVCB(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
425 if (uMsg
== SFVM_INITMENUPOPUP
) {
426 //@todo never reached
427 InsertMenu((HMENU
)lParam
, 0, MF_BYPOSITION
, 12345, TEXT("TEST ENTRY"));
437 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
439 CONTEXT("ShellBrowser::OnDefaultCommand()");
441 if (pida
->cidl
>= 1) {
442 if (_left_hwnd
) { // explorer mode
444 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
448 parent
->smart_scan();
449 } catch(COMException
& e
) {
453 UINT firstOffset
= pida
->aoffset
[1];
454 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
456 Entry
* entry
= parent
->find_entry(pidl
);
458 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
459 if (entry
->_etype
== ET_SHELL
)
460 if (_last_sel
&& select_entry(_last_sel
, entry
))
464 } else { // no tree control
465 if (MainFrameBase::OpenShellFolders(pida
, _hWndFrame
))
468 /* create new Frame Window
469 if (MainFrame::OpenShellFolders(pida, 0))
479 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
481 CONTEXT("ShellBrowser::select_entry()");
483 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
486 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
487 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
488 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
490 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
503 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
508 // iterate through the hierarchy and open all folders to reach pidl
511 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
512 Entry
* entry
= _root
._entry
;
514 for(const void*p
=pidl
;;) {
518 if (!entry
|| !hitem
)
521 entry
->smart_scan(SORT_NAME
);
523 Entry
* found
= entry
->find_entry(p
);
524 p
= entry
->get_next_path_component(p
);
527 hitem
= select_entry(hitem
, found
);
538 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
541 _shellpath_info(info
) //@@ copies info -> no referenz to _create_info !
543 /**todo Conversion of shell path into path string -> store into URL history
544 const String& path = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORADDRESSBAR);
545 const String& parsingpath = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORPARSING);
547 // store path into history
548 if (info._path && *info._path)
549 _url_history.push(info._path);
554 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
556 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
557 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
559 return static_cast<MDIShellBrowserChild
*>(child
);
563 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
565 CONTEXT("MDIShellBrowserChild::Init()");
567 if (super::Init(pcs
))
570 update_shell_browser();
572 if (_shellBrowser
.get())
574 _shellBrowser
->Init();
576 _shellBrowser
->UpdateFolderView(_create_info
._shell_path
.get_folder());
582 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
585 case PM_DISPATCH_COMMAND
: {
586 switch(LOWORD(wparam
)) {
587 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
588 MDIShellBrowserChild::create(_create_info
);
592 //@todo refresh shell child
593 _shellBrowser
->invalidate_cache();
597 MainFrameBase::Create(ExplorerCmd(_url
, false));
601 return super::WndProc(nmsg
, wparam
, lparam
);
606 return super::WndProc(nmsg
, wparam
, lparam
);
612 void MDIShellBrowserChild::update_shell_browser()
614 int split_pos
= DEFAULT_SPLIT_POS
;
616 if (_shellBrowser
.get()) {
617 split_pos
= _split_pos
;
618 delete _shellBrowser
.release();
621 ///@todo use OWM_ROOTED flag
623 // create explorer treeview
624 if (_create_info
._open_mode
& OWM_EXPLORE
) {
626 ClientRect
rect(_hwnd
);
628 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
629 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
630 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
631 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
635 DestroyWindow(_left_hwnd
);
640 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _left_hwnd
, _right_hwnd
,
641 _shellpath_info
, this, _cm_ifs
));
643 _shellBrowser
->Init(_hwndFrame
);
647 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
651 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
652 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
656 if (SplitFileSysURL(url
, dir
, fname
)) {
660 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
661 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
668 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
670 if (entry
->_etype
== ET_SHELL
) {
671 ShellEntry
* shell_entry
= static_cast<ShellEntry
*>(entry
);
672 IShellFolder
* folder
;
674 if (shell_entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
675 folder
= static_cast<ShellDirectory
*>(shell_entry
)->_folder
;
677 folder
= shell_entry
->get_parent_folder();
684 TCHAR path
[MAX_PATH
];
686 if (shell_entry
->get_path(path
, COUNTOF(path
))) {
690 url
.printf(TEXT("shell://%s"), path
);
692 url
.printf(TEXT("file://%s"), path
);
697 _shellBrowser
->UpdateFolderView(folder
);
699 // set size of new created shell view windows
700 ClientRect
rt(_hwnd
);
701 resize_children(rt
.right
, rt
.bottom
);