| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shell_dialogs/select_file_dialog_mac.h" | 5 #include "ui/shell_dialogs/select_file_dialog_mac.h" |
| 6 | 6 |
| 7 #include <CoreServices/CoreServices.h> | 7 #include <CoreServices/CoreServices.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/mac/bundle_locations.h" | 15 #include "base/mac/bundle_locations.h" |
| 16 #include "base/mac/foundation_util.h" | 16 #include "base/mac/foundation_util.h" |
| 17 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 18 #import "base/mac/sdk_forward_declarations.h" |
| 18 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
| 21 #import "ui/base/cocoa/nib_loading.h" | 22 #import "ui/base/cocoa/nib_loading.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" | 23 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 #include "ui/strings/grit/ui_strings.h" | 24 #include "ui/strings/grit/ui_strings.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 28 const int kAccessoryViewHorizontalMargin = 20; |
| 29 const int kAccessoryViewVerticalMargin = 20; |
| 27 const int kFileTypePopupTag = 1234; | 30 const int kFileTypePopupTag = 1234; |
| 28 | 31 |
| 29 CFStringRef CreateUTIFromExtension(const base::FilePath::StringType& ext) { | 32 CFStringRef CreateUTIFromExtension(const base::FilePath::StringType& ext) { |
| 30 base::ScopedCFTypeRef<CFStringRef> ext_cf(base::SysUTF8ToCFStringRef(ext)); | 33 base::ScopedCFTypeRef<CFStringRef> ext_cf(base::SysUTF8ToCFStringRef(ext)); |
| 31 return UTTypeCreatePreferredIdentifierForTag( | 34 return UTTypeCreatePreferredIdentifierForTag( |
| 32 kUTTagClassFilenameExtension, ext_cf.get(), NULL); | 35 kUTTagClassFilenameExtension, ext_cf.get(), NULL); |
| 33 } | 36 } |
| 34 | 37 |
| 35 NSString* GetDescriptionFromExtension(const base::FilePath::StringType& ext) { | 38 NSString* GetDescriptionFromExtension(const base::FilePath::StringType& ext) { |
| 36 base::ScopedCFTypeRef<CFStringRef> uti(CreateUTIFromExtension(ext)); | 39 base::ScopedCFTypeRef<CFStringRef> uti(CreateUTIFromExtension(ext)); |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 // through it. | 261 // through it. |
| 259 std::vector<NSSavePanel*> panels; | 262 std::vector<NSSavePanel*> panels; |
| 260 for (const auto& value : dialog_data_map_) | 263 for (const auto& value : dialog_data_map_) |
| 261 panels.push_back(value.first); | 264 panels.push_back(value.first); |
| 262 | 265 |
| 263 for (const auto& panel : panels) | 266 for (const auto& panel : panels) |
| 264 [panel cancel:panel]; | 267 [panel cancel:panel]; |
| 265 } | 268 } |
| 266 | 269 |
| 267 // static | 270 // static |
| 271 NSView* SelectFileDialogImpl::CreateAccessoryView() { |
| 272 base::scoped_nsobject<NSTextField> textfield([[NSTextField alloc] init]); |
| 273 [textfield |
| 274 setStringValue:l10n_util::GetNSString(IDS_SAVE_PAGE_FILE_FORMAT_PROMPT)]; |
| 275 [textfield setDrawsBackground:NO]; |
| 276 [textfield setEditable:NO]; |
| 277 [textfield setSelectable:NO]; |
| 278 [textfield setBezeled:NO]; |
| 279 [textfield setFont:[NSFont systemFontOfSize:0]]; |
| 280 [textfield setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 281 |
| 282 base::scoped_nsobject<NSPopUpButton> popup([[NSPopUpButton alloc] init]); |
| 283 [popup setTag:kFileTypePopupTag]; |
| 284 [popup setAlignment:NSNaturalTextAlignment]; |
| 285 [popup setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 286 |
| 287 NSStackView* accessory_view = |
| 288 [NSStackView stackViewWithViews:@[ textfield, popup ]]; |
| 289 [accessory_view |
| 290 setEdgeInsets:NSEdgeInsetsMake(kAccessoryViewVerticalMargin, |
| 291 kAccessoryViewHorizontalMargin, |
| 292 kAccessoryViewVerticalMargin, |
| 293 kAccessoryViewHorizontalMargin)]; |
| 294 return accessory_view; |
| 295 } |
| 296 |
| 297 // static |
| 268 base::scoped_nsobject<ExtensionDropdownHandler> | 298 base::scoped_nsobject<ExtensionDropdownHandler> |
| 269 SelectFileDialogImpl::SetAccessoryView( | 299 SelectFileDialogImpl::SetAccessoryView( |
| 270 NSSavePanel* dialog, | 300 NSSavePanel* dialog, |
| 271 const FileTypeInfo* file_types, | 301 const FileTypeInfo* file_types, |
| 272 int file_type_index, | 302 int file_type_index, |
| 273 const base::FilePath::StringType& default_extension) { | 303 const base::FilePath::StringType& default_extension) { |
| 274 DCHECK(file_types); | 304 DCHECK(file_types); |
| 275 NSView* accessory_view = ui::GetViewFromNib(@"SaveAccessoryView"); | |
| 276 if (!accessory_view) | |
| 277 return base::scoped_nsobject<ExtensionDropdownHandler>(); | |
| 278 [dialog setAccessoryView:accessory_view]; | |
| 279 | 305 |
| 306 NSView* accessory_view = CreateAccessoryView(); |
| 280 NSPopUpButton* popup = [accessory_view viewWithTag:kFileTypePopupTag]; | 307 NSPopUpButton* popup = [accessory_view viewWithTag:kFileTypePopupTag]; |
| 281 DCHECK(popup); | 308 DCHECK(popup); |
| 282 | 309 |
| 283 // Create an array with each item corresponding to an array of different | 310 // Create an array with each item corresponding to an array of different |
| 284 // extensions in an extension group. | 311 // extensions in an extension group. |
| 285 NSMutableArray* file_type_lists = [NSMutableArray array]; | 312 NSMutableArray* file_type_lists = [NSMutableArray array]; |
| 286 int default_extension_index = -1; | 313 int default_extension_index = -1; |
| 287 for (size_t i = 0; i < file_types->extensions.size(); ++i) { | 314 for (size_t i = 0; i < file_types->extensions.size(); ++i) { |
| 288 const std::vector<base::FilePath::StringType>& ext_list = | 315 const std::vector<base::FilePath::StringType>& ext_list = |
| 289 file_types->extensions[i]; | 316 file_types->extensions[i]; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 } else if (!default_extension.empty() && default_extension_index != -1) { | 375 } else if (!default_extension.empty() && default_extension_index != -1) { |
| 349 [popup selectItemAtIndex:default_extension_index]; | 376 [popup selectItemAtIndex:default_extension_index]; |
| 350 [dialog | 377 [dialog |
| 351 setAllowedFileTypes:@[ base::SysUTF8ToNSString(default_extension) ]]; | 378 setAllowedFileTypes:@[ base::SysUTF8ToNSString(default_extension) ]]; |
| 352 } else { | 379 } else { |
| 353 // Select the first item. | 380 // Select the first item. |
| 354 [popup selectItemAtIndex:0]; | 381 [popup selectItemAtIndex:0]; |
| 355 [handler popupAction:popup]; | 382 [handler popupAction:popup]; |
| 356 } | 383 } |
| 357 | 384 |
| 385 // This should not have been needed but is required for correct layout on OSX |
| 386 // 10.9. |
| 387 [accessory_view layoutSubtreeIfNeeded]; |
| 388 [dialog setAccessoryView:accessory_view]; |
| 358 return handler; | 389 return handler; |
| 359 } | 390 } |
| 360 | 391 |
| 361 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { | 392 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { |
| 362 return hasMultipleFileTypeChoices_; | 393 return hasMultipleFileTypeChoices_; |
| 363 } | 394 } |
| 364 | 395 |
| 365 SelectFileDialog* CreateSelectFileDialog(SelectFileDialog::Listener* listener, | 396 SelectFileDialog* CreateSelectFileDialog(SelectFileDialog::Listener* listener, |
| 366 SelectFilePolicy* policy) { | 397 SelectFilePolicy* policy) { |
| 367 return new SelectFileDialogImpl(listener, policy); | 398 return new SelectFileDialogImpl(listener, policy); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 // For save dialogs, this causes the first item in the allowedFileTypes | 473 // For save dialogs, this causes the first item in the allowedFileTypes |
| 443 // array to be used as the extension for the save panel. | 474 // array to be used as the extension for the save panel. |
| 444 [dialog_ setAllowedFileTypes:[fileTypeLists_ objectAtIndex:index]]; | 475 [dialog_ setAllowedFileTypes:[fileTypeLists_ objectAtIndex:index]]; |
| 445 } else { | 476 } else { |
| 446 // The user selected "All files" option. | 477 // The user selected "All files" option. |
| 447 [dialog_ setAllowedFileTypes:nil]; | 478 [dialog_ setAllowedFileTypes:nil]; |
| 448 } | 479 } |
| 449 } | 480 } |
| 450 | 481 |
| 451 @end | 482 @end |
| OLD | NEW |