| 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 "chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h" | 5 #include "chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_nsobject.h" | 8 #include "base/memory/scoped_nsobject.h" |
| 9 #include "chrome/browser/ui/browser_dialogs.h" | 9 #include "chrome/browser/ui/browser_dialogs.h" |
| 10 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" | 10 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" |
| 11 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac2.h" | 11 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac2.h" |
| 12 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h" | 12 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 14 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" | 14 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "ui/base/l10n/l10n_util_mac.h" | 16 #include "ui/base/l10n/l10n_util_mac.h" |
| 17 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
| 18 | 18 |
| 19 // static |
| 20 TabModalConfirmDialog* TabModalConfirmDialog::Create( |
| 21 TabModalConfirmDialogDelegate* delegate, |
| 22 TabContents* tab_contents) { |
| 23 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 24 switches::kEnableFramelessConstrainedDialogs)) { |
| 25 // Deletes itself when closed. |
| 26 return new TabModalConfirmDialogMac2(delegate, tab_contents); |
| 27 } |
| 28 // Deletes itself when closed. |
| 29 return new TabModalConfirmDialogMac(delegate, tab_contents); |
| 30 } |
| 31 |
| 19 // The delegate of the NSAlert used to display the dialog. Forwards the alert's | 32 // The delegate of the NSAlert used to display the dialog. Forwards the alert's |
| 20 // completion event to the C++ class |TabModalConfirmDialogDelegate|. | 33 // completion event to the C++ class |TabModalConfirmDialogDelegate|. |
| 21 @interface TabModalConfirmDialogMacBridge : NSObject { | 34 @interface TabModalConfirmDialogMacBridge : NSObject { |
| 22 TabModalConfirmDialogDelegate* delegate_; // weak | 35 TabModalConfirmDialogDelegate* delegate_; // weak |
| 23 } | 36 } |
| 24 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; | 37 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; |
| 25 - (void)alertDidEnd:(NSAlert*)alert | 38 - (void)alertDidEnd:(NSAlert*)alert |
| 26 returnCode:(int)returnCode | 39 returnCode:(int)returnCode |
| 27 contextInfo:(void*)contextInfo; | 40 contextInfo:(void*)contextInfo; |
| 28 @end | 41 @end |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 returnCode:(int)returnCode | 52 returnCode:(int)returnCode |
| 40 contextInfo:(void*)contextInfo { | 53 contextInfo:(void*)contextInfo { |
| 41 if (returnCode == NSAlertFirstButtonReturn) { | 54 if (returnCode == NSAlertFirstButtonReturn) { |
| 42 delegate_->Accept(); | 55 delegate_->Accept(); |
| 43 } else { | 56 } else { |
| 44 delegate_->Cancel(); | 57 delegate_->Cancel(); |
| 45 } | 58 } |
| 46 } | 59 } |
| 47 @end | 60 @end |
| 48 | 61 |
| 49 namespace chrome { | |
| 50 | |
| 51 // Declared in browser_dialogs.h so others don't have to depend on our header. | |
| 52 void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, | |
| 53 TabContents* tab_contents) { | |
| 54 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 55 switches::kEnableFramelessConstrainedDialogs)) { | |
| 56 // Deletes itself when closed. | |
| 57 new TabModalConfirmDialogMac2(delegate, tab_contents); | |
| 58 } else { | |
| 59 // Deletes itself when closed. | |
| 60 new TabModalConfirmDialogMac(delegate, tab_contents); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 } // namespace chrome | |
| 65 | |
| 66 TabModalConfirmDialogMac::TabModalConfirmDialogMac( | 62 TabModalConfirmDialogMac::TabModalConfirmDialogMac( |
| 67 TabModalConfirmDialogDelegate* delegate, | 63 TabModalConfirmDialogDelegate* delegate, |
| 68 TabContents* tab_contents) | 64 TabContents* tab_contents) |
| 69 : ConstrainedWindowMacDelegateSystemSheet( | 65 : ConstrainedWindowMacDelegateSystemSheet( |
| 70 [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] | 66 [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] |
| 71 autorelease], | 67 autorelease], |
| 72 @selector(alertDidEnd:returnCode:contextInfo:)), | 68 @selector(alertDidEnd:returnCode:contextInfo:)), |
| 73 delegate_(delegate) { | 69 delegate_(delegate) { |
| 74 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | 70 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
| 75 [alert setMessageText: | 71 [alert setMessageText: |
| 76 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; | 72 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; |
| 77 [alert setInformativeText: | 73 [alert setInformativeText: |
| 78 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; | 74 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; |
| 79 [alert addButtonWithTitle: | 75 [alert addButtonWithTitle: |
| 80 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; | 76 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; |
| 81 [alert addButtonWithTitle: | 77 [alert addButtonWithTitle: |
| 82 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; | 78 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; |
| 83 gfx::Image* icon = delegate->GetIcon(); | 79 gfx::Image* icon = delegate->GetIcon(); |
| 84 if (icon) | 80 if (icon) |
| 85 [alert setIcon:icon->ToNSImage()]; | 81 [alert setIcon:icon->ToNSImage()]; |
| 86 | 82 |
| 87 set_sheet(alert); | 83 set_sheet(alert); |
| 88 | 84 |
| 89 delegate->set_window(new ConstrainedWindowMac(tab_contents, this)); | 85 delegate->set_window(new ConstrainedWindowMac(tab_contents, this)); |
| 90 } | 86 } |
| 91 | 87 |
| 92 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { | 88 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { |
| 93 NSWindow* window = [(NSAlert*)sheet() window]; | 89 CancelTabModalDialog(); |
| 94 if (window && is_sheet_open()) { | |
| 95 [NSApp endSheet:window | |
| 96 returnCode:NSAlertSecondButtonReturn]; | |
| 97 } | |
| 98 } | 90 } |
| 99 | 91 |
| 100 // "DeleteDelegate" refers to this class being a ConstrainedWindow delegate | 92 // "DeleteDelegate" refers to this class being a ConstrainedWindow delegate |
| 101 // and deleting itself, not to deleting the member variable |delegate_|. | 93 // and deleting itself, not to deleting the member variable |delegate_|. |
| 102 void TabModalConfirmDialogMac::DeleteDelegate() { | 94 void TabModalConfirmDialogMac::DeleteDelegate() { |
| 103 delete this; | 95 delete this; |
| 104 } | 96 } |
| 105 | 97 |
| 98 void TabModalConfirmDialogMac::AcceptTabModalDialog() { |
| 99 NSWindow* window = [(NSAlert*)sheet() window]; |
| 100 if (window && is_sheet_open()) { |
| 101 [NSApp endSheet:window |
| 102 returnCode:NSAlertFirstButtonReturn]; |
| 103 } |
| 104 } |
| 105 |
| 106 void TabModalConfirmDialogMac::CancelTabModalDialog() { |
| 107 NSWindow* window = [(NSAlert*)sheet() window]; |
| 108 if (window && is_sheet_open()) { |
| 109 [NSApp endSheet:window |
| 110 returnCode:NSAlertSecondButtonReturn]; |
| 111 } |
| 112 } |
| 113 |
| 114 |
| 106 @interface TabModalConfirmDialogMacBridge2 : NSObject { | 115 @interface TabModalConfirmDialogMacBridge2 : NSObject { |
| 107 TabModalConfirmDialogDelegate* delegate_; // weak | 116 TabModalConfirmDialogDelegate* delegate_; // weak |
| 108 } | 117 } |
| 109 @end | 118 @end |
| 110 | 119 |
| 111 @implementation TabModalConfirmDialogMacBridge2 | 120 @implementation TabModalConfirmDialogMacBridge2 |
| 112 | 121 |
| 113 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { | 122 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { |
| 114 if ((self = [super init])) { | 123 if ((self = [super init])) { |
| 115 delegate_ = delegate; | 124 delegate_ = delegate; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 [[alert_ closeButton] setTarget:bridge_]; | 162 [[alert_ closeButton] setTarget:bridge_]; |
| 154 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; | 163 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; |
| 155 [alert_ layout]; | 164 [alert_ layout]; |
| 156 | 165 |
| 157 delegate->set_window( | 166 delegate->set_window( |
| 158 new ConstrainedWindowMac2(tab_contents, [alert_ window])); | 167 new ConstrainedWindowMac2(tab_contents, [alert_ window])); |
| 159 } | 168 } |
| 160 | 169 |
| 161 TabModalConfirmDialogMac2::~TabModalConfirmDialogMac2() { | 170 TabModalConfirmDialogMac2::~TabModalConfirmDialogMac2() { |
| 162 } | 171 } |
| 172 |
| 173 void TabModalConfirmDialogMac2::AcceptTabModalDialog() { |
| 174 } |
| 175 |
| 176 void TabModalConfirmDialogMac2::CancelTabModalDialog() { |
| 177 } |
| OLD | NEW |