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 HIMAGELIST himl
, 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
),
62 ShellBrowser::~ShellBrowser()
64 (void)TreeView_SetImageList(_left_hwnd
, _himl_old
, TVSIL_NORMAL
);
67 _pShellView
->Release();
70 _pDropTarget
->Release();
75 DestroyWindow(_right_hwnd
);
81 LRESULT
ShellBrowser::Init(HWND hWndFrame
)
83 CONTEXT("ShellBrowser::Init()");
85 _hWndFrame
= hWndFrame
;
87 const String
& root_name
= GetDesktopFolder().get_name(_create_info
._root_shell_path
, SHGDN_FORADDRESSBAR
);
89 _root
._drive_type
= DRIVE_UNKNOWN
;
90 lstrcpy(_root
._volname
, root_name
);
92 lstrcpy(_root
._fs
, TEXT("Desktop"));
94 _root
._entry
= new ShellDirectory(GetDesktopFolder(), _create_info
._root_shell_path
, _hwnd
);
96 jump_to(_create_info
._shell_path
);
99 _root
._entry
->read_directory();
101 /* already filled by ShellDirectory constructor
102 lstrcpy(_root._entry->_data.cFileName, TEXT("Desktop")); */
107 void ShellBrowser::jump_to(LPCITEMIDLIST pidl
)
113 _cur_dir
= static_cast<ShellDirectory
*>(_root
._entry
);
115 //LOG(FmtString(TEXT("ShellBrowser::jump_to(): pidl=%s"), (LPCTSTR)FileSysShellPath(pidl)));
118 static DynamicFct
<LPITEMIDLIST(WINAPI
*)(LPCITEMIDLIST
, LPCITEMIDLIST
)> ILFindChild(TEXT("SHELL32"), 24);
121 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)
122 _root._entry->read_tree(_create_info._root_shell_path.get_folder(), info._shell_path, SORT_NAME);
123 -> see FileChildWindow::FileChildWindow()_create_info._shell_path
126 LPCITEMIDLIST child_pidl
;
129 child_pidl
= (*ILFindChild
)(_cur_dir
->create_absolute_pidl(), pidl
);
131 child_pidl
= pidl
; // This is not correct in the common case, but works on the desktop level.
134 _cur_dir
->smart_scan();
136 entry
= _cur_dir
->find_entry(child_pidl
);
139 _cur_dir
= static_cast<ShellDirectory
*>(entry
);
140 _callback
->entry_selected(entry
);
145 //@@ work around as long as we don't iterate correctly through the ShellEntry tree
147 UpdateFolderView(ShellFolder(pidl
));
151 void ShellBrowser::InitializeTree(HIMAGELIST himl
)
153 CONTEXT("ShellBrowserChild::InitializeTree()");
155 _himl_old
= TreeView_SetImageList(_left_hwnd
, himl
, TVSIL_NORMAL
);
156 TreeView_SetScrollTime(_left_hwnd
, 100);
158 TV_INSERTSTRUCT tvInsert
;
160 tvInsert
.hParent
= 0;
161 tvInsert
.hInsertAfter
= TVI_LAST
;
163 TV_ITEM
& tvItem
= tvInsert
.item
;
164 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
165 tvItem
.lParam
= (LPARAM
)_root
._entry
;
166 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
167 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
168 tvItem
.cChildren
= 1;
170 HTREEITEM hItem
= TreeView_InsertItem(_left_hwnd
, &tvInsert
);
171 TreeView_SelectItem(_left_hwnd
, hItem
);
172 TreeView_Expand(_left_hwnd
, hItem
, TVE_EXPAND
);
175 bool ShellBrowser::InitDragDrop()
177 CONTEXT("ShellBrowser::InitDragDrop()");
179 _pDropTarget
= new TreeDropTarget(_left_hwnd
);
184 _pDropTarget
->AddRef();
186 if (FAILED(RegisterDragDrop(_left_hwnd
, _pDropTarget
))) {//calls addref
187 _pDropTarget
->Release(); // free TreeDropTarget
192 _pDropTarget
->Release();
196 ftetc
.dwAspect
= DVASPECT_CONTENT
;
198 ftetc
.tymed
= TYMED_HGLOBAL
;
199 ftetc
.cfFormat
= CF_HDROP
;
201 _pDropTarget
->AddSuportedFormat(ftetc
);
207 void ShellBrowser::OnTreeItemRClick(int idCtrl
, LPNMHDR pnmh
)
209 CONTEXT("ShellBrowser::OnTreeItemRClick()");
213 GetCursorPos(&tvhti
.pt
);
214 ScreenToClient(_left_hwnd
, &tvhti
.pt
);
216 tvhti
.flags
= LVHT_NOWHERE
;
217 (void)TreeView_HitTest(_left_hwnd
, &tvhti
);
219 if (TVHT_ONITEM
& tvhti
.flags
) {
220 LPARAM itemData
= TreeView_GetItemData(_left_hwnd
, tvhti
.hItem
);
223 Entry
* entry
= (Entry
*)itemData
;
224 ClientToScreen(_left_hwnd
, &tvhti
.pt
);
226 CHECKERROR(entry
->do_context_menu(_hwnd
, tvhti
.pt
, _cm_ifs
));
231 void ShellBrowser::OnTreeGetDispInfo(int idCtrl
, LPNMHDR pnmh
)
233 CONTEXT("ShellBrowser::OnTreeGetDispInfo()");
235 LPNMTVDISPINFO lpdi
= (LPNMTVDISPINFO
)pnmh
;
236 ShellEntry
* entry
= (ShellEntry
*)lpdi
->item
.lParam
;
239 if (lpdi
->item
.mask
& TVIF_TEXT
)
240 lpdi
->item
.pszText
= entry
->_display_name
;
242 if (lpdi
->item
.mask
& (/*TVIF_TEXT|*/TVIF_IMAGE
|TVIF_SELECTEDIMAGE
)) {
243 ShellPath pidl_abs
= entry
->create_absolute_pidl(); // Caching of absolute PIDLs could enhance performance.
244 LPCITEMIDLIST pidl
= pidl_abs
;
248 if (lpdi->item.mask & TVIF_TEXT)
249 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_DISPLAYNAME))
250 lstrcpy(lpdi->item.pszText, sfi.szDisplayName); ///@todo look at cchTextMax if there is enough space available
252 lpdi->item.pszText = entry->_data.cFileName;
254 if (lpdi
->item
.mask
& TVIF_IMAGE
)
255 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_LINKOVERLAY
|SHGFI_SMALLICON
) == _himl
)
256 lpdi
->item
.iImage
= sfi
.iIcon
;
258 lpdi
->item
.iImage
= -1;
260 if (lpdi
->item
.mask
& TVIF_SELECTEDIMAGE
)
261 if ((HIMAGELIST
)SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_PIDL
|SHGFI_SYSICONINDEX
|SHGFI_OPENICON
|SHGFI_SMALLICON
) == _himl
)
262 lpdi
->item
.iSelectedImage
= sfi
.iIcon
;
264 lpdi
->item
.iSelectedImage
= -1;
269 void ShellBrowser::OnTreeItemExpanding(int idCtrl
, LPNMTREEVIEW pnmtv
)
271 CONTEXT("ShellBrowser::OnTreeItemExpanding()");
273 if (pnmtv
->action
== TVE_COLLAPSE
)
274 TreeView_Expand(_left_hwnd
, pnmtv
->itemNew
.hItem
, TVE_COLLAPSE
|TVE_COLLAPSERESET
);
275 else if (pnmtv
->action
== TVE_EXPAND
) {
276 ShellDirectory
* entry
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, pnmtv
->itemNew
.hItem
);
279 if (!InsertSubitems(pnmtv
->itemNew
.hItem
, entry
, entry
->_folder
)) {
280 entry
->_shell_attribs
&= ~SFGAO_HASSUBFOLDER
;
282 // remove subitem "+"
285 tvItem
.mask
= TVIF_CHILDREN
;
286 tvItem
.hItem
= pnmtv
->itemNew
.hItem
;
287 tvItem
.cChildren
= 0;
289 TreeView_SetItem(_left_hwnd
, &tvItem
);
294 int ShellBrowser::InsertSubitems(HTREEITEM hParentItem
, Entry
* entry
, IShellFolder
* pParentFolder
)
296 CONTEXT("ShellBrowser::InsertSubitems()");
302 SendMessage(_left_hwnd
, WM_SETREDRAW
, FALSE
, 0);
306 } catch(COMException
& e
) {
307 HandleException(e
, g_Globals
._hMainWnd
);
311 TV_INSERTSTRUCT tvInsert
;
313 for(entry
=entry
->_down
; entry
; entry
=entry
->_next
) {
315 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
318 ZeroMemory(&tvItem
, sizeof(tvItem
));
320 tvItem
.mask
= TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
;
321 tvItem
.pszText
= LPSTR_TEXTCALLBACK
;
322 tvItem
.iImage
= tvItem
.iSelectedImage
= I_IMAGECALLBACK
;
323 tvItem
.lParam
= (LPARAM
)entry
;
324 tvItem
.cChildren
= entry
->_shell_attribs
& SFGAO_HASSUBFOLDER
? 1: 0;
326 if (entry
->_shell_attribs
& SFGAO_SHARE
) {
327 tvItem
.mask
|= TVIF_STATE
;
328 tvItem
.stateMask
|= TVIS_OVERLAYMASK
;
329 tvItem
.state
|= INDEXTOOVERLAYMASK(1);
332 tvInsert
.item
= tvItem
;
333 tvInsert
.hInsertAfter
= TVI_LAST
;
334 tvInsert
.hParent
= hParentItem
;
336 (void)TreeView_InsertItem(_left_hwnd
, &tvInsert
);
342 SendMessage(_left_hwnd
, WM_SETREDRAW
, TRUE
, 0);
347 void ShellBrowser::OnTreeItemSelected(int idCtrl
, LPNMTREEVIEW pnmtv
)
349 CONTEXT("ShellBrowser::OnTreeItemSelected()");
351 ShellEntry
* entry
= (ShellEntry
*)pnmtv
->itemNew
.lParam
;
353 _last_sel
= pnmtv
->itemNew
.hItem
;
356 _callback
->entry_selected(entry
);
359 void ShellBrowser::UpdateFolderView(IShellFolder
* folder
)
361 CONTEXT("ShellBrowser::UpdateFolderView()");
364 IShellView
* pLastShellView
= _pShellView
;
369 pLastShellView
->GetCurrentInfo(&fs
);
371 fs
.ViewMode
= _create_info
._open_mode
&OWM_DETAILS
? FVM_DETAILS
: FVM_ICON
;
372 fs
.fFlags
= FWF_NOCLIENTEDGE
|FWF_BESTFITWINDOW
;
375 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
376 SFV_CREATE sfv_create
;
378 sfv_create
.cbSize
= sizeof(SFV_CREATE
);
379 sfv_create
.pshf
= folder
;
380 sfv_create
.psvOuter
= NULL
;
381 sfv_create
.psfvcb
= this;
383 HRESULT hr
= SHCreateShellFolderView(&sfv_create
, &_pShellView
);
385 HRESULT hr
= folder
->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
393 RECT rect
= {CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
};
394 hr
= _pShellView
->CreateViewWindow(pLastShellView
, &fs
, static_cast<IShellBrowser
*>(this), &rect
, &_right_hwnd
/*&m_hWndListView*/);
396 if (pLastShellView
) {
397 pLastShellView
->GetCurrentInfo(&fs
);
398 pLastShellView
->UIActivate(SVUIA_DEACTIVATE
);
399 pLastShellView
->DestroyViewWindow();
400 pLastShellView
->Release();
403 _pShellView
->UIActivate(SVUIA_ACTIVATE_NOFOCUS
);
407 #ifndef __MINGW32__ // IShellFolderViewCB missing in MinGW (as of 25.09.2005)
409 /// shell view callback
410 HRESULT STDMETHODCALLTYPE
ShellBrowser::MessageSFVCB(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
412 if (uMsg
== SFVM_INITMENUPOPUP
) {
413 //@todo never reached
414 InsertMenu((HMENU
)lParam
, 0, MF_BYPOSITION
, 12345, TEXT("TEST ENTRY"));
424 HRESULT
ShellBrowser::OnDefaultCommand(LPIDA pida
)
426 CONTEXT("ShellBrowser::OnDefaultCommand()");
428 if (pida
->cidl
>= 1) {
429 if (_left_hwnd
) { // explorer mode
431 ShellDirectory
* parent
= (ShellDirectory
*)TreeView_GetItemData(_left_hwnd
, _last_sel
);
435 parent
->smart_scan();
436 } catch(COMException
& e
) {
440 UINT firstOffset
= pida
->aoffset
[1];
441 LPITEMIDLIST pidl
= (LPITEMIDLIST
)((LPBYTE
)pida
+firstOffset
);
443 Entry
* entry
= parent
->find_entry(pidl
);
445 if (entry
&& (entry
->_data
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
))
446 if (entry
->_etype
== ET_SHELL
)
447 if (_last_sel
&& select_entry(_last_sel
, entry
))
451 } else { // no tree control
452 if (MainFrameBase::OpenShellFolders(pida
, _hWndFrame
))
455 /* create new Frame Window
456 if (MainFrame::OpenShellFolders(pida, 0))
466 HTREEITEM
ShellBrowser::select_entry(HTREEITEM hitem
, Entry
* entry
, bool expand
)
468 CONTEXT("ShellBrowser::select_entry()");
470 if (expand
&& !TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
))
473 for(hitem
=TreeView_GetChild(_left_hwnd
,hitem
); hitem
; hitem
=TreeView_GetNextSibling(_left_hwnd
,hitem
)) {
474 if ((Entry
*)TreeView_GetItemData(_left_hwnd
,hitem
) == entry
) {
475 if (TreeView_SelectItem(_left_hwnd
, hitem
)) {
477 TreeView_Expand(_left_hwnd
, hitem
, TVE_EXPAND
);
490 bool ShellBrowser::jump_to_pidl(LPCITEMIDLIST pidl
)
495 // iterate through the hierarchy and open all folders to reach pidl
498 HTREEITEM hitem
= TreeView_GetRoot(_left_hwnd
);
499 Entry
* entry
= _root
._entry
;
501 for(const void*p
=pidl
;;) {
505 if (!entry
|| !hitem
)
508 entry
->smart_scan(SORT_NAME
);
510 Entry
* found
= entry
->find_entry(p
);
511 p
= entry
->get_next_path_component(p
);
514 hitem
= select_entry(hitem
, found
);
525 MDIShellBrowserChild::MDIShellBrowserChild(HWND hwnd
, const ShellChildWndInfo
& info
)
528 _shellpath_info(info
) //@@ copies info -> no referenz to _create_info !
530 /**todo Conversion of shell path into path string -> store into URL history
531 const String& path = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORADDRESSBAR);
532 const String& parsingpath = GetDesktopFolder().get_name(info._shell_path, SHGDN_FORPARSING);
534 // store path into history
535 if (info._path && *info._path)
536 _url_history.push(info._path);
541 MDIShellBrowserChild
* MDIShellBrowserChild::create(const ShellChildWndInfo
& info
)
543 ChildWindow
* child
= ChildWindow::create(info
, info
._pos
.rcNormalPosition
,
544 WINDOW_CREATOR_INFO(MDIShellBrowserChild
,ShellChildWndInfo
), CLASSNAME_CHILDWND
, NULL
, info
._pos
.showCmd
==SW_SHOWMAXIMIZED
? WS_MAXIMIZE
: 0);
546 return static_cast<MDIShellBrowserChild
*>(child
);
550 LRESULT
MDIShellBrowserChild::Init(LPCREATESTRUCT pcs
)
552 CONTEXT("MDIShellBrowserChild::Init()");
554 if (super::Init(pcs
))
559 update_shell_browser();
563 _shellBrowser
->Init(_himlSmall
);
565 _shellBrowser
->UpdateFolderView(_create_info
._shell_path
.get_folder());
571 LRESULT
MDIShellBrowserChild::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
574 case PM_DISPATCH_COMMAND
: {
575 switch(LOWORD(wparam
)) {
576 case ID_WINDOW_NEW
: {CONTEXT("MDIShellBrowserChild PM_DISPATCH_COMMAND ID_WINDOW_NEW");
577 MDIShellBrowserChild::create(_create_info
);
581 //@todo refresh shell child
585 MainFrameBase::Create(ExplorerCmd(_url
, false));
589 return super::WndProc(nmsg
, wparam
, lparam
);
594 return super::WndProc(nmsg
, wparam
, lparam
);
600 void MDIShellBrowserChild::update_shell_browser()
602 int split_pos
= DEFAULT_SPLIT_POS
;
604 if (_shellBrowser
.get()) {
605 split_pos
= _split_pos
;
606 delete _shellBrowser
.release();
609 ///@todo use OWM_ROOTED flag
611 // create explorer treeview
612 if (_create_info
._open_mode
& OWM_EXPLORE
) {
614 ClientRect
rect(_hwnd
);
616 _left_hwnd
= CreateWindowEx(0, WC_TREEVIEW
, NULL
,
617 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|WS_CHILD
|TVS_HASLINES
|TVS_LINESATROOT
|TVS_HASBUTTONS
|TVS_SHOWSELALWAYS
,//|TVS_NOTOOLTIPS
618 0, rect
.top
, split_pos
-SPLIT_WIDTH
/2, rect
.bottom
-rect
.top
,
619 _hwnd
, (HMENU
)IDC_FILETREE
, g_Globals
._hInstance
, 0);
623 DestroyWindow(_left_hwnd
);
628 _shellBrowser
= auto_ptr
<ShellBrowser
>(new ShellBrowser(_hwnd
, _left_hwnd
, _right_hwnd
,
629 _shellpath_info
, _himlSmall
, this, _cm_ifs
));
631 _shellBrowser
->Init(_hwndFrame
);
635 String
MDIShellBrowserChild::jump_to_int(LPCTSTR url
)
639 if (!_tcsnicmp(url
, TEXT("shell://"), 8)) {
640 if (_shellBrowser
->jump_to_pidl(ShellPath(url
+8)))
644 if (SplitFileSysURL(url
, dir
, fname
)) {
648 if (_shellBrowser
->jump_to_pidl(ShellPath(dir
)))
649 return FmtString(TEXT("file://%s"), (LPCTSTR
)dir
);
656 void MDIShellBrowserChild::entry_selected(Entry
* entry
)
658 if (entry
->_etype
== ET_SHELL
) {
659 ShellEntry
* shell_entry
= static_cast<ShellEntry
*>(entry
);
660 IShellFolder
* folder
;
662 if (shell_entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
663 folder
= static_cast<ShellDirectory
*>(shell_entry
)->_folder
;
665 folder
= shell_entry
->get_parent_folder();
672 TCHAR path
[MAX_PATH
];
674 if (shell_entry
->get_path(path
, COUNTOF(path
))) {
678 url
.printf(TEXT("shell://%s"), path
);
680 url
.printf(TEXT("file://%s"), path
);
685 _shellBrowser
->UpdateFolderView(folder
);
687 // set size of new created shell view windows
688 ClientRect
rt(_hwnd
);
689 resize_children(rt
.right
, rt
.bottom
);