2006-06-15 04:19:30 +00:00
# include <shlobj.h>
# include <stdio.h>
# include "VFSManagerWnd.h"
2008-04-12 16:02:19 +00:00
# include "layout/LayoutEngine.h"
2008-04-05 03:38:53 +00:00
# include "layout/LayoutStretch.h"
# include "layout/HorizontalLayout.h"
2006-06-15 04:19:30 +00:00
# include "win32/LayoutWindow.h"
# include "win32/Static.h"
# include "PtrMacro.h"
2008-08-24 21:28:42 +00:00
# include "../AppConfig.h"
2006-06-15 04:19:30 +00:00
# include "CdromSelectionWnd.h"
2007-02-20 21:16:34 +00:00
# include "string_cast.h"
2006-06-15 04:19:30 +00:00
2007-02-20 21:16:34 +00:00
# define CLSNAME _T("VFSManagerWnd")
2006-06-15 04:19:30 +00:00
# define WNDSTYLE (WS_CAPTION | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU)
# define WNDSTYLEEX (WS_EX_DLGMODALFRAME)
# define CDROM0PATH "ps2.cdrom0.path"
using namespace Framework ;
2007-02-20 21:16:34 +00:00
using namespace std ;
2006-06-15 04:19:30 +00:00
CVFSManagerWnd : : CVFSManagerWnd ( HWND hParent ) :
CModalWindow ( hParent )
{
2008-04-12 16:02:19 +00:00
RECT rc ;
if ( ! DoesWindowClassExist ( CLSNAME ) )
{
WNDCLASSEX w ;
memset ( & w , 0 , sizeof ( WNDCLASSEX ) ) ;
w . cbSize = sizeof ( WNDCLASSEX ) ;
w . lpfnWndProc = CWindow : : WndProc ;
w . lpszClassName = CLSNAME ;
w . hbrBackground = ( HBRUSH ) GetSysColorBrush ( COLOR_BTNFACE ) ;
w . hInstance = GetModuleHandle ( NULL ) ;
w . hCursor = LoadCursor ( NULL , IDC_ARROW ) ;
w . style = CS_HREDRAW | CS_VREDRAW ;
RegisterClassEx ( & w ) ;
}
SetRect ( & rc , 0 , 0 , 400 , 250 ) ;
Create ( WNDSTYLEEX , CLSNAME , _T ( " Virtual File System Manager " ) , WNDSTYLE , & rc , hParent , NULL ) ;
SetClassPtr ( ) ;
SetRect ( & rc , 0 , 0 , 1 , 1 ) ;
m_pList = new Win32 : : CListView ( m_hWnd , & rc , LVS_REPORT | LVS_SORTASCENDING ) ;
m_pList - > SetExtendedListViewStyle ( m_pList - > GetExtendedListViewStyle ( ) | LVS_EX_FULLROWSELECT ) ;
m_pOk = new Win32 : : CButton ( _T ( " OK " ) , m_hWnd , & rc ) ;
m_pCancel = new Win32 : : CButton ( _T ( " Cancel " ) , m_hWnd , & rc ) ;
m_pLayout =
VerticalLayoutContainer (
LayoutExpression ( Win32 : : CLayoutWindow : : CreateCustomBehavior ( 1 , 1 , 1 , 1 , m_pList ) ) +
LayoutExpression ( Win32 : : CLayoutWindow : : CreateTextBoxBehavior ( 100 , 40 , new Win32 : : CStatic ( m_hWnd , _T ( " Warning: Changing file system bindings while a program is running might be dangerous. Changes to 'cdrom0' bindings will take effect next time you load an executable. " ) , SS_LEFT ) ) ) +
HorizontalLayoutContainer (
LayoutExpression ( CLayoutStretch : : Create ( ) ) +
LayoutExpression ( Win32 : : CLayoutWindow : : CreateButtonBehavior ( 100 , 23 , m_pOk ) ) +
LayoutExpression ( Win32 : : CLayoutWindow : : CreateButtonBehavior ( 100 , 23 , m_pCancel ) )
)
) ;
RefreshLayout ( ) ;
m_devices [ 0 ] = new CDirectoryDevice ( " mc0 " , " ps2.mc0.directory " ) ;
m_devices [ 1 ] = new CDirectoryDevice ( " mc1 " , " ps2.mc1.directory " ) ;
m_devices [ 2 ] = new CDirectoryDevice ( " host " , " ps2.host.directory " ) ;
m_devices [ 3 ] = new CCdrom0Device ( ) ;
CreateListColumns ( ) ;
UpdateList ( ) ;
2006-06-15 04:19:30 +00:00
}
CVFSManagerWnd : : ~ CVFSManagerWnd ( )
{
2008-04-12 16:02:19 +00:00
for ( DeviceList : : const_iterator deviceIterator ( m_devices . begin ( ) ) ;
m_devices . end ( ) ! = deviceIterator ; deviceIterator + + )
{
delete deviceIterator - > second ;
}
2006-06-15 04:19:30 +00:00
}
long CVFSManagerWnd : : OnCommand ( unsigned short nID , unsigned short nCmd , HWND hSender )
{
if ( hSender = = m_pOk - > m_hWnd )
{
Save ( ) ;
Destroy ( ) ;
return TRUE ;
}
else if ( hSender = = m_pCancel - > m_hWnd )
{
Destroy ( ) ;
return TRUE ;
}
return FALSE ;
}
long CVFSManagerWnd : : OnNotify ( WPARAM wParam , NMHDR * pHDR )
{
2008-04-12 16:02:19 +00:00
if ( pHDR - > hwndFrom = = m_pList - > m_hWnd )
{
switch ( pHDR - > code )
{
case NM_DBLCLK :
int nSel = m_pList - > GetSelection ( ) ;
if ( nSel ! = - 1 )
{
DeviceList : : const_iterator deviceIterator ( m_devices . find ( m_pList - > GetItemData ( nSel ) ) ) ;
if ( deviceIterator ! = m_devices . end ( ) )
{
CDevice * pDevice ( deviceIterator - > second ) ;
if ( pDevice - > RequestModification ( m_hWnd ) )
{
UpdateList ( ) ;
}
}
}
break ;
}
}
return FALSE ;
2006-06-15 04:19:30 +00:00
}
void CVFSManagerWnd : : RefreshLayout ( )
{
RECT rc ;
GetClientRect ( & rc ) ;
SetRect ( & rc , rc . left + 10 , rc . top + 10 , rc . right - 10 , rc . bottom - 10 ) ;
m_pLayout - > SetRect ( rc . left , rc . top , rc . right , rc . bottom ) ;
m_pLayout - > RefreshGeometry ( ) ;
Redraw ( ) ;
}
void CVFSManagerWnd : : CreateListColumns ( )
{
LVCOLUMN col ;
RECT rc ;
m_pList - > GetClientRect ( & rc ) ;
memset ( & col , 0 , sizeof ( LVCOLUMN ) ) ;
2007-02-20 21:16:34 +00:00
col . pszText = _T ( " Device " ) ;
2006-06-15 04:19:30 +00:00
col . mask = LVCF_TEXT | LVCF_WIDTH ;
col . cx = rc . right / 4 ;
m_pList - > InsertColumn ( 0 , & col ) ;
memset ( & col , 0 , sizeof ( LVCOLUMN ) ) ;
2007-02-20 21:16:34 +00:00
col . pszText = _T ( " Binding Type " ) ;
2006-06-15 04:19:30 +00:00
col . mask = LVCF_TEXT | LVCF_WIDTH ;
col . cx = rc . right / 4 ;
m_pList - > InsertColumn ( 1 , & col ) ;
memset ( & col , 0 , sizeof ( LVCOLUMN ) ) ;
2007-02-20 21:16:34 +00:00
col . pszText = _T ( " Binding Value " ) ;
2006-06-15 04:19:30 +00:00
col . mask = LVCF_TEXT | LVCF_WIDTH ;
col . cx = rc . right / 2 ;
m_pList - > InsertColumn ( 2 , & col ) ;
}
void CVFSManagerWnd : : UpdateList ( )
{
2008-04-12 16:02:19 +00:00
for ( DeviceList : : const_iterator deviceIterator ( m_devices . begin ( ) ) ;
m_devices . end ( ) ! = deviceIterator ; deviceIterator + + )
{
CDevice * pDevice = deviceIterator - > second ;
unsigned int key = deviceIterator - > first ;
unsigned int index = m_pList - > FindItemData ( key ) ;
if ( index = = - 1 )
{
2007-02-20 21:16:34 +00:00
tstring sDeviceName ( string_cast < tstring > ( pDevice - > GetDeviceName ( ) ) ) ;
2008-04-12 16:02:19 +00:00
LVITEM itm ;
memset ( & itm , 0 , sizeof ( LVITEM ) ) ;
itm . mask = LVIF_TEXT | LVIF_PARAM ;
itm . pszText = const_cast < TCHAR * > ( sDeviceName . c_str ( ) ) ;
itm . lParam = key ;
index = m_pList - > InsertItem ( & itm ) ;
}
m_pList - > SetItemText ( index , 1 , string_cast < tstring > ( pDevice - > GetBindingType ( ) ) . c_str ( ) ) ;
m_pList - > SetItemText ( index , 2 , string_cast < tstring > ( pDevice - > GetBinding ( ) ) . c_str ( ) ) ;
}
2006-06-15 04:19:30 +00:00
}
void CVFSManagerWnd : : Save ( )
{
2008-04-12 16:02:19 +00:00
for ( DeviceList : : const_iterator deviceIterator ( m_devices . begin ( ) ) ;
m_devices . end ( ) ! = deviceIterator ; deviceIterator + + )
{
CDevice * pDevice = deviceIterator - > second ;
pDevice - > Save ( ) ;
}
2006-06-15 04:19:30 +00:00
}
///////////////////////////////////////////
//CDevice Implementation
///////////////////////////////////////////
CVFSManagerWnd : : CDevice : : ~ CDevice ( )
{
}
///////////////////////////////////////////
//CDirectoryDevice Implementation
///////////////////////////////////////////
CVFSManagerWnd : : CDirectoryDevice : : CDirectoryDevice ( const char * sName , const char * sPreference )
{
2008-04-12 16:02:19 +00:00
m_sName = sName ;
m_sPreference = sPreference ;
2008-08-24 21:28:42 +00:00
m_sValue = CAppConfig : : GetInstance ( ) . GetPreferenceString ( m_sPreference ) ;
2006-06-15 04:19:30 +00:00
}
CVFSManagerWnd : : CDirectoryDevice : : ~ CDirectoryDevice ( )
{
2008-04-12 16:02:19 +00:00
2006-06-15 04:19:30 +00:00
}
const char * CVFSManagerWnd : : CDirectoryDevice : : GetDeviceName ( )
{
2008-04-12 16:02:19 +00:00
return m_sName ;
2006-06-15 04:19:30 +00:00
}
const char * CVFSManagerWnd : : CDirectoryDevice : : GetBindingType ( )
{
2008-04-12 16:02:19 +00:00
return " Directory " ;
2006-06-15 04:19:30 +00:00
}
const char * CVFSManagerWnd : : CDirectoryDevice : : GetBinding ( )
{
2008-04-12 16:02:19 +00:00
return m_sValue . c_str ( ) ;
2006-06-15 04:19:30 +00:00
}
bool CVFSManagerWnd : : CDirectoryDevice : : RequestModification ( HWND hParent )
{
BROWSEINFO bi ;
LPITEMIDLIST item ;
2007-02-20 21:16:34 +00:00
TCHAR sPath [ MAX_PATH ] ;
2006-06-15 04:19:30 +00:00
memset ( & bi , 0 , sizeof ( BROWSEINFO ) ) ;
bi . hwndOwner = hParent ;
2007-02-20 21:16:34 +00:00
bi . lpszTitle = _T ( " Select new folder for device " ) ;
2006-06-15 04:19:30 +00:00
bi . ulFlags = BIF_RETURNONLYFSDIRS ;
bi . lpfn = BrowseCallback ;
bi . lParam = ( LPARAM ) this ;
item = SHBrowseForFolder ( & bi ) ;
if ( item = = NULL )
{
return false ;
}
if ( SHGetPathFromIDList ( item , sPath ) = = 0 )
{
2007-02-20 21:16:34 +00:00
MessageBox ( hParent , _T ( " Invalid directory. " ) , NULL , 16 ) ;
2006-06-15 04:19:30 +00:00
CoTaskMemFree ( item ) ;
return false ;
}
CoTaskMemFree ( item ) ;
2007-02-20 21:16:34 +00:00
m_sValue = string_cast < string > ( sPath ) . c_str ( ) ;
2006-06-15 04:19:30 +00:00
return true ;
}
void CVFSManagerWnd : : CDirectoryDevice : : Save ( )
{
2008-08-24 21:28:42 +00:00
CAppConfig : : GetInstance ( ) . SetPreferenceString ( m_sPreference , m_sValue . c_str ( ) ) ;
2006-06-15 04:19:30 +00:00
}
int CVFSManagerWnd : : CDirectoryDevice : : BrowseCallback ( HWND hFrom , unsigned int nMsg , LPARAM lParam , LPARAM pData )
{
2008-04-12 16:02:19 +00:00
CDirectoryDevice * pDevice = reinterpret_cast < CDirectoryDevice * > ( pData ) ;
2006-06-15 04:19:30 +00:00
switch ( nMsg )
{
case BFFM_INITIALIZED :
2008-04-12 16:02:19 +00:00
tstring sPath ( string_cast < tstring > ( pDevice - > m_sValue . c_str ( ) ) ) ;
SendMessage ( hFrom , BFFM_SETSELECTION , TRUE , reinterpret_cast < LPARAM > ( sPath . c_str ( ) ) ) ;
2006-06-15 04:19:30 +00:00
break ;
}
return 0 ;
}
///////////////////////////////////////////
//CCdrom0Device Implementation
///////////////////////////////////////////
CVFSManagerWnd : : CCdrom0Device : : CCdrom0Device ( )
{
const char * sPath ;
char sDevicePath [ 32 ] ;
2008-08-24 21:28:42 +00:00
sPath = CAppConfig : : GetInstance ( ) . GetPreferenceString ( CDROM0PATH ) ;
2006-06-15 04:19:30 +00:00
//Detect the binding type from the path format
if ( ! strcmp ( sPath , " " ) )
{
m_nBindingType = CCdromSelectionWnd : : BINDING_IMAGE ;
m_sImagePath = " " ;
}
else if ( strlen ( sPath ) = = 6 & & ! strncmp ( sPath , " \\ \\ . \\ " , 4 ) )
{
sprintf ( sDevicePath , " %c: \\ " , toupper ( sPath [ 4 ] ) ) ;
m_nBindingType = CCdromSelectionWnd : : BINDING_PHYSICAL ;
m_sDevicePath = sDevicePath ;
}
else
{
m_nBindingType = CCdromSelectionWnd : : BINDING_IMAGE ;
m_sImagePath = sPath ;
}
}
CVFSManagerWnd : : CCdrom0Device : : ~ CCdrom0Device ( )
{
}
const char * CVFSManagerWnd : : CCdrom0Device : : GetDeviceName ( )
{
return " cdrom0 " ;
}
const char * CVFSManagerWnd : : CCdrom0Device : : GetBindingType ( )
{
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_PHYSICAL )
{
return " Physical Device " ;
}
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_IMAGE )
{
return " Disk Image " ;
}
return " " ;
}
const char * CVFSManagerWnd : : CCdrom0Device : : GetBinding ( )
{
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_IMAGE )
{
2008-04-12 16:02:19 +00:00
if ( m_sImagePath . length ( ) = = 0 )
2006-06-15 04:19:30 +00:00
{
return " (None) " ;
}
else
{
2008-04-12 16:02:19 +00:00
return m_sImagePath . c_str ( ) ;
2006-06-15 04:19:30 +00:00
}
}
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_PHYSICAL )
{
2008-04-12 16:02:19 +00:00
return m_sDevicePath . c_str ( ) ;
2006-06-15 04:19:30 +00:00
}
return " " ;
}
bool CVFSManagerWnd : : CCdrom0Device : : RequestModification ( HWND hParent )
{
CCdromSelectionWnd : : CDROMBINDING Binding ;
char sDevicePath [ 32 ] ;
Binding . nType = ( CCdromSelectionWnd : : BINDINGTYPE ) m_nBindingType ;
2008-04-12 16:02:19 +00:00
Binding . sImagePath = m_sImagePath . c_str ( ) ;
Binding . nPhysicalDevice = m_sDevicePath [ 0 ] - ' A ' ;
2006-06-15 04:19:30 +00:00
2007-02-20 21:16:34 +00:00
CCdromSelectionWnd SelectionWnd ( hParent , _T ( " Modify cdrom0 Binding " ) , & Binding ) ;
2006-06-15 04:19:30 +00:00
SelectionWnd . DoModal ( ) ;
if ( ! SelectionWnd . WasConfirmed ( ) ) return false ;
SelectionWnd . GetBindingInfo ( & Binding ) ;
sprintf ( sDevicePath , " %c: \\ " , Binding . nPhysicalDevice + ' A ' ) ;
m_nBindingType = Binding . nType ;
m_sImagePath = Binding . sImagePath ;
m_sDevicePath = sDevicePath ;
return true ;
}
void CVFSManagerWnd : : CCdrom0Device : : Save ( )
{
2008-04-12 16:02:19 +00:00
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_IMAGE )
{
2008-08-24 21:28:42 +00:00
CAppConfig : : GetInstance ( ) . SetPreferenceString ( CDROM0PATH , m_sImagePath . c_str ( ) ) ;
2008-04-12 16:02:19 +00:00
}
if ( m_nBindingType = = CCdromSelectionWnd : : BINDING_PHYSICAL )
{
char sDevicePath [ 32 ] ;
sprintf ( sDevicePath , " \\ \\ . \\ %c: " , m_sDevicePath [ 0 ] ) ;
2008-08-24 21:28:42 +00:00
CAppConfig : : GetInstance ( ) . SetPreferenceString ( CDROM0PATH , sDevicePath ) ;
2008-04-12 16:02:19 +00:00
}
2006-06-15 04:19:30 +00:00
}