BsWin32BrowseDialogs.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsCorePrerequisites.h"
  4. #include "Platform/BsPlatform.h"
  5. #include "Threading/BsAsyncOp.h"
  6. #include "CoreThread/BsCoreThread.h"
  7. #include "Win32/BsWin32Window.h"
  8. #include <ShObjIdl.h>
  9. using namespace std::placeholders;
  10. namespace bs
  11. {
  12. void addFiltersToDialog(IFileDialog* fileDialog, const WString& filterList)
  13. {
  14. const wchar_t EMPTY_WSTR[] = L"";
  15. if (filterList.empty())
  16. return;
  17. Vector<WString> filters = StringUtil::split(filterList, L";");
  18. UINT32 numFilters = (UINT32)filters.size();
  19. if (numFilters == 0)
  20. return;
  21. COMDLG_FILTERSPEC* specList = bs_newN<COMDLG_FILTERSPEC>(numFilters);
  22. for (size_t i = 0; i < numFilters; ++i)
  23. {
  24. specList[i].pszName = EMPTY_WSTR;
  25. specList[i].pszSpec = filters[i].c_str();
  26. }
  27. fileDialog->SetFileTypes(numFilters, specList);
  28. bs_deleteN(specList, numFilters);
  29. }
  30. void setDefaultPath(IFileDialog* dialog, const Path& defaultPath)
  31. {
  32. WString pathStr = defaultPath.toWString();
  33. const wchar_t* defaultPathW = pathStr.c_str();
  34. IShellItem* folder;
  35. HRESULT result = SHCreateItemFromParsingName(defaultPathW, NULL, IID_PPV_ARGS(&folder));
  36. // Valid non results.
  37. if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || result == HRESULT_FROM_WIN32(ERROR_INVALID_DRIVE))
  38. return;
  39. if (!SUCCEEDED(result))
  40. return;
  41. dialog->SetFolder(folder);
  42. folder->Release();
  43. }
  44. void getPaths(IShellItemArray* shellItems, Vector<Path>& paths)
  45. {
  46. DWORD numShellItems;
  47. shellItems->GetCount(&numShellItems);
  48. for (DWORD i = 0; i < numShellItems; ++i)
  49. {
  50. IShellItem* shellItem = nullptr;
  51. shellItems->GetItemAt(i, &shellItem);
  52. SFGAOF attribs;
  53. shellItem->GetAttributes(SFGAO_FILESYSTEM, &attribs);
  54. if (!(attribs & SFGAO_FILESYSTEM))
  55. continue;
  56. LPWSTR name;
  57. shellItem->GetDisplayName(SIGDN_FILESYSPATH, &name);
  58. paths.push_back((Path)name);
  59. CoTaskMemFree(name);
  60. }
  61. }
  62. void openBrowseDialogCore(FileDialogType type, const Path& defaultPath, const WString& filterList,
  63. Vector<Path>& paths, AsyncOp& returnValue)
  64. {
  65. // Init COM library.
  66. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  67. IFileDialog* fileDialog = nullptr;
  68. UINT32 dialogType = ((UINT32)type & (UINT32)FileDialogType::TypeMask);
  69. bool isOpenDialog = dialogType == (UINT32)FileDialogType::OpenFile || dialogType == (UINT32)FileDialogType::OpenFolder;
  70. // Create dialog
  71. IID classId = isOpenDialog ? CLSID_FileOpenDialog : CLSID_FileSaveDialog;
  72. CoCreateInstance(classId, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&fileDialog));
  73. addFiltersToDialog(fileDialog, filterList);
  74. setDefaultPath(fileDialog, defaultPath);
  75. // Apply multiselect flags
  76. bool isMultiselect = false;
  77. if (isOpenDialog)
  78. {
  79. if (dialogType == (UINT32)FileDialogType::OpenFile)
  80. {
  81. if (((UINT32)type & (UINT32)FileDialogType::Multiselect) != 0)
  82. {
  83. DWORD dwFlags;
  84. fileDialog->GetOptions(&dwFlags);
  85. fileDialog->SetOptions(dwFlags | FOS_ALLOWMULTISELECT);
  86. isMultiselect = true;
  87. }
  88. }
  89. else
  90. {
  91. DWORD dwFlags;
  92. fileDialog->GetOptions(&dwFlags);
  93. fileDialog->SetOptions(dwFlags | FOS_PICKFOLDERS);
  94. }
  95. }
  96. // Show the dialog
  97. bool finalResult = false;
  98. // Need to enable all windows, otherwise when the browse dialog closes the active window will become some
  99. // background window
  100. Win32Window::_enableAllWindows();
  101. if (SUCCEEDED(fileDialog->Show(nullptr)))
  102. {
  103. if (isMultiselect)
  104. {
  105. // Get file names
  106. IFileOpenDialog* fileOpenDialog;
  107. fileDialog->QueryInterface(IID_IFileOpenDialog, (void**)&fileOpenDialog);
  108. IShellItemArray* shellItems = nullptr;
  109. fileOpenDialog->GetResults(&shellItems);
  110. getPaths(shellItems, paths);
  111. shellItems->Release();
  112. fileOpenDialog->Release();
  113. }
  114. else
  115. {
  116. // Get the file name
  117. IShellItem* shellItem = nullptr;
  118. fileDialog->GetResult(&shellItem);
  119. LPWSTR filePath = nullptr;
  120. shellItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath);
  121. paths.push_back((Path)filePath);
  122. CoTaskMemFree(filePath);
  123. shellItem->Release();
  124. }
  125. finalResult = true;
  126. }
  127. // Restore modal window state (before we enabled all windows)
  128. Win32Window::_restoreModalWindows();
  129. CoUninitialize();
  130. returnValue._completeOperation(finalResult);
  131. }
  132. bool Platform::openBrowseDialog(FileDialogType type, const Path& defaultPath, const WString& filterList,
  133. Vector<Path>& paths)
  134. {
  135. AsyncOp returnValue = gCoreThread().queueReturnCommand(std::bind(&openBrowseDialogCore, type,
  136. std::cref(defaultPath), std::cref(filterList), std::ref(paths), _1),
  137. CTQF_InternalQueue | CTQF_BlockUntilComplete);
  138. return returnValue.getReturnValue<bool>();
  139. }
  140. }