OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/shell_dialogs.h" | 5 #include "chrome/browser/shell_dialogs.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <Commdlg.h> | 8 #include <Commdlg.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 | 10 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // Implementation of SelectFileDialog that shows a Windows common dialog for | 185 // Implementation of SelectFileDialog that shows a Windows common dialog for |
186 // choosing a file or folder. | 186 // choosing a file or folder. |
187 class SelectFileDialogImpl : public SelectFileDialog, | 187 class SelectFileDialogImpl : public SelectFileDialog, |
188 public BaseShellDialogImpl { | 188 public BaseShellDialogImpl { |
189 public: | 189 public: |
190 explicit SelectFileDialogImpl(Listener* listener); | 190 explicit SelectFileDialogImpl(Listener* listener); |
191 virtual ~SelectFileDialogImpl(); | 191 virtual ~SelectFileDialogImpl(); |
192 | 192 |
193 // SelectFileDialog implementation: | 193 // SelectFileDialog implementation: |
194 virtual void SelectFile(Type type, const std::wstring& title, | 194 virtual void SelectFile(Type type, const std::wstring& title, |
195 const std::wstring& default_path, HWND owning_hwnd, | 195 const std::wstring& default_path, |
| 196 const std::wstring& filter, |
| 197 HWND owning_hwnd, |
196 void* params); | 198 void* params); |
197 virtual bool IsRunning(HWND owning_hwnd) const; | 199 virtual bool IsRunning(HWND owning_hwnd) const; |
198 virtual void ListenerDestroyed(); | 200 virtual void ListenerDestroyed(); |
199 | 201 |
200 private: | 202 private: |
201 // Shows the file selection dialog modal to |owner| and calls the result | 203 // Shows the file selection dialog modal to |owner| and calls the result |
202 // back on the ui thread. Run on the dialog thread. | 204 // back on the ui thread. Run on the dialog thread. |
203 void ExecuteSelectFile(Type type, | 205 void ExecuteSelectFile(Type type, |
204 const std::wstring& title, | 206 const std::wstring& title, |
205 const std::wstring& default_path, | 207 const std::wstring& default_path, |
| 208 const std::wstring& filter, |
206 RunState run_state, | 209 RunState run_state, |
207 void* params); | 210 void* params); |
208 | 211 |
209 // Notifies the listener that a folder was chosen. Run on the ui thread. | 212 // Notifies the listener that a folder was chosen. Run on the ui thread. |
210 void FileSelected(const std::wstring& path, void* params, RunState run_state); | 213 void FileSelected(const std::wstring& path, void* params, RunState run_state); |
211 | 214 |
212 // Notifies the listener that no file was chosen (the action was canceled). | 215 // Notifies the listener that no file was chosen (the action was canceled). |
213 // Run on the ui thread. | 216 // Run on the ui thread. |
214 void FileNotSelected(void* params, RunState run_state); | 217 void FileNotSelected(void* params, RunState run_state); |
215 | 218 |
216 // Runs a Folder selection dialog box, passes back the selected folder in | 219 // Runs a Folder selection dialog box, passes back the selected folder in |
217 // |path| and returns true if the user clicks OK. If the user cancels the | 220 // |path| and returns true if the user clicks OK. If the user cancels the |
218 // dialog box the value in |path| is not modified and returns false. |title| | 221 // dialog box the value in |path| is not modified and returns false. |title| |
219 // is the user-supplied title text to show for the dialog box. Run on the | 222 // is the user-supplied title text to show for the dialog box. Run on the |
220 // dialog thread. | 223 // dialog thread. |
221 bool RunSelectFolderDialog(const std::wstring& title, | 224 bool RunSelectFolderDialog(const std::wstring& title, |
222 HWND owner, | 225 HWND owner, |
223 std::wstring* path); | 226 std::wstring* path); |
224 | 227 |
225 // Runs an Open file dialog box, with similar semantics for input paramaters | 228 // Runs an Open file dialog box, with similar semantics for input paramaters |
226 // as RunSelectFolderDialog. | 229 // as RunSelectFolderDialog. |
227 bool RunOpenFileDialog(const std::wstring& title, | 230 bool RunOpenFileDialog(const std::wstring& title, |
| 231 const std::wstring& filters, |
228 HWND owner, | 232 HWND owner, |
229 std::wstring* path); | 233 std::wstring* path); |
230 | 234 |
231 // The listener to be notified of selection completion. | 235 // The listener to be notified of selection completion. |
232 Listener* listener_; | 236 Listener* listener_; |
233 | 237 |
234 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); | 238 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); |
235 }; | 239 }; |
236 | 240 |
237 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener) | 241 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener) |
238 : listener_(listener), | 242 : listener_(listener), |
239 BaseShellDialogImpl() { | 243 BaseShellDialogImpl() { |
240 } | 244 } |
241 | 245 |
242 SelectFileDialogImpl::~SelectFileDialogImpl() { | 246 SelectFileDialogImpl::~SelectFileDialogImpl() { |
243 } | 247 } |
244 | 248 |
245 void SelectFileDialogImpl::SelectFile(Type type, | 249 void SelectFileDialogImpl::SelectFile(Type type, |
246 const std::wstring& title, | 250 const std::wstring& title, |
247 const std::wstring& default_path, | 251 const std::wstring& default_path, |
| 252 const std::wstring& filter, |
248 HWND owner, | 253 HWND owner, |
249 void* params) { | 254 void* params) { |
250 RunState run_state = BeginRun(owner); | 255 RunState run_state = BeginRun(owner); |
251 run_state.dialog_thread->message_loop()->PostTask(FROM_HERE, | 256 run_state.dialog_thread->message_loop()->PostTask(FROM_HERE, |
252 NewRunnableMethod(this, &SelectFileDialogImpl::ExecuteSelectFile, type, | 257 NewRunnableMethod(this, &SelectFileDialogImpl::ExecuteSelectFile, type, |
253 title, default_path, run_state, params)); | 258 title, default_path, filter, run_state, params)); |
254 } | 259 } |
255 | 260 |
256 bool SelectFileDialogImpl::IsRunning(HWND owning_hwnd) const { | 261 bool SelectFileDialogImpl::IsRunning(HWND owning_hwnd) const { |
257 return listener_ && IsRunningDialogForOwner(owning_hwnd); | 262 return listener_ && IsRunningDialogForOwner(owning_hwnd); |
258 } | 263 } |
259 | 264 |
260 void SelectFileDialogImpl::ListenerDestroyed() { | 265 void SelectFileDialogImpl::ListenerDestroyed() { |
261 // Our associated listener has gone away, so we shouldn't call back to it if | 266 // Our associated listener has gone away, so we shouldn't call back to it if |
262 // our worker thread returns after the listener is dead. | 267 // our worker thread returns after the listener is dead. |
263 listener_ = NULL; | 268 listener_ = NULL; |
264 } | 269 } |
265 | 270 |
266 void SelectFileDialogImpl::ExecuteSelectFile(Type type, | 271 void SelectFileDialogImpl::ExecuteSelectFile( |
267 const std::wstring& title, | 272 Type type, |
268 const std::wstring& default_path, | 273 const std::wstring& title, |
269 RunState run_state, | 274 const std::wstring& default_path, |
270 void* params) { | 275 const std::wstring& filter, |
| 276 RunState run_state, |
| 277 void* params) { |
271 std::wstring path = default_path; | 278 std::wstring path = default_path; |
272 bool success = false; | 279 bool success = false; |
273 if (type == SELECT_FOLDER) { | 280 if (type == SELECT_FOLDER) { |
274 success = RunSelectFolderDialog(title, run_state.owner, &path); | 281 success = RunSelectFolderDialog(title, run_state.owner, &path); |
275 } else if (type == SELECT_SAVEAS_FILE) { | 282 } else if (type == SELECT_SAVEAS_FILE) { |
276 success = win_util::SaveFileAs(run_state.owner, default_path, &path); | 283 success = win_util::SaveFileAs(run_state.owner, default_path, &path); |
277 DisableOwner(run_state.owner); | 284 DisableOwner(run_state.owner); |
278 } else if (type == SELECT_OPEN_FILE) { | 285 } else if (type == SELECT_OPEN_FILE) { |
279 success = RunOpenFileDialog(title, run_state.owner, &path); | 286 success = RunOpenFileDialog(title, filter, run_state.owner, &path); |
280 } | 287 } |
281 | 288 |
282 if (success) { | 289 if (success) { |
283 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 290 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
284 &SelectFileDialogImpl::FileSelected, path, params, run_state)); | 291 &SelectFileDialogImpl::FileSelected, path, params, run_state)); |
285 } else { | 292 } else { |
286 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 293 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
287 &SelectFileDialogImpl::FileNotSelected, params, run_state)); | 294 &SelectFileDialogImpl::FileNotSelected, params, run_state)); |
288 } | 295 } |
289 } | 296 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 // According to MSDN, win2000 will not resolve shortcuts, so we do it | 331 // According to MSDN, win2000 will not resolve shortcuts, so we do it |
325 // ourself. | 332 // ourself. |
326 file_util::ResolveShortcut(path); | 333 file_util::ResolveShortcut(path); |
327 return true; | 334 return true; |
328 } | 335 } |
329 CoTaskMemFree(list); | 336 CoTaskMemFree(list); |
330 } | 337 } |
331 return false; | 338 return false; |
332 } | 339 } |
333 | 340 |
334 bool SelectFileDialogImpl::RunOpenFileDialog(const std::wstring& title, | 341 bool SelectFileDialogImpl::RunOpenFileDialog( |
335 HWND owner, | 342 const std::wstring& title, |
336 std::wstring* path) { | 343 const std::wstring& filter, |
| 344 HWND owner, |
| 345 std::wstring* path) { |
337 OPENFILENAME ofn; | 346 OPENFILENAME ofn; |
338 // We must do this otherwise the ofn's FlagsEx may be initialized to random | 347 // We must do this otherwise the ofn's FlagsEx may be initialized to random |
339 // junk in release builds which can cause the Places Bar not to show up! | 348 // junk in release builds which can cause the Places Bar not to show up! |
340 ZeroMemory(&ofn, sizeof(ofn)); | 349 ZeroMemory(&ofn, sizeof(ofn)); |
341 ofn.lStructSize = sizeof(ofn); | 350 ofn.lStructSize = sizeof(ofn); |
342 ofn.hwndOwner = owner; | 351 ofn.hwndOwner = owner; |
343 | 352 |
344 wchar_t filename[MAX_PATH]; | 353 wchar_t filename[MAX_PATH]; |
345 base::wcslcpy(filename, path->c_str(), arraysize(filename)); | 354 base::wcslcpy(filename, path->c_str(), arraysize(filename)); |
346 | 355 |
347 ofn.lpstrFile = filename; | 356 ofn.lpstrFile = filename; |
348 ofn.nMaxFile = MAX_PATH; | 357 ofn.nMaxFile = MAX_PATH; |
349 // We use OFN_NOCHANGEDIR so that the user can rename or delete the directory | 358 // We use OFN_NOCHANGEDIR so that the user can rename or delete the directory |
350 // without having to close Chrome first. | 359 // without having to close Chrome first. |
351 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; | 360 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; |
352 | 361 |
353 // TODO(beng): (http://b/issue?id=1126563) edit the filter options in the | 362 if (!filter.empty()) { |
354 // dropdown list. | 363 ofn.lpstrFilter = filter.c_str(); |
| 364 } |
355 bool success = !!GetOpenFileName(&ofn); | 365 bool success = !!GetOpenFileName(&ofn); |
356 DisableOwner(owner); | 366 DisableOwner(owner); |
357 if (success) | 367 if (success) |
358 *path = filename; | 368 *path = filename; |
359 return success; | 369 return success; |
360 } | 370 } |
361 | 371 |
362 // static | 372 // static |
363 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { | 373 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { |
364 return new SelectFileDialogImpl(listener); | 374 return new SelectFileDialogImpl(listener); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 void SelectFontDialogImpl::FontNotSelected(void* params, RunState run_state) { | 530 void SelectFontDialogImpl::FontNotSelected(void* params, RunState run_state) { |
521 if (listener_) | 531 if (listener_) |
522 listener_->FontSelectionCanceled(params); | 532 listener_->FontSelectionCanceled(params); |
523 EndRun(run_state); | 533 EndRun(run_state); |
524 } | 534 } |
525 | 535 |
526 // static | 536 // static |
527 SelectFontDialog* SelectFontDialog::Create(Listener* listener) { | 537 SelectFontDialog* SelectFontDialog::Create(Listener* listener) { |
528 return new SelectFontDialogImpl(listener); | 538 return new SelectFontDialogImpl(listener); |
529 } | 539 } |
530 | |
OLD | NEW |