bool SyncCommand::FileOpenSave(CString& path, BOOL& bWithLocals, bool bOpen, HWND hwndOwner) { HRESULT hr; bWithLocals = FALSE; // Create a new common save file dialog CComPtr<IFileDialog> pfd = NULL; hr = pfd.CoCreateInstance(bOpen ? CLSID_FileOpenDialog : CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER); if (SUCCEEDED(hr)) { // Set the dialog options DWORD dwOptions; if (SUCCEEDED(hr = pfd->GetOptions(&dwOptions))) { if (bOpen) hr = pfd->SetOptions(dwOptions | FOS_FILEMUSTEXIST | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); else { hr = pfd->SetOptions(dwOptions | FOS_OVERWRITEPROMPT | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); hr = pfd->SetFileName(CPathUtils::GetFileNameFromPath(path)); } } // Set a title if (SUCCEEDED(hr)) { CString temp; temp.LoadString(IDS_SYNC_SETTINGSFILE); CStringUtils::RemoveAccelerators(temp); pfd->SetTitle(temp); } CSelectFileFilter fileFilter(IDS_EXPORTFILTER); hr = pfd->SetFileTypes(fileFilter.GetCount(), fileFilter); if (!bOpen) { CComPtr<IFileDialogCustomize> pfdCustomize; hr = pfd.QueryInterface(&pfdCustomize); if (SUCCEEDED(hr)) { pfdCustomize->AddCheckButton(101, CString(MAKEINTRESOURCE(IDS_SYNC_INCLUDELOCAL)), FALSE); } } // Show the save/open file dialog if (SUCCEEDED(hr) && SUCCEEDED(hr = pfd->Show(hwndOwner))) { // Get the selection from the user CComPtr<IShellItem> psiResult = NULL; hr = pfd->GetResult(&psiResult); if (SUCCEEDED(hr)) { PWSTR pszPath = NULL; hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { path = CString(pszPath); if (!bOpen) { CComPtr<IFileDialogCustomize> pfdCustomize; hr = pfd.QueryInterface(&pfdCustomize); if (SUCCEEDED(hr)) { pfdCustomize->GetCheckButtonState(101, &bWithLocals); } } return true; } } } } return false; }
CBrowseFolder::retVal CBrowseFolder::Show(HWND parent, CString& path, const CString& sDefaultPath /* = CString() */) { m_sDefaultPath = sDefaultPath; if (m_sDefaultPath.IsEmpty() && !path.IsEmpty()) { while (!PathFileExists(path) && !path.IsEmpty()) { CString p = path.Left(path.ReverseFind(L'\\')); if ((p.GetLength() == 2) && (p[1] == L':')) { p += L"\\"; if (p.Compare(path) == 0) p.Empty(); } if (p.GetLength() < 2) p.Empty(); path = p; } // if the result path already contains a path, use that as the default path m_sDefaultPath = path; } // Create a new common open file dialog CComPtr<IFileOpenDialog> pfd; if (FAILED(pfd.CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER))) return CANCEL; // Set the dialog as a folder picker DWORD dwOptions; if (FAILED(pfd->GetOptions(&dwOptions))) return CANCEL; if (FAILED(pfd->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST))) return CANCEL; // Set a title TCHAR* nl = _tcschr(m_title, '\n'); if (nl) *nl = 0; pfd->SetTitle(m_title); // set the default folder CComPtr<IShellItem> psiDefault; if (FAILED(SHCreateItemFromParsingName(m_sDefaultPath, nullptr, IID_PPV_ARGS(&psiDefault)))) return CANCEL; if (FAILED(pfd->SetFolder(psiDefault))) return CANCEL; CComObjectStackEx<BrowseFolderDlgEventHandler> cbk; cbk.m_DisableCheckbox2WhenCheckbox1IsChecked = m_DisableCheckbox2WhenCheckbox1IsChecked; CComQIPtr<IFileDialogEvents> pEvents = cbk.GetUnknown(); if (!m_CheckText.IsEmpty()) { CComPtr<IFileDialogCustomize> pfdCustomize; if (FAILED(pfd.QueryInterface(&pfdCustomize))) return CANCEL; pfdCustomize->StartVisualGroup(100, L""); pfdCustomize->AddCheckButton(101, m_CheckText, FALSE); if (!m_CheckText2.IsEmpty()) pfdCustomize->AddCheckButton(102, m_CheckText2, FALSE); pfdCustomize->EndVisualGroup(); } DWORD eventsCookie; if (FAILED(pfd->Advise(pEvents, &eventsCookie))) return CANCEL; SCOPE_EXIT { pfd->Unadvise(eventsCookie); }; // Show the open file dialog if (FAILED(pfd->Show(parent))) return CANCEL; // Get the selection from the user CComPtr<IShellItem> psiResult; if (FAILED(pfd->GetResult(&psiResult))) return CANCEL; PWSTR pszPath = nullptr; if (SUCCEEDED(psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath))) { path = pszPath; CoTaskMemFree(pszPath); } CComPtr<IFileDialogCustomize> pfdCustomize; if (SUCCEEDED(pfd.QueryInterface(&pfdCustomize))) { pfdCustomize->GetCheckButtonState(101, &m_bCheck); pfdCustomize->GetCheckButtonState(102, &m_bCheck2); } return OK; }