www.pudn.com > NewDir.zip > DirSelDlg.cpp, change:1999-05-18,size:9655b


// DirDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "DirSelDlg.h" 
#include "DLGS.H" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
//Special Handling 
#define WM_SPECIAL_MSG		(WM_USER+101) 
 
 
//Standard File Open Dialog's child control IDs 
//EDIT, LIST and FileType combo controls 
#define IDC_FOPEN_EDIT		edt1 
#define IDC_FOPEN_LIST		lst1 
#define IDC_FOPEN_COMB		cmb1 
//static texts before edit & combo boxes 
#define IDC_FOPEN_ESTAT		stc3 
#define IDC_FOPEN_CSTAT		stc2 
 
 
//THE NEW CONTROLS which are being created as children of parent CFileDialog 
#define IDC_LIST_SPL_FILES	1432 
#define IDC_CHK_SPL_SILES	1433 
#define IDC_BTN_SPL_SEL		1434 
#define IDC_COMB_SPL_DIR	1435 
 
 
//Global pointer 
static CDirSelDialog* g_pThis = NULL; 
 
 
//Global Bookkeeping 
static CStringArray g_DirArr; 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CDirSelDialog 
 
CDirSelDialog::CDirSelDialog( CString szTitle ) : 
		CFileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,  
			"Dirs |.*||", AfxGetMainWnd() ) 
{ 
	m_szTitle = szTitle; 
	g_pThis = this; 
} 
 
CDirSelDialog::~CDirSelDialog() 
{ 
} 
 
BEGIN_MESSAGE_MAP(CDirSelDialog, CFileDialog) 
	//{{AFX_MSG_MAP(CDirSelDialog) 
	ON_MESSAGE(WM_SPECIAL_MSG, OnSpecialMsg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
//NOW We are putting a new hook for windproc 
static WNDPROC g_wndOldProc = NULL; 
 
// Function name	: WindProcNew 
// Description	    : Call this function when user navigate into CFileDialog. 
// Return type		: LRESULT 
// Argument         : HWND hwnd 
// Argument         : UINT message 
// Argument         : WPARAM wParam 
// Argument         : LPARAM lParam 
LRESULT CALLBACK WindProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	if (message ==  WM_COMMAND) 
	{ 
		if (HIWORD(wParam) == CBN_SELCHANGE) 
		{ 
			if (LOWORD(wParam) == IDC_COMB_SPL_DIR) 
			{ 
				if( CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(hwnd) ) 
				{ 
					if( g_pThis ) 
						g_pThis->OnSplDirSelChanged(); 
				} 
			} 
		} 
		else if (HIWORD(wParam) == BN_CLICKED) 
		{ 
			if (LOWORD(wParam) == IDC_CHK_SPL_SILES) 
			{ 
				if( CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(hwnd) ) 
				{ 
					if( g_pThis ) 
						g_pThis->OnChkFiles(); 
				} 
			} 
			else if( LOWORD(wParam) == IDC_BTN_SPL_SEL ) 
			{ 
				if( CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(hwnd) ) 
				{ 
					if( g_pThis ) 
						g_pThis->OnBtnDirOk(); 
				} 
			} 
			else if (LOWORD(wParam) == IDOK) 
			{ 
				if( CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(hwnd) ) 
				{ 
					CString sTyped; 
					BOOL bOk = FALSE; 
					pDlg->GetDlgItemText(IDC_FOPEN_EDIT, sTyped); 
					if( !sTyped.IsEmpty() ) 
					{ 
						DWORD dwAttr = ::GetFileAttributes(sTyped); 
						if( dwAttr != 0xFFFFFFFF && 
							(dwAttr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY ) 
						{ 
							bOk = TRUE; 
						} 
						else 
						{ 
							if( strncmp( sTyped, "\\\\", 2 ) == 0 ) 
							{ 
								bOk = TRUE; 
 
								//Postmessage to do some thing 
								if( g_pThis ) 
									g_pThis->PostMessage(WM_SPECIAL_MSG); 
							} 
							else if( sTyped.Compare(".*") == 0 ) 
							{ 
								bOk = TRUE; 
							} 
						} 
					} 
					if( !bOk ) 
					{ 
						::MessageBeep(MB_ICONHAND); 
						return 0; 
					} 
				} 
			} 
		} 
	} 
	return CallWindowProc(g_wndOldProc, hwnd, message, wParam, lParam); 
} 
 
 
BOOL CDirSelDialog::OnInitDialog()  
{ 
	CFileDialog::OnInitDialog(); 
	 
	CWnd* pParent = GetParent(); 
	CWnd* pWnd; 
 
	//Create a ListBox for files only 
	//First Try to move the old list 
	CRect rectList; 
	pWnd = pParent->GetDlgItem(IDC_FOPEN_LIST); 
	pWnd->GetWindowRect(rectList); 
	pParent->ScreenToClient(rectList); 
	rectList.right -= 131; 
	int nListRight = rectList.right; 
	//pWnd->MoveWindow( rectList ); //Move when the checkbox is clicked 
 
	//Create the files list box 
	CRect rect1(rectList.right + 3, rectList.top, rectList.right + 130, rectList.bottom ); 
	m_FileList.Create(LBS_STANDARD|LBS_SORT|WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_NOINTEGRALHEIGHT,  
		rect1, pParent, IDC_LIST_SPL_FILES); 
	long lval = GetWindowLong(m_FileList.m_hWnd, GWL_EXSTYLE); 
	lval |= WS_EX_STATICEDGE; 
	SetWindowLong(m_FileList.m_hWnd, GWL_EXSTYLE, lval); 
 
	//This is MS Sans Serif font.  
	CClientDC dc(this); 
	int nHgt = ((dc.GetDeviceCaps(LOGPIXELSY)*8)/72 ); 
	m_Font.CreateFont( nHgt, 0,0,0, FW_NORMAL, 0,0,0, 
				DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, 
				DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "MS Sans Serif" ); 
	m_FileList.SetFont( &m_Font ); 
	m_FileList.ShowWindow( SW_HIDE ); //Default hide it 
 
	//Change the static texts 
	pParent->SetDlgItemText( IDC_FOPEN_ESTAT, "&Type here:" ); 
	pParent->SetDlgItemText( IDC_FOPEN_CSTAT, "&Selected:" ); 
 
	//Hide Open button 
	pWnd = pParent->GetDlgItem(IDOK); 
	pWnd->SetWindowText("Open"); //Remove & 
	CRect rectOk; 
	pWnd->GetWindowRect( rectOk ); 
	pParent->ScreenToClient( rectOk ); 
	int nOkRight = rectOk.right; 
	int nOkWidth = rectOk.Width(); 
	pWnd->ShowWindow(SW_HIDE); 
	 
	//move file name edit 
	pWnd = pParent->GetDlgItem( IDC_FOPEN_EDIT ); 
	CRect rectEdit; 
	pWnd->GetWindowRect( rectEdit ); 
	pParent->ScreenToClient( rectEdit ); 
	rectEdit.right = nListRight-2; 
	rectEdit.left -= 15; 
	pWnd->MoveWindow( rectEdit ); 
 
	//Hide the file type combo 
	pWnd = pParent->GetDlgItem( IDC_FOPEN_COMB ); 
	pWnd->ShowWindow(SW_HIDE); 
 
	//Create the check button 
	m_ChkBtn.Create("Show &Files also", WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,  
				CRect( nListRight+6, rectEdit.top+3, nOkRight, rectEdit.bottom-2),  
				pParent, IDC_CHK_SPL_SILES); 
	m_ChkBtn.SetFont(&m_Font); 
	 
	//Create the ComboBox Control 
	m_SplDirCombo.Create(WS_CHILD|WS_BORDER|WS_VISIBLE|CBS_AUTOHSCROLL|CBS_DROPDOWN|WS_VSCROLL, 
				CRect(rectEdit.left, rectEdit.bottom+7, rectEdit.right, rectEdit.bottom+120), 
				pParent, IDC_COMB_SPL_DIR); 
	m_SplDirCombo.SetFont(&m_Font); 
 
	//Add the dirs 
	for( int i = 0; g_DirArr.GetSize(); i++ ) 
	{ 
		if( m_SplDirCombo.FindStringExact(-1, g_DirArr[i]) == -1 ) 
			m_SplDirCombo.InsertString(0, g_DirArr[i]); 
	} 
 
	//Make the combo's edit control read-only 
	CWnd* pWndEdit = m_SplDirCombo.GetWindow(GW_CHILD); 
	if( pWndEdit ) 
	{ 
		((CEdit*)pWndEdit)->SetReadOnly(TRUE); 
	} 
 
	//Create the new Ok button 
	CRect rectNewOk(nListRight+3, rectEdit.bottom+7, nListRight+nOkWidth-11, rectEdit.bottom+rectEdit.Height()+8); 
	m_SelBtn.Create("&OK", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,  
				rectNewOk, pParent, IDC_BTN_SPL_SEL); 
	m_SelBtn.SetFont(&m_Font); 
 
	//Move the cancel button 
	rectNewOk.left = rectNewOk.right+6; 
	rectNewOk.right = rectNewOk.left + nOkWidth-14; 
	pParent->GetDlgItem(IDCANCEL)->MoveWindow(rectNewOk); 
 
	//Set the title 
	pParent->SetWindowText( m_szTitle ); 
	 
	//Change the Window Proc for the parent CFileDialog 
	g_wndOldProc = (WNDPROC)SetWindowLong(pParent->m_hWnd, GWL_WNDPROC, (long)WindProcNew); 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CDirSelDialog::OnFolderChange() 
{ 
	char buf[256]; 
	CommDlg_OpenSave_GetFolderPath( GetParent()->m_hWnd, 
		buf, 256 ); 
	 
	m_SplDirCombo.SetWindowText(buf); 
	m_FileList.ResetContent(); 
	int nLen = strlen(buf); 
	if( nLen ) 
	{ 
		//Add to the combo 
		if( m_SplDirCombo.FindStringExact(-1, buf) == -1 ) 
			m_SplDirCombo.InsertString(0, buf); 
 
		//Fill the filelist 
		if( buf[nLen-1] != '\\' && buf[nLen-1] != '/' ) 
			strcat(buf, "\\"); 
		strcat(buf, "*.*"); 
		m_FileList.Dir(0x0000, buf); 
 
	} 
} 
 
int CDirSelDialog::OnFileNameOK() 
{ 
	::MessageBeep( MB_ICONQUESTION ); 
	return 1; //don't let the dialog to close 
} 
 
void CDirSelDialog::OnBtnDirOk() 
{ 
	char buf[256]; 
	CommDlg_OpenSave_GetFolderPath( GetParent()->m_hWnd, buf, 256 ); 
	m_szDirName = buf; 
	if( m_szDirName.IsEmpty() ) 
	{ 
		::MessageBeep(MB_ICONEXCLAMATION); 
		return; 
	} 
 
	g_DirArr.RemoveAll(); 
	int nSize = m_SplDirCombo.GetCount(); 
	CString sDir; 
	for( int i = 0; i < nSize; i++ ) 
	{ 
		m_SplDirCombo.GetLBText(i, sDir); 
		g_DirArr.Add( sDir ); 
	} 
 
	((CFileDialog*)GetParent())->EndDialog(IDOK); 
} 
 
 
CString CDirSelDialog::GetDirName() 
{ 
	return m_szDirName; 
} 
 
void CDirSelDialog::OnChkFiles()  
{ 
	static BOOL bChkFiles = FALSE; 
	 
	bChkFiles = !bChkFiles; 
 
	CWnd* pParent = GetParent(); 
	CRect rect; 
	CWnd* pWnd = pParent->GetDlgItem(IDC_FOPEN_LIST); 
	pWnd->GetWindowRect(rect); 
	pParent->ScreenToClient(rect); 
	 
	if( !bChkFiles ) 
	{ 
		m_FileList.ShowWindow(SW_HIDE); 
 
		rect.right += 130; 
		pWnd->MoveWindow( rect ); 
	} 
	else 
	{ 
		rect.right -= 130; 
		pWnd->MoveWindow( rect ); 
 
		m_FileList.ShowWindow(SW_SHOWNORMAL); 
	} 
 
	//Refresh the windows 
	ExecuteText( ".*" ); 
} 
 
long CDirSelDialog::OnSpecialMsg(UINT, UINT) 
{ 
	//We will put .* forcefully execute and get the job done 
	ExecuteText(".*"); 
	return 1; 
} 
 
void CDirSelDialog::OnSplDirSelChanged() 
{ 
	int nSel = m_SplDirCombo.GetCurSel(); 
	if( nSel >= 0 ) 
	{ 
		CString str; 
		m_SplDirCombo.GetLBText(nSel, str); 
		ExecuteText( str ); 
//		GetParent()->PostMessage(WM_NEXTDLGCTL, 0, 0); 
	} 
} 
 
void CDirSelDialog::ExecuteText(CString strNew) 
{ 
	CWnd* pParent = GetParent(); 
	pParent->SetDlgItemText(IDC_FOPEN_EDIT, strNew); 
	pParent->SendMessage(WM_COMMAND, MAKELONG(IDOK, BN_CLICKED),  
		(long)(pParent->GetDlgItem(IDOK)->m_hWnd) ); 
	pParent->SetDlgItemText(IDC_FOPEN_EDIT, ""); 
}