| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/win/open_file_name_win.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/strings/string_util.h" |
| 9 |
| 10 namespace ui { |
| 11 namespace win { |
| 12 |
| 13 OpenFileName::OpenFileName(HWND parent_window, DWORD flags) { |
| 14 ::ZeroMemory(&openfilename_, sizeof(openfilename_)); |
| 15 openfilename_.lStructSize = sizeof(openfilename_); |
| 16 |
| 17 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, |
| 18 // The lpstrFile Buffer MUST be NULL Terminated. |
| 19 filename_buffer_[0] = 0; |
| 20 openfilename_.lpstrFile = filename_buffer_; |
| 21 openfilename_.nMaxFile = arraysize(filename_buffer_); |
| 22 |
| 23 openfilename_.Flags = flags; |
| 24 openfilename_.hwndOwner = parent_window; |
| 25 } |
| 26 |
| 27 OpenFileName::~OpenFileName() { |
| 28 } |
| 29 |
| 30 void OpenFileName::SetInitialSelection(const base::FilePath& initial_directory, |
| 31 const base::FilePath& initial_filename) { |
| 32 // First reset to the default case. |
| 33 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, |
| 34 // The lpstrFile Buffer MUST be NULL Terminated. |
| 35 filename_buffer_[0] = 0; |
| 36 openfilename_.lpstrFile = filename_buffer_; |
| 37 openfilename_.nMaxFile = arraysize(filename_buffer_); |
| 38 openfilename_.lpstrInitialDir = NULL; |
| 39 initial_directory_buffer_.clear(); |
| 40 |
| 41 if (initial_directory.empty()) |
| 42 return; |
| 43 |
| 44 initial_directory_buffer_ = initial_directory.value(); |
| 45 openfilename_.lpstrInitialDir = initial_directory_buffer_.c_str(); |
| 46 |
| 47 if (initial_filename.empty()) |
| 48 return; |
| 49 |
| 50 // The filename is ignored if no initial directory is supplied. |
| 51 base::wcslcpy(filename_buffer_, |
| 52 initial_filename.value().c_str(), |
| 53 arraysize(filename_buffer_)); |
| 54 } |
| 55 |
| 56 base::FilePath OpenFileName::GetSingleResult() { |
| 57 base::FilePath directory; |
| 58 std::vector<base::FilePath> filenames; |
| 59 GetResult(&directory, &filenames); |
| 60 if (filenames.size() != 1) |
| 61 return base::FilePath(); |
| 62 return directory.Append(filenames[0]); |
| 63 } |
| 64 |
| 65 void OpenFileName::GetResult(base::FilePath* directory, |
| 66 std::vector<base::FilePath>* filenames) { |
| 67 DCHECK(filenames->empty()); |
| 68 const wchar_t* selection = openfilename_.lpstrFile; |
| 69 while (*selection) { // Empty string indicates end of list. |
| 70 filenames->push_back(base::FilePath(selection)); |
| 71 // Skip over filename and null-terminator. |
| 72 selection += filenames->back().value().length() + 1; |
| 73 } |
| 74 if (filenames->size() == 1) { |
| 75 // When there is one file, it contains the path and filename. |
| 76 *directory = (*filenames)[0].DirName(); |
| 77 (*filenames)[0] = (*filenames)[0].BaseName(); |
| 78 } else if (filenames->size() > 1) { |
| 79 // Otherwise, the first string is the path, and the remainder are |
| 80 // filenames. |
| 81 *directory = (*filenames)[0]; |
| 82 filenames->erase(filenames->begin()); |
| 83 } |
| 84 } |
| 85 |
| 86 OPENFILENAME* OpenFileName::Get() { |
| 87 return &openfilename_; |
| 88 } |
| 89 |
| 90 } // namespace win |
| 91 } // namespace ui |
| OLD | NEW |