///////////////////////////////////////////////////////////////
//
// ExplorerListDlg.cpp
//
// Created: 17/07/2003
// Copyright (c) 2003 Ralph Hare (ralph.hare@ysgyfarnog.co.uk)
// All rights reserved.
//
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
//
///////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ExplorerListDlg.h"
#include "WindowHelper.h"
#include "OSUtils.h"
#include <algorithm>
namespace
{
LPCTSTR CLASS_IE_FRAME = _T( "IEFrame" );
BOOL __stdcall EnumWindowsProc( HWND hWnd, LPARAM lParam )
{
if( lParam == NULL )
{
return FALSE;
}
if( WindowHelper::GetClassName( hWnd ) == CLASS_IE_FRAME )
{
#ifdef _DEBUG
ATLASSERT( ::IsBadReadPtr(
reinterpret_cast< void * >( lParam ),
sizeof( WindowList )
) == FALSE );
ATLASSERT( ::IsBadWritePtr(
reinterpret_cast< void * >( lParam ),
sizeof( WindowList )
) == FALSE );
#endif
reinterpret_cast< WindowList * >( lParam )->push_back( hWnd );
}
return TRUE;
}
class InsertWindows : public std::unary_function< HWND, void >
{
public:
InsertWindows( const CListViewCtrl &listCtrl ) :
m_listCtrl( listCtrl )
{
}
void operator()( HWND hWnd )
{
int len = ::GetWindowTextLength( hWnd ) + 1;
std::vector< TCHAR > buffer( len );
::GetWindowText( hWnd, &buffer[ 0 ], len );
int item = m_listCtrl.InsertItem( 999999, &buffer[ 0 ] );
m_listCtrl.SetItemData( item, reinterpret_cast< DWORD_PTR >( hWnd ) );
}
private:
CListViewCtrl m_listCtrl;
};
}
BOOL ExplorerListDlg::Invoke( HWND hWndParent )
{
//
// modeless dialog as we don't get mouse button up messages
// if the cursor goes out of the client area with a modal dialog
// (even with SetCapture) ????
//
static ExplorerListDlg instance;
if( instance.Create( hWndParent, NULL ) == NULL )
{
return FALSE;
}
instance.ShowWindow( SW_SHOW );
return TRUE;
}
HWND ExplorerListDlg::Create( HWND hWndParent, LPARAM dwInitParam )
{
m_windowList.clear();
::EnumDesktopWindows(
NULL,
EnumWindowsProc,
reinterpret_cast< LPARAM >( &m_windowList )
);
if( m_windowList.size() < 2 )
{
return NULL;
}
HWND hWnd = CDialogImpl< ExplorerListDlg >::Create( hWndParent, dwInitParam );
if( ::IsWindow( hWnd ) == FALSE )
{
return hWnd;
}
//
// bring the window to the top
//
::SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
//
// TODO: add code to size window according to width of text
// GetFont()
// GetDC() etc.
//
m_listCtrl = GetDlgItem( IDC_LIST_EXPLORERS );
m_listCtrl.InsertColumn( 0, _T( "" ), LVCFMT_LEFT, 350, -1 );
std::for_each(
m_windowList.begin(),
m_windowList.end(),
InsertWindows( m_listCtrl )
);
HWND hWndFrame = ::GetAncestor( GetParent(), GA_ROOT );
int selected = 0;
for( int idx = m_listCtrl.GetItemCount(); --idx != -1; )
{
if( reinterpret_cast< HWND >( m_listCtrl.GetItemData( idx ) ) == hWndFrame )
{
selected = idx;
break;
}
}
m_listCtrl.SelectItem( selected );
CenterWindow();
SetCapture();
m_mouseTracker.Advise( m_listCtrl, this );
return hWnd;
}
bool ExplorerListDlg::OnMouseWheel( HWND hWnd, WORD, WORD delta, const POINT &pt )
{
int item = m_listCtrl.GetSelectedIndex();
if( short( delta ) < 0 )
{
if( ++item == m_listCtrl.GetItemCount() )
{
item = 0;
}
}
else
{
if( --item == -1 )
{
item += m_listCtrl.GetItemCount();
}
}
m_listCtrl.SelectItem( item );
return true;
}
LRESULT ExplorerListDlg::OnRightButtonUp( UINT, WPARAM, LPARAM, BOOL & )
{
ShowWindow( SW_HIDE );
int item = m_listCtrl.GetSelectedIndex();
HWND hWnd = reinterpret_cast< HWND >( m_listCtrl.GetItemData( item ) );
if( ::IsIconic( hWnd ) )
{
WINDOWPLACEMENT pl;
pl.length = sizeof( WINDOWPLACEMENT );
::GetWindowPlacement( hWnd, &pl );
if( ( pl.flags & WPF_RESTORETOMAXIMIZED ) == WPF_RESTORETOMAXIMIZED )
{
::ShowWindow( hWnd, SW_MAXIMIZE );
}
else
{
::ShowWindow( hWnd, SW_RESTORE );
}
}
::SetFocus( hWnd );
::SetForegroundWindow( hWnd );
m_mouseTracker.Unadvise();
DestroyWindow();
return 0;
}