Chromium Code Reviews| 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 } else { | |
|
Peter Kasting
2012/09/25 21:08:42
Nit: No else after return
| |
| 28 // Deletes itself when closed. | |
| 29 return new TabModalConfirmDialogMac(delegate, tab_contents); | |
| 30 } | |
| 31 } | |
| 32 | |
| 19 // The delegate of the NSAlert used to display the dialog. Forwards the alert's | 33 // The delegate of the NSAlert used to display the dialog. Forwards the alert's |
| 20 // completion event to the C++ class |TabModalConfirmDialogDelegate|. | 34 // completion event to the C++ class |TabModalConfirmDialogDelegate|. |
| 21 @interface TabModalConfirmDialogMacBridge : NSObject { | 35 @interface TabModalConfirmDialogMacBridge : NSObject { |
| 22 TabModalConfirmDialogDelegate* delegate_; // weak | 36 TabModalConfirmDialogDelegate* delegate_; // weak |
| 23 } | 37 } |
| 24 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; | 38 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; |
| 25 - (void)alertDidEnd:(NSAlert*)alert | 39 - (void)alertDidEnd:(NSAlert*)alert |
| 26 returnCode:(int)returnCode | 40 returnCode:(int)returnCode |
| 27 contextInfo:(void*)contextInfo; | 41 contextInfo:(void*)contextInfo; |
| 28 @end | 42 @end |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 39 returnCode:(int)returnCode | 53 returnCode:(int)returnCode |
| 40 contextInfo:(void*)contextInfo { | 54 contextInfo:(void*)contextInfo { |
| 41 if (returnCode == NSAlertFirstButtonReturn) { | 55 if (returnCode == NSAlertFirstButtonReturn) { |
| 42 delegate_->Accept(); | 56 delegate_->Accept(); |
| 43 } else { | 57 } else { |
| 44 delegate_->Cancel(); | 58 delegate_->Cancel(); |
| 45 } | 59 } |
| 46 } | 60 } |
| 47 @end | 61 @end |
| 48 | 62 |
| 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( | 63 TabModalConfirmDialogMac::TabModalConfirmDialogMac( |
| 67 TabModalConfirmDialogDelegate* delegate, | 64 TabModalConfirmDialogDelegate* delegate, |
| 68 TabContents* tab_contents) | 65 TabContents* tab_contents) |
| 69 : ConstrainedWindowMacDelegateSystemSheet( | 66 : ConstrainedWindowMacDelegateSystemSheet( |
| 70 [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] | 67 [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] |
| 71 autorelease], | 68 autorelease], |
| 72 @selector(alertDidEnd:returnCode:contextInfo:)), | 69 @selector(alertDidEnd:returnCode:contextInfo:)), |
| 73 delegate_(delegate) { | 70 delegate_(delegate) { |
| 74 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | 71 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
| 75 [alert setMessageText: | 72 [alert setMessageText: |
| 76 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; | 73 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; |
| 77 [alert setInformativeText: | 74 [alert setInformativeText: |
| 78 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; | 75 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; |
| 79 [alert addButtonWithTitle: | 76 [alert addButtonWithTitle: |
| 80 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; | 77 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; |
| 81 [alert addButtonWithTitle: | 78 [alert addButtonWithTitle: |
| 82 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; | 79 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; |
| 83 gfx::Image* icon = delegate->GetIcon(); | 80 gfx::Image* icon = delegate->GetIcon(); |
| 84 if (icon) | 81 if (icon) |
| 85 [alert setIcon:icon->ToNSImage()]; | 82 [alert setIcon:icon->ToNSImage()]; |
| 86 | 83 |
| 87 set_sheet(alert); | 84 set_sheet(alert); |
| 88 | 85 |
| 89 delegate->set_window(new ConstrainedWindowMac(tab_contents, this)); | 86 delegate->set_window(new ConstrainedWindowMac(tab_contents, this)); |
| 90 } | 87 } |
| 91 | 88 |
| 92 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { | 89 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { |
| 93 NSWindow* window = [(NSAlert*)sheet() window]; | 90 CancelTabModalDialog(); |
| 94 if (window && is_sheet_open()) { | |
| 95 [NSApp endSheet:window | |
| 96 returnCode:NSAlertSecondButtonReturn]; | |
| 97 } | |
| 98 } | 91 } |
| 99 | 92 |
| 100 // "DeleteDelegate" refers to this class being a ConstrainedWindow delegate | 93 // "DeleteDelegate" refers to this class being a ConstrainedWindow delegate |
| 101 // and deleting itself, not to deleting the member variable |delegate_|. | 94 // and deleting itself, not to deleting the member variable |delegate_|. |
| 102 void TabModalConfirmDialogMac::DeleteDelegate() { | 95 void TabModalConfirmDialogMac::DeleteDelegate() { |
| 103 delete this; | 96 delete this; |
| 104 } | 97 } |
| 105 | 98 |
| 99 void TabModalConfirmDialogMac::AcceptTabModalDialog() { | |
| 100 NSWindow* window = [(NSAlert*)sheet() window]; | |
| 101 if (window && is_sheet_open()) { | |
| 102 [NSApp endSheet:window | |
| 103 returnCode:NSAlertFirstButtonReturn]; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void TabModalConfirmDialogMac::CancelTabModalDialog() { | |
| 108 NSWindow* window = [(NSAlert*)sheet() window]; | |
| 109 if (window && is_sheet_open()) { | |
| 110 [NSApp endSheet:window | |
| 111 returnCode:NSAlertSecondButtonReturn]; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 | |
| 106 @interface TabModalConfirmDialogMacBridge2 : NSObject { | 116 @interface TabModalConfirmDialogMacBridge2 : NSObject { |
| 107 TabModalConfirmDialogDelegate* delegate_; // weak | 117 TabModalConfirmDialogDelegate* delegate_; // weak |
| 108 } | 118 } |
| 109 @end | 119 @end |
| 110 | 120 |
| 111 @implementation TabModalConfirmDialogMacBridge2 | 121 @implementation TabModalConfirmDialogMacBridge2 |
| 112 | 122 |
| 113 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { | 123 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { |
| 114 if ((self = [super init])) { | 124 if ((self = [super init])) { |
| 115 delegate_ = delegate; | 125 delegate_ = delegate; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 [[alert_ closeButton] setTarget:bridge_]; | 163 [[alert_ closeButton] setTarget:bridge_]; |
| 154 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; | 164 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; |
| 155 [alert_ layout]; | 165 [alert_ layout]; |
| 156 | 166 |
| 157 delegate->set_window( | 167 delegate->set_window( |
| 158 new ConstrainedWindowMac2(tab_contents, [alert_ window])); | 168 new ConstrainedWindowMac2(tab_contents, [alert_ window])); |
| 159 } | 169 } |
| 160 | 170 |
| 161 TabModalConfirmDialogMac2::~TabModalConfirmDialogMac2() { | 171 TabModalConfirmDialogMac2::~TabModalConfirmDialogMac2() { |
| 162 } | 172 } |
| 173 | |
| 174 void TabModalConfirmDialogMac2::AcceptTabModalDialog() { | |
| 175 } | |
| 176 | |
| 177 void TabModalConfirmDialogMac2::CancelTabModalDialog() { | |
| 178 } | |
| OLD | NEW |