| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/win/open_file_name_win.h" | 5 #include "ui/base/win/open_file_name_win.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 | 9 |
| 10 namespace ui { | 10 namespace ui { |
| 11 namespace win { | 11 namespace win { |
| 12 | 12 |
| 13 OpenFileName::OpenFileName(HWND parent_window, DWORD flags) { | 13 OpenFileName::OpenFileName(HWND parent_window, DWORD flags) { |
| 14 ::ZeroMemory(&openfilename_, sizeof(openfilename_)); | 14 ::ZeroMemory(&openfilename_, sizeof(openfilename_)); |
| 15 openfilename_.lStructSize = sizeof(openfilename_); | 15 openfilename_.lStructSize = sizeof(openfilename_); |
| 16 | 16 |
| 17 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, | 17 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, |
| 18 // The lpstrFile Buffer MUST be NULL Terminated. | 18 // The lpstrFile Buffer MUST be NULL Terminated. |
| 19 filename_buffer_[0] = 0; | 19 filename_buffer_[0] = 0; |
| 20 openfilename_.lpstrFile = filename_buffer_; | 20 openfilename_.lpstrFile = filename_buffer_; |
| 21 openfilename_.nMaxFile = arraysize(filename_buffer_); | 21 openfilename_.nMaxFile = arraysize(filename_buffer_); |
| 22 | 22 |
| 23 openfilename_.Flags = flags; | 23 openfilename_.Flags = flags; |
| 24 openfilename_.hwndOwner = parent_window; | 24 openfilename_.hwndOwner = parent_window; |
| 25 } | 25 } |
| 26 | 26 |
| 27 OpenFileName::~OpenFileName() { | 27 OpenFileName::~OpenFileName() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 void OpenFileName::SetFilters( |
| 31 const std::vector<Tuple2<base::string16, base::string16> >& filters) { |
| 32 openfilename_.lpstrFilter = NULL; |
| 33 filter_buffer_.clear(); |
| 34 if (filters.empty()) |
| 35 return; |
| 36 for (std::vector<Tuple2<base::string16, base::string16> >::const_iterator |
| 37 it = filters.begin(); |
| 38 it != filters.end(); |
| 39 ++it) { |
| 40 filter_buffer_.append(it->a); |
| 41 filter_buffer_.push_back(0); |
| 42 filter_buffer_.append(it->b); |
| 43 filter_buffer_.push_back(0); |
| 44 } |
| 45 filter_buffer_.push_back(0); |
| 46 openfilename_.lpstrFilter = filter_buffer_.c_str(); |
| 47 } |
| 48 |
| 30 void OpenFileName::SetInitialSelection(const base::FilePath& initial_directory, | 49 void OpenFileName::SetInitialSelection(const base::FilePath& initial_directory, |
| 31 const base::FilePath& initial_filename) { | 50 const base::FilePath& initial_filename) { |
| 32 // First reset to the default case. | 51 // First reset to the default case. |
| 33 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, | 52 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, |
| 34 // The lpstrFile Buffer MUST be NULL Terminated. | 53 // The lpstrFile Buffer MUST be NULL Terminated. |
| 35 filename_buffer_[0] = 0; | 54 filename_buffer_[0] = 0; |
| 36 openfilename_.lpstrFile = filename_buffer_; | 55 openfilename_.lpstrFile = filename_buffer_; |
| 37 openfilename_.nMaxFile = arraysize(filename_buffer_); | 56 openfilename_.nMaxFile = arraysize(filename_buffer_); |
| 38 openfilename_.lpstrInitialDir = NULL; | 57 openfilename_.lpstrInitialDir = NULL; |
| 39 initial_directory_buffer_.clear(); | 58 initial_directory_buffer_.clear(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 *directory = (*filenames)[0].DirName(); | 95 *directory = (*filenames)[0].DirName(); |
| 77 (*filenames)[0] = (*filenames)[0].BaseName(); | 96 (*filenames)[0] = (*filenames)[0].BaseName(); |
| 78 } else if (filenames->size() > 1) { | 97 } else if (filenames->size() > 1) { |
| 79 // Otherwise, the first string is the path, and the remainder are | 98 // Otherwise, the first string is the path, and the remainder are |
| 80 // filenames. | 99 // filenames. |
| 81 *directory = (*filenames)[0]; | 100 *directory = (*filenames)[0]; |
| 82 filenames->erase(filenames->begin()); | 101 filenames->erase(filenames->begin()); |
| 83 } | 102 } |
| 84 } | 103 } |
| 85 | 104 |
| 105 // static |
| 106 void OpenFileName::SetResult(const base::FilePath& directory, |
| 107 const std::vector<base::FilePath>& filenames, |
| 108 OPENFILENAME* openfilename) { |
| 109 base::string16 filename_value; |
| 110 if (filenames.size() == 1) { |
| 111 filename_value = directory.Append(filenames[0]).value(); |
| 112 } else { |
| 113 filename_value = directory.value(); |
| 114 filename_value.push_back(0); |
| 115 for (std::vector<base::FilePath>::const_iterator it = filenames.begin(); |
| 116 it != filenames.end(); |
| 117 ++it) { |
| 118 filename_value.append(it->value()); |
| 119 filename_value.push_back(0); |
| 120 } |
| 121 } |
| 122 if (filename_value.size() + 1 < openfilename->nMaxFile) { |
| 123 // Because the result has embedded nulls, we must memcpy. |
| 124 memcpy(openfilename->lpstrFile, |
| 125 filename_value.c_str(), |
| 126 (filename_value.size() + 1) * sizeof(filename_value[0])); |
| 127 } else if (openfilename->nMaxFile) { |
| 128 openfilename->lpstrFile[0] = 0; |
| 129 } |
| 130 } |
| 131 |
| 132 // static |
| 133 std::vector<Tuple2<base::string16, base::string16> > OpenFileName::GetFilters( |
| 134 const OPENFILENAME* openfilename) { |
| 135 std::vector<Tuple2<base::string16, base::string16> > filters; |
| 136 |
| 137 const base::char16* display_string = openfilename->lpstrFilter; |
| 138 if (!display_string) |
| 139 return filters; |
| 140 |
| 141 while (*display_string) { |
| 142 const base::char16* display_string_end = display_string; |
| 143 while (*display_string_end) |
| 144 ++display_string_end; |
| 145 const base::char16* pattern = display_string_end + 1; |
| 146 const base::char16* pattern_end = pattern; |
| 147 while (*pattern_end) |
| 148 ++pattern_end; |
| 149 filters.push_back( |
| 150 MakeTuple(base::string16(display_string, display_string_end), |
| 151 base::string16(pattern, pattern_end))); |
| 152 display_string = pattern_end + 1; |
| 153 } |
| 154 |
| 155 return filters; |
| 156 } |
| 157 |
| 86 } // namespace win | 158 } // namespace win |
| 87 } // namespace ui | 159 } // namespace ui |
| OLD | NEW |