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.

174 lines
4.6 KiB

  1. // FolderDialog.cpp: implementation of the CFolderDialog class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #define __FILE_ID__ 70
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. extern CClientConsoleApp theApp;
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. DWORD
  16. CFolderDialog::Init(
  17. LPCTSTR tszInitialDir, // = NULL
  18. UINT nTitleResId // = 0
  19. )
  20. {
  21. DWORD dwRes = ERROR_SUCCESS;
  22. DBG_ENTER(TEXT("CFolderDialog::Init"), dwRes);
  23. //
  24. // copy an initial folder name
  25. //
  26. m_tszInitialDir[ARR_SIZE(m_tszInitialDir) - 1] = 0;
  27. if(tszInitialDir && _tcslen(tszInitialDir) > 0)
  28. {
  29. _tcsncpy(m_tszInitialDir, tszInitialDir, ARR_SIZE(m_tszInitialDir)-1);
  30. }
  31. //
  32. // load a title resource string
  33. //
  34. if(0 != nTitleResId)
  35. {
  36. dwRes = LoadResourceString (m_cstrTitle, nTitleResId);
  37. if(ERROR_SUCCESS != dwRes)
  38. {
  39. CALL_FAIL (RESOURCE_ERR, TEXT ("LoadResourceString"), dwRes);
  40. return dwRes;
  41. }
  42. }
  43. return dwRes;
  44. } // CFolderDialog::Init
  45. int
  46. CALLBACK
  47. CFolderDialog::BrowseCallbackProc(
  48. HWND hwnd,
  49. UINT uMsg,
  50. LPARAM lp,
  51. LPARAM pData
  52. )
  53. {
  54. DBG_ENTER(TEXT("CFolderDialog::BrowseCallbackProc"));
  55. CFolderDialog* pFolderDlg = (CFolderDialog*)pData;
  56. ASSERTION(pFolderDlg);
  57. switch (uMsg)
  58. {
  59. case BFFM_SELCHANGED:
  60. {
  61. BOOL bFolderIsOK = FALSE;
  62. TCHAR szPath [MAX_PATH + 1];
  63. if (SHGetPathFromIDList ((LPITEMIDLIST) lp, szPath))
  64. {
  65. DWORD dwFileAttr = GetFileAttributes(szPath);
  66. if (-1 != dwFileAttr && (dwFileAttr & FILE_ATTRIBUTE_DIRECTORY))
  67. {
  68. //
  69. // The directory exists - enable the 'Ok' button
  70. //
  71. bFolderIsOK = TRUE;
  72. }
  73. }
  74. //
  75. // Enable / disable the 'ok' button
  76. //
  77. SendMessage(hwnd, BFFM_ENABLEOK , 0, (LPARAM)bFolderIsOK);
  78. break;
  79. }
  80. case BFFM_INITIALIZED:
  81. if(_tcslen(pFolderDlg->m_tszInitialDir) > 0)
  82. {
  83. //
  84. // WParam is TRUE since you are passing a path.
  85. // It would be FALSE if you were passing a pidl.
  86. //
  87. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)pFolderDlg->m_tszInitialDir);
  88. }
  89. break;
  90. case BFFM_VALIDATEFAILED:
  91. //
  92. // The folder name is invalid.
  93. // Do not close the dialog.
  94. //
  95. MessageBeep(MB_OK);
  96. return 1;
  97. }
  98. return 0;
  99. } // CFolderDialog::BrowseCallbackProc
  100. UINT
  101. CFolderDialog::DoModal(
  102. DWORD dwFlags /* =0 */)
  103. {
  104. DBG_ENTER(TEXT("CFolderDialog::DoModal"));
  105. BROWSEINFO browseInfo = {0};
  106. browseInfo.hwndOwner = theApp.m_pMainWnd->m_hWnd;
  107. browseInfo.pidlRoot = NULL;
  108. browseInfo.pszDisplayName = 0;
  109. browseInfo.lpszTitle = (m_cstrTitle.GetLength() != 0) ? (LPCTSTR)m_cstrTitle : NULL;
  110. browseInfo.ulFlags = dwFlags | BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS | BIF_USENEWUI | BIF_VALIDATE;
  111. browseInfo.lpfn = BrowseCallbackProc;
  112. browseInfo.lParam = (LPARAM)this;
  113. //
  114. // Need OLE for a new style of the BrowseForFolder dialog
  115. //
  116. OleInitialize(NULL);
  117. LPITEMIDLIST pItemIdList = ::SHBrowseForFolder(&browseInfo);
  118. if(NULL == pItemIdList)
  119. {
  120. //
  121. // Cancel
  122. //
  123. OleUninitialize();
  124. return IDCANCEL;
  125. }
  126. OleUninitialize();
  127. //
  128. // get path from pItemIdList
  129. //
  130. if(!SHGetPathFromIDList(pItemIdList, (TCHAR*)&m_tszSelectedDir))
  131. {
  132. m_dwLastError = ERROR_CAN_NOT_COMPLETE;
  133. CALL_FAIL (GENERAL_ERR, TEXT("SHGetPathFromIDList"), m_dwLastError);
  134. return IDABORT;
  135. }
  136. //
  137. // free pItemIdList
  138. //
  139. LPMALLOC pMalloc;
  140. HRESULT hRes = SHGetMalloc(&pMalloc);
  141. if(E_FAIL == hRes)
  142. {
  143. m_dwLastError = ERROR_CAN_NOT_COMPLETE;
  144. CALL_FAIL (GENERAL_ERR, TEXT("SHGetMalloc"), m_dwLastError);
  145. return IDABORT;
  146. }
  147. pMalloc->Free(pItemIdList);
  148. pMalloc->Release();
  149. return IDOK;
  150. } // CFolderDialog::DoModal