1 //////////////////////////////////////////////////////////////////////////
4 //Written by Nguyen Tan Hung <tanhung@yahoo.com>
5 //////////////////////////////////////////////////////////////////////////
8 #include "PathDialog.h"
14 static char THIS_FILE
[] = __FILE__
;
17 #define IDC_FOLDERTREE 0x3741
18 #define IDC_TITLE 0x3742
19 #define IDC_STATUSTEXT 0x3743
21 #define IDC_NEW_EDIT_PATH 0x3744
24 BEGIN_MESSAGE_MAP(CPathDialogSub
, CWnd
)
25 ON_BN_CLICKED(IDOK
, OnOK
)
26 ON_EN_CHANGE(IDC_NEW_EDIT_PATH
, OnChangeEditPath
)
29 void CPathDialogSub::OnOK()
31 ::GetWindowText(::GetDlgItem(m_hWnd
, IDC_NEW_EDIT_PATH
),
32 m_pPathDialog
->m_szPathName
, MAX_PATH
);
34 if(CPathDialog::MakeSurePathExists(m_pPathDialog
->m_szPathName
)==0)
36 m_pPathDialog
->m_bGetSuccess
=TRUE
;
37 ::EndDialog(m_pPathDialog
->m_hWnd
, IDOK
);
41 ::SetFocus(::GetDlgItem(m_hWnd
, IDC_NEW_EDIT_PATH
));
45 void CPathDialogSub::OnChangeEditPath()
47 ::GetWindowText(::GetDlgItem(m_hWnd
, IDC_NEW_EDIT_PATH
),
48 m_pPathDialog
->m_szPathName
, MAX_PATH
);
49 BOOL bEnableOKButton
= (_tcslen(m_pPathDialog
->m_szPathName
)>0);
50 SendMessage(BFFM_ENABLEOK
, 0, bEnableOKButton
);
52 /////////////////////////////////////////////////////////////////////////////
56 CPathDialog::CPathDialog(LPCTSTR lpszCaption
,
58 LPCTSTR lpszInitialPath
,
62 m_PathDialogSub
.m_pPathDialog
= this;
63 m_bParentDisabled
= FALSE
;
65 // Get the true parent of the dialog
66 m_pParentWnd
= CWnd::GetSafeOwner(pParent
);
68 m_lpszCaption
= lpszCaption
;
69 m_lpszInitialPath
= lpszInitialPath
;
71 memset(&m_bi
, 0, sizeof(BROWSEINFO
) );
72 m_bi
.hwndOwner
= (m_pParentWnd
==NULL
)?NULL
:m_pParentWnd
->GetSafeHwnd();
73 m_bi
.pszDisplayName
= 0;
75 m_bi
.ulFlags
= BIF_RETURNONLYFSDIRS
| BIF_STATUSTEXT
;
76 m_bi
.lpfn
= BrowseCallbackProc
;
77 m_bi
.lpszTitle
= lpszTitle
;
81 /////////////////////////////////////////////////////////////////////////////
82 // CPathDialog message handlers
84 CString
CPathDialog::GetPathName()
86 return CString(m_szPathName
);
89 int CALLBACK
CPathDialog::BrowseCallbackProc(HWND hwnd
,UINT uMsg
,LPARAM lParam
, LPARAM pData
)
91 CPathDialog
* pDlg
= (CPathDialog
*)pData
;
95 case BFFM_INITIALIZED
:
103 if(pDlg
->m_lpszCaption
!=NULL
)
105 ::SetWindowText(hwnd
, pDlg
->m_lpszCaption
);
108 VERIFY(pDlg
->m_PathDialogSub
.SubclassWindow(hwnd
));
109 ::ShowWindow(::GetDlgItem(hwnd
, IDC_STATUSTEXT
), SW_HIDE
);
110 ::GetWindowRect(::GetDlgItem(hwnd
, IDC_FOLDERTREE
), &rc
);
111 rc
.bottom
= rc
.top
- 4;
112 rc
.top
= rc
.bottom
- 23;
113 ::ScreenToClient(hwnd
, (LPPOINT
)&rc
);
114 ::ScreenToClient(hwnd
, ((LPPOINT
)&rc
)+1);
115 hEdit
= ::CreateWindowEx(WS_EX_CLIENTEDGE
, _T("EDIT"), _T(""),
116 WS_CHILD
|WS_TABSTOP
|WS_VISIBLE
|ES_AUTOHSCROLL
,
118 rc
.right
-rc
.left
, rc
.bottom
-rc
.top
,
119 hwnd
, NULL
, NULL
, NULL
);
120 ::SetWindowLong(hEdit
, GWL_ID
, IDC_NEW_EDIT_PATH
);
121 ::ShowWindow(hEdit
, SW_SHOW
);
123 hFont
= (HFONT
)::SendMessage(hwnd
, WM_GETFONT
, 0, 0);
124 ::SendMessage(hEdit
, WM_SETFONT
, (WPARAM
)hFont
, MAKELPARAM(TRUE
, 0));
126 LPCTSTR lpszPath
= pDlg
->m_lpszInitialPath
;
127 TCHAR szTemp
[MAX_PATH
];
130 ::GetCurrentDirectory(MAX_PATH
, szTemp
);
133 // WParam is TRUE since you are passing a path.
134 // It would be FALSE if you were passing a pidl.
135 ::SendMessage(hwnd
,BFFM_SETSELECTION
,TRUE
,
139 case BFFM_SELCHANGED
:
141 char szSelection
[MAX_PATH
];
142 if(!::SHGetPathFromIDList((LPITEMIDLIST
)lParam
, szSelection
) ||
143 (szSelection
[1] !=':' && szSelection
[1] != '\\'))
145 szSelection
[0] = '\0';
146 ::SendMessage(hwnd
, BFFM_ENABLEOK
, 0, FALSE
);
150 ::SendMessage(hwnd
, BFFM_ENABLEOK
, 0, TRUE
);
152 ::SendMessage(hwnd
,BFFM_SETSTATUSTEXT
,0,(LPARAM
)szSelection
);
153 ::SetWindowText(::GetDlgItem(hwnd
, IDC_NEW_EDIT_PATH
), szSelection
);
164 int CPathDialog::DoModal()
167 /////////////////////////////////////////////////////////
168 TCHAR szPathTemp
[MAX_PATH
];
169 m_bi
.lpfn
= BrowseCallbackProc
; // address of callback function
170 m_bi
.lParam
= (LPARAM
)this; // pass address of object to callback function
171 m_bi
.pszDisplayName
= szPathTemp
;
177 if(SUCCEEDED(SHGetMalloc(&pMalloc
)))
179 m_bGetSuccess
= FALSE
;
180 pidl
= SHBrowseForFolder(&m_bi
);
183 #if defined(_WIN64) && defined(__INTEL_COMPILER)
184 # pragma warning ( disable : 167)
189 //pMalloc->lpVtbl->Free(pMalloc,pidl);
190 //pMalloc->lpVtbl->Release(pMalloc);
199 if(m_bParentDisabled
&& (m_pParentWnd
!=NULL
))
201 m_pParentWnd
->EnableWindow(TRUE
);
203 m_bParentDisabled
=FALSE
;
208 BOOL
CPathDialog::IsFileNameValid(LPCTSTR
/* lpFileName */)
213 const TCHAR c_FolderDoesNotExist
[] = _T(
216 "does not exist. Do you want the folder to be created?");
217 const TCHAR c_szErrInvalidPath
[] = _T(
221 "is invalid. Please reenter.");
222 const TCHAR c_szErrCreatePath
[] = _T(
226 "\n\ncan not be created. Please double check.");
228 //return -1: user break;
230 //return 1: lpPath is invalid
231 //return 2: can not create lpPath
232 int CPathDialog::MakeSurePathExists(LPCTSTR lpPath
)
239 iRet
=Touch(lpPath
, TRUE
);
245 if(_taccess(lpPath
, 0)==0)
250 strMsg
.Format(c_FolderDoesNotExist
, lpPath
);
251 if(AfxMessageBox(strMsg
, MB_YESNO
|MB_ICONQUESTION
) == IDYES
)
254 iRet
=Touch(lpPath
, FALSE
);
267 strMsg
.Format(c_szErrInvalidPath
, lpPath
);
271 strMsg
.Format(c_szErrCreatePath
, lpPath
);
275 AfxMessageBox(strMsg
, MB_OK
|MB_ICONEXCLAMATION
);
282 //return 1: lpPath is invalid
283 //return 2: lpPath can not be created(bValidate==FALSE)
284 int CPathDialog::Touch(LPCTSTR lpPath
, BOOL bValidate
)
291 TCHAR szPath
[MAX_PATH
];
292 _tcscpy(szPath
, lpPath
);
293 size_t nLen
= _tcslen(szPath
);
300 if(_access(szPath
, 0)!=0)
310 LPTSTR lpCurrentName
;
313 lpCurrentName
= &szPath
[i
];
314 while( (szPath
[i
]!=0) && (szPath
[i
]!=_T('\\')) )
319 bLastOne
=(szPath
[i
]==0);
324 CreateDirectory(szPath
, NULL
);
325 if(_taccess(szPath
, 0)!=0)
337 szPath
[i
] = _T('\\');
343 return (bLastOne
?0:1);
348 int CPathDialog::ConcatPath(LPTSTR lpRoot
, LPCTSTR lpMorePath
)
355 size_t nLen
= _tcslen(lpRoot
);
369 _tcscat(lpRoot
, lpMorePath
);
373 _tcscat(lpRoot
, _T("\\"));
374 _tcscat(lpRoot
, lpMorePath
);