Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
11 KiB

  1. #include "Precomp.h"
  2. #include "resource.h"
  3. #include "global.h"
  4. #include "propwnd2.h"
  5. #include "FilePane.h"
  6. #include "NmAkWiz.h"
  7. #include "wndprocs.h"
  8. CFilePanePropWnd2::CFilePanePropWnd2(HWND hwndParent, UINT uIDD, LPTSTR szClassName, UINT PopUpHelpMenuTextId, int iX, int iY, int iWidth, int iHeight, BOOL bScroll ) :
  9. CPropertyDataWindow2( hwndParent, uIDD, szClassName, CFilePanePropWnd2::WndProc, PopUpHelpMenuTextId, iX, iY, iWidth, iHeight, bScroll ),
  10. m_fOpenDialog(FALSE)
  11. {
  12. // All the new stuff done in setfilepane...
  13. // this constructor is already too big
  14. }
  15. void CFilePanePropWnd2::SetFilePane(BOOL fOpenDialog, UINT editID, UINT checkID, UINT browseID, LPTSTR lptstrDesc, LPTSTR lptstrDefExtension, LPTSTR lptstrDefFileName )
  16. {
  17. SetWindowLong( m_hwnd, GWL_USERDATA, (long)this );
  18. m_fOpenDialog = fOpenDialog;
  19. m_editID = editID;
  20. m_checkID = checkID;
  21. m_browseID = browseID;
  22. m_lptstrFilter = NULL;
  23. m_lptstrDefExtension = NULL;
  24. m_lptstrDefFileName = NULL;
  25. _CopyFilter( &m_lptstrFilter, lptstrDesc, lptstrDefExtension );
  26. _CopyString( &m_lptstrDefExtension, lptstrDefExtension );
  27. _CopyString( &m_lptstrDefFileName, lptstrDefFileName );
  28. m_hwndEdit = GetDlgItem( m_hwnd, editID );
  29. m_hwndCheck = GetDlgItem( m_hwnd, checkID );
  30. Edit_LimitText( m_hwndEdit, MAX_PATH );
  31. m_hwndBrowse = GetDlgItem( m_hwnd, browseID );
  32. _SetDefaultPath();
  33. _InitOFN();
  34. }
  35. CFilePanePropWnd2::~CFilePanePropWnd2( void )
  36. {
  37. delete [] m_lptstrFilter;
  38. delete [] m_lptstrDefExtension;
  39. delete [] m_lptstrDefFileName;
  40. }
  41. LRESULT CALLBACK CFilePanePropWnd2::WndProc( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam )
  42. {
  43. switch( iMsg )
  44. {
  45. case WM_VSCROLL:
  46. {
  47. OnMsg_VScroll( hwnd, wParam );
  48. return 0;
  49. break;
  50. }
  51. case WM_COMMAND:
  52. if (BN_CLICKED == GET_WM_COMMAND_CMD(wParam, lParam))
  53. {
  54. CFilePanePropWnd2 *p = (CFilePanePropWnd2 *)GetWindowLong( hwnd, GWL_USERDATA);
  55. if (p && GET_WM_COMMAND_ID(wParam, lParam) == p->m_browseID)
  56. {
  57. p->QueryFilePath();
  58. }
  59. return(0);
  60. break;
  61. }
  62. }
  63. return( DefWindowProc( hwnd, iMsg, wParam, lParam ) );
  64. }
  65. HANDLE CFilePanePropWnd2::CreateFile( DWORD dwDesiredAccess,
  66. DWORD dwShareMode,
  67. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  68. DWORD dwCreationDisposition,
  69. DWORD dwFlagsAndAttributes )
  70. {
  71. CreateOutputDir();
  72. TCHAR szFile[ MAX_PATH ];
  73. TCHAR szPath[ MAX_PATH ];
  74. GetPath( szPath );
  75. GetFile( szFile );
  76. if( '\\' != szPath[ lstrlen( szPath ) - 1 ] )
  77. {
  78. lstrcat( szPath, TEXT("\\") );
  79. }
  80. lstrcat( szPath, szFile );
  81. return ::CreateFile( szPath,
  82. dwDesiredAccess,
  83. dwShareMode,
  84. lpSecurityAttributes,
  85. dwCreationDisposition,
  86. dwFlagsAndAttributes,
  87. NULL );
  88. }
  89. void CFilePanePropWnd2::_CopyFilter( LPTSTR* szTarget, LPTSTR szDec, LPTSTR szExt )
  90. {
  91. int index = 0;
  92. // Note - add 4 because this filter needs three null terminators and a '*'
  93. int iLen = (lstrlen( szDec ) + 1) + (1 + lstrlen( szExt ) + 1) + 1;
  94. *szTarget = new TCHAR[ iLen ];
  95. lstrcpy( *szTarget, szDec );
  96. index = lstrlen( *szTarget ) + 1;
  97. (*szTarget)[index] = '*';
  98. lstrcpy( &((*szTarget)[index+1]), szExt );
  99. (*szTarget)[ iLen - 1] = '\0';
  100. }
  101. void CFilePanePropWnd2::_CopyString( LPTSTR* szTarget, LPTSTR szSource )
  102. {
  103. int iLen = lstrlen( szSource ) + 1;
  104. *szTarget = new TCHAR[ iLen ];
  105. lstrcpy( *szTarget, szSource );
  106. }
  107. UINT CALLBACK CFilePanePropWnd2::OFNHookProc( HWND hdlg, // handle to child dialog window
  108. UINT uiMsg, // message identifier
  109. WPARAM wParam, // message parameter
  110. LPARAM lParam // message parameter
  111. )
  112. {
  113. switch (uiMsg)
  114. {
  115. case WM_INITDIALOG:
  116. {
  117. SetWindowLong(hdlg, GWL_USERDATA, ((OPENFILENAME *)lParam)->lCustData);
  118. break;
  119. }
  120. case WM_NOTIFY:
  121. {
  122. CFilePanePropWnd2 *p = (CFilePanePropWnd2 *)GetWindowLong( hdlg, GWL_USERDATA );
  123. if (NULL == p)
  124. break;
  125. return p->_OFNHookProc(hdlg, uiMsg, wParam, lParam);
  126. }
  127. default:
  128. break;
  129. }
  130. return 0;
  131. }
  132. UINT CALLBACK CFilePanePropWnd2::_OFNHookProc( HWND hdlg, // handle to child dialog window
  133. UINT uiMsg, // message identifier
  134. WPARAM wParam, // message parameter
  135. LPARAM lParam // message parameter
  136. )
  137. {
  138. switch( uiMsg )
  139. {
  140. case WM_NOTIFY:
  141. {
  142. OFNOTIFY * pOfnotify = (OFNOTIFY *) lParam;
  143. switch( pOfnotify -> hdr . code )
  144. {
  145. case CDN_FOLDERCHANGE:
  146. {
  147. TCHAR szFile[ MAX_PATH ];
  148. if( !CommDlg_OpenSave_GetSpec( GetParent( hdlg ), szFile, MAX_PATH ) ||
  149. 0 == lstrlen( szFile ) ||
  150. _tcschr( szFile, '\\' ) )
  151. {
  152. CommDlg_OpenSave_SetControlText( GetParent( hdlg ), edt1,
  153. m_szOFNData );
  154. }
  155. else
  156. {
  157. lstrcpy( m_szOFNData, szFile );
  158. OutputDebugString( szFile );
  159. }
  160. break;
  161. }
  162. case CDN_INITDONE:
  163. {
  164. GetFile( m_szOFNData );
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. return( 0 );
  171. }
  172. void CFilePanePropWnd2::_InitOFN( void )
  173. {
  174. ZeroMemory( &m_ofn, sizeof( m_ofn ) );
  175. m_ofn.lStructSize = sizeof( m_ofn );
  176. m_ofn.hwndOwner = m_hwnd;
  177. m_ofn.hInstance = g_hInstance;
  178. m_ofn.lpstrFilter = m_lptstrFilter;
  179. m_ofn.nMaxFile = MAX_PATH;
  180. m_ofn.lpstrDefExt = m_lptstrDefExtension;
  181. m_ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK ;// | OFN_OVERWRITEPROMPT;
  182. if (m_fOpenDialog)
  183. m_ofn.Flags |= OFN_FILEMUSTEXIST;
  184. m_ofn.lCustData = (long)this;
  185. m_ofn.lpfnHook = OFNHookProc;
  186. m_ofn.lpstrTitle = TEXT("Browse");
  187. }
  188. void CFilePanePropWnd2::QueryFilePath( void )
  189. {
  190. TCHAR szDir[MAX_PATH];
  191. TCHAR szFile[MAX_PATH];
  192. GetPath(szDir);
  193. GetFile(szFile);
  194. m_ofn.lpstrInitialDir = szDir;
  195. m_ofn.lpstrFile = szFile;
  196. BOOL bRet;
  197. if (m_fOpenDialog)
  198. bRet = GetOpenFileName(&m_ofn);
  199. else
  200. bRet = GetSaveFileName(&m_ofn);
  201. if( bRet )
  202. {
  203. Edit_SetText( m_hwndEdit, m_ofn.lpstrFile );
  204. }
  205. }
  206. void CFilePanePropWnd2::_SetDefaultPath( void )
  207. {
  208. TCHAR szDefaultDistributionFilePath[ MAX_PATH ];
  209. const TCHAR* szInstallationPath;
  210. szInstallationPath = GetInstallationPath();
  211. if( szInstallationPath )
  212. {
  213. lstrcpy( szDefaultDistributionFilePath, szInstallationPath );
  214. _tcscat( szDefaultDistributionFilePath, TEXT("\\output\\") );
  215. _tcscat( szDefaultDistributionFilePath, m_lptstrDefFileName );
  216. }
  217. else
  218. {
  219. _tcscat( szDefaultDistributionFilePath, m_lptstrDefFileName );
  220. }
  221. Edit_SetText( m_hwndEdit, szDefaultDistributionFilePath );
  222. }
  223. void CFilePanePropWnd2::CreateOutputDir( void )
  224. {
  225. TCHAR sz[ MAX_PATH ];
  226. GetPath( sz );
  227. CreateDirectory( sz, NULL );
  228. }
  229. LPTSTR CFilePanePropWnd2::GetPathAndFile( LPTSTR lpstrPath )
  230. {
  231. Edit_GetText( m_hwndEdit, lpstrPath, MAX_PATH );
  232. return lpstrPath;
  233. }
  234. LPTSTR CFilePanePropWnd2::GetPath( LPTSTR sz )
  235. {
  236. TCHAR path[ MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR];
  237. Edit_GetText( m_hwndEdit, path, MAX_PATH );
  238. _splitpath( path, drive, dir, NULL, NULL );
  239. wsprintf( sz, TEXT("%s%s"), drive, dir );
  240. return sz;
  241. }
  242. LPTSTR CFilePanePropWnd2::GetFile( LPTSTR sz )
  243. {
  244. TCHAR path[ MAX_PATH], file[_MAX_FNAME], ext[ _MAX_EXT];
  245. Edit_GetText( m_hwndEdit, path, MAX_PATH );
  246. _splitpath( path, NULL, NULL, file, ext );
  247. if (file[0] && (NULL == _tcschr(file, '\\')))
  248. {
  249. if (!lstrcmp( m_lptstrDefExtension, ext))
  250. {
  251. wsprintf(sz, TEXT("%s%s"), file, m_lptstrDefExtension);
  252. }
  253. else
  254. {
  255. wsprintf(sz, TEXT("%s%s%s"), file, ext, m_lptstrDefExtension);
  256. }
  257. }
  258. else
  259. {
  260. lstrcpy(sz, m_lptstrDefFileName);
  261. }
  262. return sz;
  263. }
  264. BOOL CFilePanePropWnd2::OptionEnabled()
  265. {
  266. return Button_GetCheck( m_hwndCheck ) ? TRUE : FALSE;
  267. }
  268. BOOL CFilePanePropWnd2::Validate( BOOL bMsg )
  269. {
  270. if( !OptionEnabled() )
  271. {
  272. return TRUE;
  273. }
  274. TCHAR szPath[ MAX_PATH ];
  275. TCHAR drive[ _MAX_DRIVE], dir[_MAX_DIR], ext[ _MAX_EXT];
  276. Edit_GetText( m_hwndEdit, szPath, MAX_PATH );
  277. if( 0 == lstrlen( szPath ) )
  278. {
  279. _SetDefaultPath();
  280. return FALSE;
  281. }
  282. _splitpath( szPath, drive, dir, NULL, ext );
  283. if( 0 != lstrcmp( m_lptstrDefExtension, ext ) )
  284. {
  285. lstrcat( szPath, m_lptstrDefExtension );
  286. }
  287. wsprintf( szPath, TEXT("%s%s"), drive, dir );
  288. // Verify that we can write to the location
  289. if( szPath[ lstrlen( szPath ) - 1 ] != '\\' ) {
  290. _tcscat( szPath, TEXT("\\") );
  291. }
  292. strcat( szPath, TEXT("eraseme.now") );
  293. HANDLE hFile = ::CreateFile( szPath,
  294. GENERIC_WRITE,
  295. 0,
  296. NULL,
  297. CREATE_ALWAYS,
  298. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
  299. NULL
  300. );
  301. if( INVALID_HANDLE_VALUE == hFile )
  302. {
  303. switch( GetLastError() )
  304. {
  305. case ERROR_PATH_NOT_FOUND:
  306. {
  307. // Try to create the directory...
  308. GetPath( szPath );
  309. if( CreateDirectory( szPath, NULL ) )
  310. {
  311. // Everything is OK, we created the directory at the path
  312. RemoveDirectory( szPath );
  313. // Ask if we should create the directory
  314. if( bMsg )
  315. {
  316. if( IDNO == NmrkMessageBox(MAKEINTRESOURCE(IDS_CREATE_DIRECTORY), NULL, MB_YESNO | MB_ICONQUESTION ) )
  317. {
  318. return FALSE;
  319. }
  320. else
  321. {
  322. return TRUE;
  323. }
  324. }
  325. else
  326. {
  327. _SetDefaultPath();
  328. return FALSE;
  329. }
  330. }
  331. //ErrorMessage();
  332. if( bMsg )
  333. {
  334. NmrkMessageBox(MAKEINTRESOURCE(IDS_SELECTED_PATH_IS_INVALID_PLEASE_CHANGE_THE_PATH_NAME_OR_BROWSE_FOR_A_NEW_PATH),
  335. MAKEINTRESOURCE( IDS_NMAKWIZ_ERROR_CAPTION),
  336. MB_OK | MB_ICONEXCLAMATION
  337. );
  338. }
  339. else
  340. {
  341. _SetDefaultPath();
  342. }
  343. return FALSE;
  344. break;
  345. }
  346. case ERROR_ACCESS_DENIED:
  347. {
  348. if( bMsg )
  349. {
  350. NmrkMessageBox(
  351. MAKEINTRESOURCE(IDS_YOU_DO_NOT_HAVE_WRITE_ACCESS_TO_THE_SELECTED_PATH_PLEASE_SELECT_A_PATH_IN_WHICH_YOU_HAVE_WRITE_PERMISSION),
  352. MAKEINTRESOURCE(IDS_NMAKWIZ_ERROR_CAPTION),
  353. MB_OK | MB_ICONEXCLAMATION
  354. );
  355. }
  356. else
  357. {
  358. _SetDefaultPath();
  359. }
  360. return FALSE;
  361. break;
  362. }
  363. default:
  364. return FALSE;
  365. break;
  366. }
  367. }
  368. else {
  369. CloseHandle( hFile );
  370. DeleteFile( szPath );
  371. }
  372. return TRUE;
  373. }