Chromium Code Reviews| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| 86 OPENFILENAME* OpenFileName::Get() { | 105 OPENFILENAME* OpenFileName::Get() { |
| 87 return &openfilename_; | 106 return &openfilename_; |
| 88 } | 107 } |
| 89 | 108 |
| 109 const OPENFILENAME* OpenFileName::Get() const { | |
| 110 return &openfilename_; | |
|
sky
2014/07/31 20:47:46
nit: this is trivial enough that I would inline it
erikwright (departed)
2014/08/01 13:53:15
Done.
| |
| 111 } | |
| 112 | |
| 113 // static | |
| 114 void OpenFileName::SetResult(const base::FilePath& directory, | |
| 115 const std::vector<base::FilePath>& filenames, | |
| 116 OPENFILENAME* openfilename) { | |
| 117 base::string16 filename_value; | |
| 118 if (filenames.size() == 1) { | |
| 119 filename_value = directory.Append(filenames[0]).value(); | |
| 120 } else { | |
| 121 filename_value = directory.value(); | |
| 122 filename_value.push_back(0); | |
| 123 for (std::vector<base::FilePath>::const_iterator it = filenames.begin(); | |
| 124 it != filenames.end(); | |
| 125 ++it) { | |
| 126 filename_value.append(it->value()); | |
| 127 filename_value.push_back(0); | |
| 128 } | |
| 129 } | |
| 130 if (filename_value.size() + 1 < openfilename->nMaxFile) { | |
|
sky
2014/07/31 20:47:46
What about the else case here?
erikwright (departed)
2014/08/01 13:53:15
Done. Also added a test case.
| |
| 131 // Because the result has embedded nulls, we must memcpy. | |
| 132 memcpy(openfilename->lpstrFile, | |
| 133 filename_value.c_str(), | |
| 134 (filename_value.size() + 1) * sizeof(filename_value[0])); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 std::vector<Tuple2<base::string16, base::string16> > OpenFileName::GetFilters( | |
| 140 const OPENFILENAME* openfilename) { | |
| 141 std::vector<Tuple2<base::string16, base::string16> > filters; | |
| 142 | |
| 143 const base::char16* name = openfilename->lpstrFilter; | |
|
sky
2014/07/31 20:47:46
Assigning 'name' to 'filter' is rather confusing.
erikwright (departed)
2014/08/01 13:53:15
Done.
| |
| 144 if (!name) | |
| 145 return filters; | |
| 146 | |
| 147 while (*name) { | |
| 148 const base::char16* name_end = name; | |
| 149 while (*name_end) { | |
|
sky
2014/07/31 20:47:46
nit: no {} here and 154
erikwright (departed)
2014/08/01 13:53:15
Done.
| |
| 150 ++name_end; | |
| 151 } | |
| 152 const base::char16* value = name_end + 1; | |
| 153 const base::char16* value_end = value; | |
| 154 while (*value_end) { | |
| 155 ++value_end; | |
| 156 } | |
| 157 filters.push_back(MakeTuple(base::string16(name, name_end), | |
| 158 base::string16(value, value_end))); | |
| 159 name = value_end + 1; | |
| 160 } | |
| 161 | |
| 162 return filters; | |
| 163 } | |
| 164 | |
| 90 } // namespace win | 165 } // namespace win |
| 91 } // namespace ui | 166 } // namespace ui |
| OLD | NEW |