Skip to content

Commit

Permalink
Set up modernized folder selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ariccio committed Feb 28, 2015
1 parent c25a256 commit 30a31f0
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 21 deletions.
264 changes: 264 additions & 0 deletions WinDirStat/windirstat/COM_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
#include "stdafx.h"
#include "ScopeGuard.h"

//several are from the "Show Shell Common File Dialog" sample



// Controls
// It is OK for CONTROL_RADIOBUTTON2 to have the same ID as CONTROL_RADIOBUTTONLIST,
// because it is a child control under CONTROL_RADIOBUTTONLIST.
//#define CONTROL_GROUP 2000
#define CONTROL_RADIOBUTTONLIST 2
#define CONTROL_RADIOBUTTON1 1
#define CONTROL_RADIOBUTTON2 2

namespace {
const COMDLG_FILTERSPEC c_rgSaveTypes[ ] =
{
{ L"All folders", L"*" }
};

}




//
// CLASS: CFileDialogEventHandler
//
// PURPOSE:
// File Dialog Event Handler that responds to Events in Added Controls. The
// events handler provided by the calling process can implement
// IFileDialogControlEvents in addition to IFileDialogEvents.
// IFileDialogControlEvents enables the calling process to react to these events:
// 1) PushButton clicked.
// 2) CheckButton state changed.
// 3) Item selected from a menu, ComboBox, or RadioButton list.
// 4) Control activating. This is sent when a menu is about to display a
// drop-down list, in case the calling process wants to change the items in
// the list.
//
class CFileDialogEventHandler : public IFileDialogEvents, public IFileDialogControlEvents {
public:

//
// IUnknown methods
//

IFACEMETHODIMP QueryInterface( _In_ REFIID riid, _COM_Outptr_ void** ppv ) {
static const QITAB qit[ ] =
{
QITABENT( CFileDialogEventHandler, IFileDialogEvents ),
QITABENT( CFileDialogEventHandler, IFileDialogControlEvents ),
{ 0 }
#pragma warning( suppress: 4365)
};
return QISearch( this, qit, riid, ppv );
}

IFACEMETHODIMP_( ULONG ) AddRef( ) {
#pragma warning( suppress: 4365 )
return InterlockedIncrement( &m_cRef );
}

IFACEMETHODIMP_( ULONG ) Release( ) {
const auto cRef = InterlockedDecrement( &m_cRef );
if ( !cRef ) {
delete this;
}
#pragma warning( suppress: 4365 )
return cRef;
}

//
// IFileDialogEvents methods
//

IFACEMETHODIMP OnFileOk( IFileDialog* ) {
return S_OK;
}
IFACEMETHODIMP OnFolderChange( IFileDialog* ) {
return S_OK;
}
IFACEMETHODIMP OnFolderChanging( IFileDialog*, IShellItem* ) {
return S_OK;
}
IFACEMETHODIMP OnHelp( IFileDialog* ) {
return S_OK;
}
IFACEMETHODIMP OnSelectionChange( IFileDialog* ) {
return S_OK;
}
IFACEMETHODIMP OnTypeChange( IFileDialog* ) {
return S_OK;
}
IFACEMETHODIMP OnShareViolation( IFileDialog*, IShellItem*, FDE_SHAREVIOLATION_RESPONSE* ) {
return S_OK;
}
IFACEMETHODIMP OnOverwrite( IFileDialog*, IShellItem*, FDE_OVERWRITE_RESPONSE* ) {
return S_OK;
}

//
// IFileDialogControlEvents methods
//

IFACEMETHODIMP OnItemSelected( IFileDialogCustomize* pfdc, DWORD dwIDCtl, DWORD dwIDItem ) {
IFileDialog *pfd = NULL;
const HRESULT pfdc_query_interface_result = pfdc->QueryInterface( &pfd );
if ( !SUCCEEDED( pfdc_query_interface_result ) ) {
return pfdc_query_interface_result;
}
auto guard = WDS_SCOPEGUARD_INSTANCE( [&] { pfd->Release( ); } );
if ( dwIDCtl == CONTROL_RADIOBUTTONLIST ) {
switch ( dwIDItem ) {
case CONTROL_RADIOBUTTON1:
return pfd->SetTitle( L"Windows Vista" );
//break;

case CONTROL_RADIOBUTTON2:
return pfd->SetTitle( L"Windows 7" );
//break;
}
}
//pfd->Release( );
//if ( SUCCEEDED( hr ) ) {
// }
return pfdc_query_interface_result;
}

IFACEMETHODIMP OnButtonClicked( IFileDialogCustomize*, DWORD ) {
return S_OK;
}
IFACEMETHODIMP OnControlActivating( IFileDialogCustomize*, DWORD ) {
return S_OK;
}
IFACEMETHODIMP OnCheckButtonToggled( IFileDialogCustomize*, DWORD, BOOL ) {
return S_OK;
}

CFileDialogEventHandler( ) : m_cRef { 1 } { }

protected:

~CFileDialogEventHandler( ) { }
long m_cRef;
#pragma warning( suppress: 4265 )
};




//https://code.msdn.microsoft.com/CppShellCommonFileDialog-17b20409/sourcecode?fileId=52757&pathId=1435971692
const HRESULT CFileDialogEventHandler_CreateInstance( _In_ REFIID riid, _COM_Outptr_ void** ppv ) {
*ppv = NULL;
auto pFileDialogEventHandler = new CFileDialogEventHandler( );
const HRESULT file_dialog_event_handler_query_interface_result = pFileDialogEventHandler->QueryInterface( riid, ppv );
pFileDialogEventHandler->Release( );
return file_dialog_event_handler_query_interface_result;
}



std::wstring OnOpenAFolder( HWND /*hWnd*/ ) {
IFileDialog* file_dialog = nullptr;
const HRESULT create_file_dialog_result = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS( &file_dialog ) );
if ( !SUCCEEDED( create_file_dialog_result ) ) {
TRACE( _T( "create_file_dialog_result-> FAILED!\r\n" ) );
return L"";
}
auto file_dialog_guard = WDS_SCOPEGUARD_INSTANCE( [ &] { file_dialog->Release( ); } );

IFileDialogEvents* file_dialog_event = NULL;
const HRESULT dialog_event_handler_create_instance_result = CFileDialogEventHandler_CreateInstance( IID_PPV_ARGS( &file_dialog_event ) );
if ( !SUCCEEDED( dialog_event_handler_create_instance_result ) ) {
TRACE( _T( "CFileDialogEventHandler_CreateInstance-> FAILED!\r\n" ) );
return L"";
}

auto file_dialog_event_guard = WDS_SCOPEGUARD_INSTANCE( [ &] { file_dialog_event->Release( ); } );

DWORD dwCookie_temp = 0;
const HRESULT file_dialog_advise_cookie_result = file_dialog->Advise( file_dialog_event, &dwCookie_temp );
if ( !SUCCEEDED( file_dialog_advise_cookie_result ) ) {
TRACE( _T( "file_dialog->Advise FAILED!\r\n" ) );
return L"";
}

const DWORD dwCookie = dwCookie_temp;

auto file_dialog_advise_cookie_guard = WDS_SCOPEGUARD_INSTANCE( [ &] { file_dialog->Unadvise( dwCookie ); } );

DWORD dialog_options_flags = 0;

const HRESULT get_file_dialog_options_result = file_dialog->GetOptions( &dialog_options_flags );
if ( !SUCCEEDED( get_file_dialog_options_result ) ) {
TRACE( _T( "file_dialog->GetOptions FAILED!\r\n" ) );
return L"";
}

const HRESULT set_file_dialog_options_result = file_dialog->SetOptions( dialog_options_flags bitor FOS_FORCEFILESYSTEM bitor FOS_PICKFOLDERS bitor FOS_FORCESHOWHIDDEN );
if ( !SUCCEEDED( set_file_dialog_options_result ) ) {
TRACE( _T( "file_dialog->SetOptions FAILED!\r\n" ) );
return L"";
}

//const HRESULT set_file_dialog_types_result = file_dialog->SetFileTypes( ARRAYSIZE( c_rgSaveTypes ), c_rgSaveTypes );
//if ( !SUCCEEDED( set_file_dialog_types_result ) ) {
// TRACE( _T( "file_dialog->SetFileTypes FAILED!\r\n" ) );
// return;
//}

//const HRESULT set_file_type_index_result = file_dialog->SetFileTypeIndex( 1 );
//if ( !SUCCEEDED( set_file_type_index_result ) ) {
// TRACE( _T( "file_dialog->SetFileTypeIndex FAILED!\r\n" ) );
// return;
// }

//const HRESULT set_file_type_default_extension = file_dialog->SetDefaultExtension( L"d" );
//if ( !SUCCEEDED( set_file_type_default_extension ) ) {
// TRACE( _T( "file_dialog->SetDefaultExtension FAILED!\r\n" ) );
// return;
// }

const HRESULT file_dialog_show_dialog_result = file_dialog->Show( NULL );
if ( !SUCCEEDED( file_dialog_show_dialog_result ) ) {
TRACE( _T( "file_dialog->Show FAILED!\r\n" ) );
return L"";
}

IShellItem* shell_item_result = nullptr;
const HRESULT file_dialog_get_result_result = file_dialog->GetResult( &shell_item_result );
if ( !SUCCEEDED( file_dialog_get_result_result ) ) {
TRACE( _T( "file_dialog->GetResult FAILED!\r\n" ) );
return L"";
}

auto psi_result_guard = WDS_SCOPEGUARD_INSTANCE( [ &] { shell_item_result->Release( ); } );

PWSTR file_path_temp = nullptr;
const HRESULT shell_item_get_display_name_result = shell_item_result->GetDisplayName( SIGDN_FILESYSPATH, &file_path_temp );
if ( !SUCCEEDED( shell_item_get_display_name_result ) ) {
TRACE( _T( "shell_item_result->GetDisplayName FAILED!\r\n" ) );
return L"";
}

PWSTR const file_path = file_path_temp;

auto file_path_guard = WDS_SCOPEGUARD_INSTANCE( [ &] { CoTaskMemFree( file_path ); } );
std::wstring path_string( file_path );
return path_string;

//const HRESULT task_dialog_result = TaskDialog( NULL, NULL, L"CommonFileDialogApp", file_path, NULL, TDCBF_OK_BUTTON, TD_INFORMATION_ICON, NULL );
//if ( !SUCCEEDED( task_dialog_result ) ) {
// TRACE( _T( "TaskDialog FAILED!\r\n" ) );
// return;
// }


//TRACE( _T( "OpenAFolder is returning SUCESSFULLY!\r\n" ) );
}



13 changes: 6 additions & 7 deletions WinDirStat/windirstat/SelectDrivesDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ UINT CSelectDrivesDlg::_serial;


#pragma warning(suppress:4355)
CSelectDrivesDlg::CSelectDrivesDlg( CWnd* pParent /*=NULL*/ ) : CDialog( CSelectDrivesDlg::IDD, pParent ), m_radio( RADIO_ALLLOCALDRIVES ), m_layout( static_cast<CWnd*>( this ), global_strings::select_drives_dialog_layout ), m_name_pool( volume_name_pool_size ) {
CSelectDrivesDlg::CSelectDrivesDlg( CWnd* pParent /*=NULL*/ ) : CDialog( CSelectDrivesDlg::IDD, pParent ), m_radio( RADIO_SOMEDRIVES ), m_layout( static_cast<CWnd*>( this ), global_strings::select_drives_dialog_layout ), m_name_pool( volume_name_pool_size ) {
_serial++;
//InitializeCriticalSection_wrapper( _csRunningThreads );
InitializeCriticalSection_wrapper( m_running_threads_CRITICAL_SECTION );
Expand Down Expand Up @@ -466,7 +466,7 @@ BOOL CSelectDrivesDlg::OnInitDialog( ) {

switch ( m_radio )
{
case RADIO_ALLLOCALDRIVES:
//case RADIO_ALLLOCALDRIVES:
case RADIO_AFOLDER:
m_okButton.SetFocus( );
m_wtl_ok_button.SetFocus( );
Expand Down Expand Up @@ -515,8 +515,7 @@ void CSelectDrivesDlg::handle_RADIO_other( ) {
//`/analyze` is confused.
return;
}
if ( ( m_radio == RADIO_ALLLOCALDRIVES ) &&
( !IsSUBSTedDrive( item->m_path.c_str( ) ) ) ||
if ( ( !IsSUBSTedDrive( item->m_path.c_str( ) ) ) ||
( m_radio == RADIO_SOMEDRIVES ) &&
( ( LVIS_SELECTED == m_list.GetItemState( i, LVIS_SELECTED ) ) )
) {
Expand Down Expand Up @@ -563,9 +562,9 @@ _Pre_defensive_ void CSelectDrivesDlg::UpdateButtons( ) {
BOOL enableOk = FALSE;
switch ( m_radio )
{
case RADIO_ALLLOCALDRIVES:
enableOk = TRUE;
break;
//case RADIO_ALLLOCALDRIVES:
//enableOk = TRUE;
//break;
case RADIO_SOMEDRIVES:
enableOk = ( ( m_list.GetSelectedCount( ) > 0 ) ? TRUE : FALSE );
break;
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/TreeListControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "TreeListControl.h"
#include "item.h"
#include "typeview.h"
#include "SelectDrivesDlg.h"
//#include "SelectDrivesDlg.h"


#include "globalhelpers.h"
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/datastructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ struct DIRINFO {

// The dialog has these three radio buttons.
enum RADIO : INT {
RADIO_ALLLOCALDRIVES,
//RADIO_ALLLOCALDRIVES,
RADIO_SOMEDRIVES,
RADIO_AFOLDER
};
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/globalhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ std::wstring EncodeSelection( _In_ const RADIO radio, _In_ const std::wstring fo
std::wstring ret;
TRACE( _T( "Encoding selection %s\r\n" ), folder.c_str( ) );
switch ( radio ) {
case RADIO_ALLLOCALDRIVES:
//case RADIO_ALLLOCALDRIVES:
case RADIO_SOMEDRIVES:
{
for ( size_t i = 0; i < drives.size( ); i++ ) {
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ void CItemBranch::stdRecurseCollectExtensionData_FILE( _Inout_ std::unordered_ma
static_assert( std::is_same< std::decay<decltype(*m_name)>::type, wchar_t>::value, "Bad division below!" );
const auto alt_length = ( ( std::ptrdiff_t( m_name + m_name_length ) - std::ptrdiff_t( resultPtrStr ) ) / sizeof( wchar_t ) );
ASSERT( wcslen( resultPtrStr ) == alt_length );
TRACE( _T( "Calculated length: %lld, actual length: %llu\r\n" ), LONGLONG( alt_length ), ULONGLONG( wcslen( resultPtrStr ) ) );
//TRACE( _T( "Calculated length: %lld, actual length: %llu\r\n" ), LONGLONG( alt_length ), ULONGLONG( wcslen( resultPtrStr ) ) );
auto& value = extensionMap[ resultPtrStr ];
++( value.files );
value.bytes += m_size;
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static_assert( _WIN32_WINNT >= 0x0600, "" );
//#define DUMP_MEMUSAGE
//#define GRAPH_LAYOUT_DEBUG
//#define EXTENSION_LIST_DEBUG
#define COLOR_DEBUGGING
//#define COLOR_DEBUGGING
//#define SIMD_ACCESS_DEBUGGING
//#define WDS_STRING_ALLOC_DEBUGGING
#define DISPLAY_FINAL_CITEMBRANCH_SIZE
Expand Down

0 comments on commit 30a31f0

Please sign in to comment.