| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 "chrome/browser/cocoa/download_request_dialog_delegate_mac.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "app/l10n_util_mac.h" | |
| 10 #include "app/message_box_flags.h" | |
| 11 #include "base/sys_string_conversions.h" | |
| 12 #include "chrome/browser/tab_contents/constrained_window.h" | |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 14 #include "grit/generated_resources.h" | |
| 15 | |
| 16 @interface SheetCallback : NSObject { | |
| 17 DownloadRequestDialogDelegateMac* target_; // weak | |
| 18 } | |
| 19 - (id)initWithTarget:(DownloadRequestDialogDelegateMac*)target; | |
| 20 - (void) alertDidEnd:(NSAlert *)alert | |
| 21 returnCode:(int)returnCode | |
| 22 contextInfo:(void *)contextInfo; | |
| 23 @end | |
| 24 | |
| 25 @implementation SheetCallback | |
| 26 - (id)initWithTarget:(DownloadRequestDialogDelegateMac*)target { | |
| 27 if ((self = [super init]) != nil) { | |
| 28 target_ = target; | |
| 29 } | |
| 30 return self; | |
| 31 } | |
| 32 - (void)alertDidEnd:(NSAlert *)alert | |
| 33 returnCode:(int)returnCode | |
| 34 contextInfo:(void *)contextInfo { | |
| 35 target_->SheetDidEnd(returnCode); | |
| 36 } | |
| 37 @end | |
| 38 | |
| 39 | |
| 40 // static | |
| 41 DownloadRequestDialogDelegate* DownloadRequestDialogDelegate::Create( | |
| 42 TabContents* tab, | |
| 43 DownloadRequestManager::TabDownloadState* host) { | |
| 44 return new DownloadRequestDialogDelegateMac(tab, host); | |
| 45 } | |
| 46 | |
| 47 DownloadRequestDialogDelegateMac::DownloadRequestDialogDelegateMac( | |
| 48 TabContents* tab, | |
| 49 DownloadRequestManager::TabDownloadState* host) | |
| 50 : DownloadRequestDialogDelegate(host), | |
| 51 ConstrainedWindowMacDelegateSystemSheet( | |
| 52 [[[SheetCallback alloc] initWithTarget:this] autorelease], | |
| 53 @selector(alertDidEnd:returnCode:contextInfo:)), | |
| 54 responded_(false) { | |
| 55 | |
| 56 // Set up alert. | |
| 57 NSAlert* alert = [[[NSAlert alloc] init] autorelease]; | |
| 58 [alert addButtonWithTitle: | |
| 59 l10n_util::GetNSStringWithFixup(IDS_MULTI_DOWNLOAD_WARNING_ALLOW)]; | |
| 60 NSButton* denyButton = [alert addButtonWithTitle: | |
| 61 l10n_util::GetNSStringWithFixup(IDS_MULTI_DOWNLOAD_WARNING_DENY)]; | |
| 62 [alert setMessageText: | |
| 63 l10n_util::GetNSStringWithFixup(IDS_MULTI_DOWNLOAD_WARNING)]; | |
| 64 [alert setAlertStyle:NSInformationalAlertStyle]; | |
| 65 | |
| 66 // Cocoa automatically gives ESC as a key equivalent to buttons with the text | |
| 67 // "Cancel". Since we use a different text, we have to set this ourselves. | |
| 68 NSString* escapeKey = @"\e"; | |
| 69 [denyButton setKeyEquivalent:escapeKey]; | |
| 70 | |
| 71 set_sheet(alert); | |
| 72 | |
| 73 // Display alert in a per-tab sheet. | |
| 74 window_ = tab->CreateConstrainedDialog(this); | |
| 75 } | |
| 76 | |
| 77 void DownloadRequestDialogDelegateMac::CloseWindow() { | |
| 78 window_->CloseConstrainedWindow(); | |
| 79 } | |
| 80 | |
| 81 void DownloadRequestDialogDelegateMac::DeleteDelegate() { | |
| 82 if (is_sheet_open()) { | |
| 83 // Close sheet if it's still open. | |
| 84 [NSApp endSheet:[(NSAlert*)sheet() window]]; // calls SheetDidEnd(). | |
| 85 DCHECK(responded_); | |
| 86 } | |
| 87 if (!responded_) { | |
| 88 // Happens if the sheet was never visible. | |
| 89 responded_ = true; | |
| 90 DoCancel(); | |
| 91 } | |
| 92 DCHECK(!host_); | |
| 93 delete this; | |
| 94 } | |
| 95 | |
| 96 void DownloadRequestDialogDelegateMac::SheetDidEnd(int returnCode) { | |
| 97 DCHECK(!responded_); | |
| 98 if (returnCode == NSAlertFirstButtonReturn) { | |
| 99 DoAccept(); | |
| 100 } else { | |
| 101 DoCancel(); | |
| 102 } | |
| 103 responded_ = true; | |
| 104 } | |
| OLD | NEW |