Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 260 additions & 0 deletions Src/ClassicExplorer/ExplorerBHO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "DownloadHelper.h"
#include "FNVHash.h"
#include "dllmain.h"
#include "ExplorerBand.h"
#include <uxtheme.h>
#include <dwmapi.h>
#include <Ntquery.h>
Expand Down Expand Up @@ -1445,6 +1446,8 @@ HRESULT STDMETHODCALLTYPE CExplorerBHO::SetSite( IUnknown *pUnkSite )
else
{
// unhook
ClearAlternateBand();
m_bProbedAlternateBand=false;
GetTlsData()->bho=NULL;
if (m_Hook)
UnhookWindowsHookEx(m_Hook);
Expand Down Expand Up @@ -1512,6 +1515,248 @@ void CExplorerBHO::ClearComboItems( void )
}
}

// Reserve one toolbar row only after the tab host is laid out.
void CExplorerBHO::ProbeAlternateBand( IShellView *pView )
{
if (m_bProbedAlternateBand || !pView || !m_TopWindow || !m_pBrowser || !m_pWebBrowser)
return;
// Respect the existing opt-out from automatic toolbar activation.
if (GetSettingBool(L"NoInitialToolbar"))
return;

HWND view=NULL;
HRESULT hr=pView->GetWindow(&view);
if (FAILED(hr) || !IsWindow(view))
return;

HWND host=NULL;
for (HWND hwnd=view;hwnd && hwnd!=m_TopWindow;hwnd=GetParent(hwnd))
{
wchar_t name[128];
if (GetClassName(hwnd,name,_countof(name)) && _wcsicmp(name,L"ShellTabWindowClass")==0)
{
host=hwnd;
break;
}
}
if (!host || GetAncestor(host,GA_ROOT)!=m_TopWindow || GetWindowThreadProcessId(host,NULL)!=GetCurrentThreadId())
return;
if (FindChildWindow(m_TopWindow,REBARCLASSNAME))
return;

m_bProbedAlternateBand=true;
m_AlternateHost=host;
if (!SetWindowSubclass(host,AlternateHostProc,(UINT_PTR)this,0))
{
m_AlternateHost=NULL;
return;
}
// The initial document event occurs before ShellTabWindowClass has a size.
m_AlternateTicks=0;
if (!SetTimer(host,(UINT_PTR)this,250,NULL))
ClearAlternateBand();
}

bool CExplorerBHO::LayoutAlternateBand( void )
{
HWND host=m_AlternateHost;
if (!IsWindowVisible(host))
return false;
HWND view=FindWindowEx(host,NULL,L"DUIViewWndClassName",NULL);
RECT rc, client;
if (!IsWindowVisible(view) || !GetClientRect(host,&client) || !GetWindowRect(view,&rc))
return false;
MapWindowPoints(NULL,host,(POINT*)&rc,2);
// Explorer can replace the reserved rectangle during navigation or resizing.
// Keep the existing toolbar and reserve space again only for the known view.
if (m_AlternateBand)
{
if (view!=m_AlternateView || !m_AlternateBand->IsWindow())
return false;
HWND toolbar=m_AlternateBand->GetToolbar();
int height=m_AlternateReserved.top-m_AlternateOriginal.top;
if (!IsWindow(toolbar) || height<=0)
return false;
if (EqualRect(&rc,&m_AlternateReserved) && rc.right==client.right)
return true;
if (rc.top!=0 || rc.left!=0 || rc.right!=client.right || rc.bottom>client.bottom || rc.bottom<100 || height>rc.bottom/2)
return false;
RECT reserved=rc;
reserved.top+=height;
// Discard pixels copied while the native view overlapped the toolbar.
if (!SetWindowPos(view,NULL,rc.left,reserved.top,rc.right-rc.left,rc.bottom-reserved.top,SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS))
return false;
m_AlternateOriginal=rc;
m_AlternateReserved=reserved;
::SetWindowPos(m_AlternateBand->m_hWnd,HWND_TOP,0,0,rc.right,height,SWP_NOACTIVATE|SWP_SHOWWINDOW);
::SetWindowPos(toolbar,HWND_TOP,0,0,rc.right,height,SWP_NOACTIVATE|SWP_SHOWWINDOW);
// Queue a repaint only after repairing the native content rectangle.
RedrawWindow(view,NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
return true;
}
// Only the observed layout is supported: a direct content child spanning the tab.
if (rc.top!=0 || rc.left!=0 || rc.right!=client.right || rc.bottom>client.bottom || rc.bottom<100)
return false;

CBandWindow *band=new CBandWindow;
band->SetBrowsers(m_pBrowser,m_pWebBrowser);
band->Create(host,NULL,NULL,WS_CHILD|WS_CLIPSIBLINGS);
HWND toolbar=band->GetToolbar();
RECT item={};
int count=IsWindow(toolbar)?(int)::SendMessage(toolbar,TB_BUTTONCOUNT,0,0):0;
if (!band->IsWindow() || count<=0 || !::SendMessage(toolbar,TB_GETITEMRECT,count-1,(LPARAM)&item) || item.bottom<=0 || item.bottom>rc.bottom/2)
{
if (band->IsWindow()) band->DestroyWindow();
band->Clear();
delete band;
return false;
}
int height=item.bottom;
RECT reserved=rc;
reserved.top+=height;
if (!SetWindowPos(view,NULL,rc.left,reserved.top,rc.right-rc.left,rc.bottom-reserved.top,SWP_NOZORDER|SWP_NOACTIVATE))
{
band->DestroyWindow();
band->Clear();
delete band;
return false;
}
m_AlternateBand=band;
m_AlternateView=view;
m_AlternateOriginal=rc;
m_AlternateReserved=reserved;
// Reserve the toolbar row before native content layout changes become visible.
if (GetWindowThreadProcessId(view,NULL)==GetCurrentThreadId())
SetWindowSubclass(view,AlternateViewProc,(UINT_PTR)this,0);
::SetWindowPos(band->m_hWnd,HWND_TOP,0,0,rc.right,height,SWP_NOACTIVATE|SWP_SHOWWINDOW);
::SetWindowPos(toolbar,HWND_TOP,0,0,rc.right,height,SWP_NOACTIVATE|SWP_SHOWWINDOW);
band->UpdateToolbar();
return true;
}

void CExplorerBHO::ClearAlternateBand( void )
{
if (IsWindow(m_AlternateView))
RemoveWindowSubclass(m_AlternateView,AlternateViewProc,(UINT_PTR)this);
if (IsWindow(m_AlternateHost))
{
KillTimer(m_AlternateHost,(UINT_PTR)this);
RemoveWindowSubclass(m_AlternateHost,AlternateHostProc,(UINT_PTR)this);
}
if (m_AlternateBand)
{
CBandWindow *band=m_AlternateBand;
m_AlternateBand=NULL;
if (band->IsWindow()) band->DestroyWindow();
band->Clear();
delete band;
}
// Restore only if Explorer has not already changed the layout.
RECT rc;
if (IsWindow(m_AlternateHost) && IsWindow(m_AlternateView) && GetParent(m_AlternateView)==m_AlternateHost && GetWindowRect(m_AlternateView,&rc))
{
MapWindowPoints(NULL,m_AlternateHost,(POINT*)&rc,2);
if (EqualRect(&rc,&m_AlternateReserved))
{
const RECT &old=m_AlternateOriginal;
SetWindowPos(m_AlternateView,NULL,old.left,old.top,old.right-old.left,old.bottom-old.top,SWP_NOZORDER|SWP_NOACTIVATE);
}
}
m_AlternateHost=m_AlternateView=NULL;
}

LRESULT CALLBACK CExplorerBHO::AlternateHostProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
CExplorerBHO *pThis=(CExplorerBHO*)uIdSubclass;
if (uMsg==WM_WINDOWPOSCHANGING && pThis->m_AlternateBand)
{
LRESULT result=DefSubclassProc(hWnd,uMsg,wParam,lParam);
WINDOWPOS *pos=(WINDOWPOS*)lParam;
RECT old;
// Avoid preserving a composite of the toolbar and native view on resize.
if (pos && !(pos->flags&SWP_NOSIZE) && GetWindowRect(hWnd,&old) &&
(pos->cx!=old.right-old.left || pos->cy!=old.bottom-old.top))
pos->flags|=SWP_NOCOPYBITS;
return result;
}
if (uMsg==WM_TIMER && wParam==uIdSubclass)
{
// Keep observing the tab host after initial activation. Explorer
// may reset its content rectangle asynchronously after WM_SIZE.
if (!pThis->LayoutAlternateBand() && !pThis->m_AlternateBand && ++pThis->m_AlternateTicks>=40)
KillTimer(hWnd,uIdSubclass);
return 0;
}
if (uMsg==WM_NCDESTROY)
{
KillTimer(hWnd,uIdSubclass);
RemoveWindowSubclass(hWnd,AlternateHostProc,uIdSubclass);
pThis->m_AlternateHost=NULL;
pThis->ClearAlternateBand();
pThis->m_bProbedAlternateBand=false;
}
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
}

LRESULT CALLBACK CExplorerBHO::AlternateViewProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
CExplorerBHO *pThis=(CExplorerBHO*)uIdSubclass;
if (uMsg==WM_NCDESTROY)
{
RemoveWindowSubclass(hWnd,AlternateViewProc,uIdSubclass);
if (pThis->m_AlternateView==hWnd)
pThis->m_AlternateView=NULL;
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
}
if (uMsg==WM_WINDOWPOSCHANGING)
{
LRESULT result=DefSubclassProc(hWnd,uMsg,wParam,lParam);
WINDOWPOS *pos=(WINDOWPOS*)lParam;
HWND host=pThis->m_AlternateHost;
if (pos && pThis->m_AlternateView==hWnd && pThis->m_AlternateBand &&
pThis->m_AlternateBand->IsWindow() && GetParent(hWnd)==host &&
!(pos->flags&SWP_NOMOVE) && pos->x==0 && pos->y==0)
{
RECT client, band, current;
const RECT &previous=pThis->m_AlternateReserved;
int height=previous.top-pThis->m_AlternateOriginal.top;
if (height>0 && GetClientRect(host,&client) &&
GetWindowRect(pThis->m_AlternateBand->m_hWnd,&band) &&
GetWindowRect(hWnd,&current))
{
MapWindowPoints(NULL,host,(POINT*)&band,2);
MapWindowPoints(NULL,host,(POINT*)&current,2);
// Intercept only the verified full-width layout. Keep the
// existing timer for initialization and other geometries.
if (band.left==0 && band.top==0 && band.right==client.right &&
band.bottom==height && previous.right==client.right)
{
if (pos->flags&SWP_NOSIZE)
{
if (EqualRect(&current,&previous))
pos->y=height;
}
else if (pos->cx==client.right && pos->cy>=100 &&
pos->cy<=client.bottom && height<=pos->cy/2)
{
RECT original={0,0,pos->cx,pos->cy};
RECT reserved=original;
reserved.top+=height;
pos->y=height;
pos->cy-=height;
if (current.right-current.left!=pos->cx || current.bottom-current.top!=pos->cy)
pos->flags|=SWP_NOCOPYBITS;
pThis->m_AlternateOriginal=original;
pThis->m_AlternateReserved=reserved;
}
}
}
}
return result;
}
return DefSubclassProc(hWnd,uMsg,wParam,lParam);
}

STDMETHODIMP CExplorerBHO::OnDocumentComplete( IDispatch *pDisp, VARIANT *URL )
{
// this is called when the current folder changes. disable the Up button if this is the desktop folder
Expand Down Expand Up @@ -1542,6 +1787,9 @@ STDMETHODIMP CExplorerBHO::OnDocumentComplete( IDispatch *pDisp, VARIANT *URL )
m_pBrowser->QueryActiveShellView(&pView);
if (pView)
{
ProbeAlternateBand(pView);
if (m_AlternateBand && m_AlternateBand->IsWindow())
m_AlternateBand->UpdateToolbar();
CComQIPtr<IFolderView> pFolderView(pView);
if (pFolderView)
{
Expand Down Expand Up @@ -1666,6 +1914,18 @@ STDMETHODIMP CExplorerBHO::OnDocumentComplete( IDispatch *pDisp, VARIANT *URL )
return S_OK;
}

STDMETHODIMP CExplorerBHO::OnCommandStateChange( long Command, VARIANT_BOOL Enable )
{
if (m_AlternateBand && m_AlternateBand->IsWindow())
{
if (Command==CSC_NAVIGATEFORWARD)
m_AlternateBand->EnableButton(CBandWindow::ID_GOFORWARD,Enable?true:false);
if (Command==CSC_NAVIGATEBACK)
m_AlternateBand->EnableButton(CBandWindow::ID_GOBACK,Enable?true:false);
}
return S_OK;
}

STDMETHODIMP CExplorerBHO::OnQuit( void )
{
if (m_pWebBrowser && DispEvent1::m_dwEventCookie!=0xFEFEFEFE) // ATL's event cookie is 0xFEFEFEFE, when the sink is not advised
Expand Down
14 changes: 14 additions & 0 deletions Src/ClassicExplorer/ExplorerBHO.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <shdispid.h>


class CBandWindow;

// CExplorerBHO

class ATL_NO_VTABLE CExplorerBHO :
Expand Down Expand Up @@ -57,6 +59,7 @@ class ATL_NO_VTABLE CExplorerBHO :

BEGIN_SINK_MAP( CExplorerBHO )
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_COMMANDSTATECHANGE, OnCommandStateChange)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_ONQUIT, OnQuit)
SINK_ENTRY_EX(2, DIID_DShellFolderViewEvents, DISPID_SELECTIONCHANGED, OnSelChanged)
END_SINK_MAP()
Expand Down Expand Up @@ -107,6 +110,7 @@ class ATL_NO_VTABLE CExplorerBHO :

// DWebBrowserEvents2
STDMETHOD(OnDocumentComplete)( IDispatch *pDisp, VARIANT *URL );
STDMETHOD(OnCommandStateChange)( long Command, VARIANT_BOOL Enable );
STDMETHOD(OnQuit)( void );

// DShellFolderViewEvents
Expand All @@ -130,6 +134,16 @@ class ATL_NO_VTABLE CExplorerBHO :
CComPtr<IInternetZoneManager> m_pZoneManager;
CComPtr<IInternetSecurityManager> m_pSecurityManager;
std::map<unsigned int,HICON> m_ZoneIconCache;
bool m_bProbedAlternateBand=false;
CBandWindow *m_AlternateBand=NULL;
HWND m_AlternateHost=NULL, m_AlternateView=NULL;
RECT m_AlternateOriginal={}, m_AlternateReserved={};
int m_AlternateTicks=0;
void ProbeAlternateBand( IShellView *pView );
bool LayoutAlternateBand( void );
void ClearAlternateBand( void );
static LRESULT CALLBACK AlternateHostProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData );
static LRESULT CALLBACK AlternateViewProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData );
bool m_bResetStatus;
bool m_bForceRefresh;
bool m_bNoBreadcrumbs;
Expand Down