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 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()
53 (void)TreeView_SetImageList(_left_hwnd
, 0, TVSIL_NORMAL
);
56 _pShellView
->Release();
59 _pDropTarget
->Release();
64 DestroyWindow(_right_hwnd
);
70 LRESULT
ShellBrowser::Init(HWND hWndFrame
)
72 CONTEXT("ShellBrowser::Init()");
74 _hWndFrame
= hWndFrame
;
76 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORADDRESSBAR
);
78 _root
._drive_type
= DRIVE_UNKNOWN
;
79 lstrcpy(_root
._volname
, root_name
);
81 lstrcpy(_root
._fs
, TEXT("Desktop"));
83 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
85 jump_to(_create_info
._shell_path
);
88 _root
._entry
->read_directory();
90 /* already filled by ShellDirectory constructor
91 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
96 void ShellBrowser::jump_to(LPCITEMIDLIST pidl
)
102 _cur_dir
= static_cast<ShellDirectory
*>(_root
._entry
);
105 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
106 _root._entry->read_tree(shell_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME);
107 -> see FileChildWindow::FileChildWindow()
111 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
113 LPCITEMIDLIST child_pidl
;
116 child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
118 child_pidl
= pidl
; // This is not correct in the common case, but works on the desktop level.
121 _cur_dir
->smart_scan();
123 entry
= _cur_dir
->find_entry(child_pidl
);
126 _callback
->entry_selected(entry
);
130 //@@ work around as long as we don't iterate correctly through the ShellEntry tree
132 UpdateFolderView(ShellFolder(pidl
));
136 void ShellBrowser::InitializeTree(HIMAGELIST himl
)
138 CONTEXT("ShellBrowserChild::InitializeTree()");
140 (void)TreeView_SetImageList(_left_hwnd
, himl
, TVSIL_NORMAL
);
141 TreeView_SetScrollTime(_left_hwnd
, 100);
143 TV_INSERTSTRUCT tvInsert
;
145 tvInsert
.hParent
= 0;
146 tvInsert
.hInsertAfter
= TVI_LAST
;
148 TV_ITEM
& tvItem
= tvInsert
.item
;
149 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
150 tvItem
.lParam
= (LPARAM
)_root
._entry
;
151 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
152 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
153 tvItem
.cChildren
= 1;
155 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
156 TreeView_SelectItem(_left_hwnd
, hItem
);
157 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
160 bool ShellBrowser::InitDragDrop()
162 CONTEXT("ShellBrowser::InitDragDrop()");
164 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
169 _pDropTarget
->AddRef();
171 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
172 _pDropTarget
->Release(); // free TreeDropTarget
177 _pDropTarget
->Release();
181 ftetc
.dwAspect
= DVASPECT_CONTENT
;
183 ftetc
.tymed
= TYMED_HGLOBAL
;
184 ftetc
.cfFormat
= CF_HDROP
;
186 _pDropTarget
->AddSuportedFormat(ftetc
);
192 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
194 CONTEXT("ShellBrowser::OnTreeItemRClick()");
198 GetCursorPos(&tvhti
.pt
);
199 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
201 tvhti
.flags
= LVHT_NOWHERE
;
202 (void)TreeView_HitTest(_left_hwnd
, &tvhti
);
204 if (TVHT_ONITEM
& tvhti
.flags
) {
205 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
208 Entry
* entry
= (Entry
*)itemData
;
209 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
211 CHECKERROR(entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
));
216 void ShellBrowser::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
218 CONTEXT("ShellBrowser::OnTreeGetDispInfo()");
220 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
221 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
224 if (lpdi
->item
.mask
& TVIF_TEXT
)
225 lpdi
->item
.pszText
= entry
->_display_name
;
227 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
228 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
229 LPCITEMIDLIST pidl
= pidl_abs
;
233 if (lpdi->item.mask & TVIF_TEXT)
234 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
235 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); ///@todo look at cchTextMax if there is enough space available
237 lpdi->item.pszText = entry->_data.cFileName;
239 if (lpdi
->item
.mask
& TVIF_IMAGE
)
240 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_LINKOVERLAY
) == _himl
)
241 lpdi
->item
.iImage
= sfi
.iIcon
;
243 lpdi
->item
.iImage
= -1;
245 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
246 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
|SHGFI_OPENICON
) == _himl
)
247 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
249 lpdi
->item
.iSelectedImage
= -1;
254 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
256 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
258 if (pnmtv
->action
== TVE_COLLAPSE
)
259 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
260 else if (pnmtv
->action
== TVE_EXPAND
) {
261 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
264 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
265 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
267 // remove subitem "+"
270 tvItem
.mask
= TVIF_CHILDREN
;
271 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
272 tvItem
.cChildren
= 0;
274 TreeView_SetItem(_left_hwnd
, &tvItem
);
279 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
281 CONTEXT("ShellBrowser::InsertSubitems()");
287 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
291 } catch(COMException
& e
) {
292 HandleException(e
, g_Globals
._hMainWnd
);
296 TV_INSERTSTRUCT tvInsert
;
298 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
300 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
303 ZeroMemory(&tvItem
, sizeof(tvItem
));
305 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
306 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
307 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
308 tvItem
.lParam
= (LPARAM
)entry
;
309 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
311 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
312 tvItem
.mask
|= TVIF_STATE
;
313 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
314 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
317 tvInsert
.item
= tvItem
;
318 tvInsert
.hInsertAfter
= TVI_LAST
;
319 tvInsert
.hParent
= hParentItem
;
321 (void)TreeView_InsertItem(_left_hwnd
, &tvInsert
);
327 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
332 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
334 CONTEXT("ShellBrowser::OnTreeItemSelected()");
336 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
338 _last_sel
= pnmtv
->itemNew
.hItem
;
341 _callback
->entry_selected(entry
);
344 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
346 CONTEXT("ShellBrowser::UpdateFolderView()");
349 IShellView
* pLastShellView
= _pShellView
;
354 pLastShellView
->GetCurrentInfo(&fs
);
356 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
357 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
360 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
367 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
368 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
370 if (pLastShellView
) {
371 pLastShellView
->GetCurrentInfo(&fs
);
372 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
373 pLastShellView
->DestroyViewWindow();
374 pLastShellView
->Release();
377 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
381 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
383 CONTEXT("ShellBrowser::OnDefaultCommand()");
385 if (pida
->cidl
>= 1) {
386 if (_left_hwnd
) { // explorer mode
388 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
392 parent
->smart_scan();
393 } catch(COMException
& e
) {
397 UINT firstOffset
= pida
->aoffset
[1];
398 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
400 Entry
* entry
= parent
->find_entry(pidl
);
402 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
403 if (entry
->_etype
== ET_SHELL
)
404 if (_last_sel
&& select_entry(_last_sel
, entry
))
408 } else { // no tree control
409 if (MainFrameBase::OpenShellFolders(pida
, _hWndFrame
))
412 /* create new Frame Window
413 if (MainFrame::OpenShellFolders(pida, 0))
423 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
425 CONTEXT("ShellBrowser::select_entry()");
427 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
430 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
431 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
432 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
434 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
447 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
452 // iterate through the hierarchy and open all folders to reach pidl
455 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
456 Entry
* entry
= _root
._entry
;
458 for(const void*p
=pidl
;;) {
462 if (!entry
|| !hitem
)
465 entry
->smart_scan(SORT_NAME
);
467 Entry
* found
= entry
->find_entry(p
);
468 p
= entry
->get_next_path_component(p
);
471 hitem
= select_entry(hitem
, found
);
482 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
485 _shellpath_info(info
) //@@ copies info -> no referenz to _create_info !
487 /**todo Conversion of shell path into path string -> store into URL history
488 const String& path = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORADDRESSBAR);
489 const String& parsingpath = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORPARSING);
491 // store path into history
492 if (info._path && *info._path)
493 _url_history.push(info._path);
498 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
500 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
501 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
503 return static_cast<MDIShellBrowserChild
*>(child
);
507 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
509 CONTEXT("MDIShellBrowserChild::Init()");
511 if (super::Init(pcs
))
516 update_shell_browser();
520 _shellBrowser
->Init(_himlSmall
);
522 _shellBrowser
->UpdateFolderView(_create_info
._shell_path
.get_folder());
528 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
531 case PM_DISPATCH_COMMAND
: {
532 switch(LOWORD(wparam
)) {
533 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
534 MDIShellBrowserChild::create(_create_info
);
538 //@todo refresh shell child
542 MainFrameBase::Create(_url
, false);
546 return super::WndProc(nmsg
, wparam
, lparam
);
551 return super::WndProc(nmsg
, wparam
, lparam
);
557 void MDIShellBrowserChild::update_shell_browser()
559 int split_pos
= DEFAULT_SPLIT_POS
;
561 if (_shellBrowser
.get()) {
562 split_pos
= _split_pos
;
563 delete _shellBrowser
.release();
566 // create explorer treeview
567 if (_create_info
._open_mode
& OWM_EXPLORE
) {
569 ClientRect
rect(_hwnd
);
571 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
572 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
573 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
574 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
578 DestroyWindow(_left_hwnd
);
583 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _left_hwnd
, _right_hwnd
,
584 _shellpath_info
, _himlSmall
, this, _cm_ifs
));
586 _shellBrowser
->Init(_hwndFrame
);
590 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
594 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
595 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
599 if (SplitFileSysURL(url
, dir
, fname
)) {
603 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
604 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
611 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
613 if (entry
->_etype
== ET_SHELL
) {
614 ShellEntry
* shell_entry
= static_cast<ShellEntry
*>(entry
);
615 IShellFolder
* folder
;
617 if (shell_entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
618 folder
= static_cast<ShellDirectory
*>(shell_entry
)->_folder
;
620 folder
= shell_entry
->get_parent_folder();
627 TCHAR path
[MAX_PATH
];
629 if (shell_entry
->get_path(path
)) {
633 url
.printf(TEXT("shell://%s"), path
);
635 url
.printf(TEXT("file://%s"), path
);
640 _shellBrowser
->UpdateFolderView(folder
);
642 // set size of new created shell view windows
643 ClientRect
rt(_hwnd
);
644 resize_children(rt
.right
, rt
.bottom
);