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/memory/scoped_nsobject.h" | 7 #include "base/memory/scoped_nsobject.h" |
8 #include "chrome/browser/ui/browser_dialogs.h" | 8 #include "chrome/browser/ui/browser_dialogs.h" |
9 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" | 9 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" |
10 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac2.h" | |
11 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h" | 10 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h" |
12 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" | 11 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
13 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
14 #include "ui/base/l10n/l10n_util_mac.h" | 13 #include "ui/base/l10n/l10n_util_mac.h" |
15 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
16 | 15 |
17 // static | 16 // static |
18 TabModalConfirmDialog* TabModalConfirmDialog::Create( | 17 TabModalConfirmDialog* TabModalConfirmDialog::Create( |
19 TabModalConfirmDialogDelegate* delegate, | 18 TabModalConfirmDialogDelegate* delegate, |
20 content::WebContents* web_contents) { | 19 content::WebContents* web_contents) { |
21 if (chrome::UseChromeStyleDialogs()) { | |
22 // Deletes itself when closed. | |
23 return new TabModalConfirmDialogMac2(delegate, web_contents); | |
24 } | |
25 // Deletes itself when closed. | 20 // Deletes itself when closed. |
26 return new TabModalConfirmDialogMac(delegate, web_contents); | 21 return new TabModalConfirmDialogMac(delegate, web_contents); |
27 } | 22 } |
28 | 23 |
29 // The delegate of the NSAlert used to display the dialog. Forwards the alert's | |
30 // completion event to the C++ class |TabModalConfirmDialogDelegate|. | |
31 @interface TabModalConfirmDialogMacBridge : NSObject { | 24 @interface TabModalConfirmDialogMacBridge : NSObject { |
32 TabModalConfirmDialogDelegate* delegate_; // weak | 25 TabModalConfirmDialogDelegate* delegate_; // weak |
33 } | 26 } |
34 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; | |
35 - (void)alertDidEnd:(NSAlert*)alert | |
36 returnCode:(int)returnCode | |
37 contextInfo:(void*)contextInfo; | |
38 @end | |
39 | |
40 @implementation TabModalConfirmDialogMacBridge | |
41 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { | |
42 if ((self = [super init])) { | |
43 delegate_ = delegate; | |
44 } | |
45 return self; | |
46 } | |
47 | |
48 - (void)alertDidEnd:(NSAlert*)alert | |
49 returnCode:(int)returnCode | |
50 contextInfo:(void*)contextInfo { | |
51 if (returnCode == NSAlertFirstButtonReturn) { | |
52 delegate_->Accept(); | |
53 } else { | |
54 delegate_->Cancel(); | |
55 } | |
56 } | |
57 @end | |
58 | |
59 TabModalConfirmDialogMac::TabModalConfirmDialogMac( | |
60 TabModalConfirmDialogDelegate* delegate, | |
61 content::WebContents* web_contents) | |
62 : ConstrainedWindowMacDelegateSystemSheet( | |
63 [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] | |
64 autorelease], | |
65 @selector(alertDidEnd:returnCode:contextInfo:)), | |
66 delegate_(delegate) { | |
67 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | |
68 [alert setMessageText: | |
69 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; | |
70 [alert setInformativeText: | |
71 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; | |
72 [alert addButtonWithTitle: | |
73 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; | |
74 [alert addButtonWithTitle: | |
75 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; | |
76 gfx::Image* icon = delegate->GetIcon(); | |
77 if (icon) | |
78 [alert setIcon:icon->ToNSImage()]; | |
79 | |
80 set_sheet(alert); | |
81 | |
82 delegate->set_window(new ConstrainedWindowMac(web_contents, this)); | |
83 } | |
84 | |
85 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { | |
86 CancelTabModalDialog(); | |
87 } | |
88 | |
89 // "DeleteDelegate" refers to this class being a ConstrainedWindow delegate | |
90 // and deleting itself, not to deleting the member variable |delegate_|. | |
91 void TabModalConfirmDialogMac::DeleteDelegate() { | |
92 delete this; | |
93 } | |
94 | |
95 void TabModalConfirmDialogMac::AcceptTabModalDialog() { | |
96 NSWindow* window = [(NSAlert*)sheet() window]; | |
97 if (window && is_sheet_open()) { | |
98 [NSApp endSheet:window | |
99 returnCode:NSAlertFirstButtonReturn]; | |
100 } | |
101 } | |
102 | |
103 void TabModalConfirmDialogMac::CancelTabModalDialog() { | |
104 NSWindow* window = [(NSAlert*)sheet() window]; | |
105 if (window && is_sheet_open()) { | |
106 [NSApp endSheet:window | |
107 returnCode:NSAlertSecondButtonReturn]; | |
108 } | |
109 } | |
110 | |
111 | |
112 @interface TabModalConfirmDialogMacBridge2 : NSObject { | |
113 TabModalConfirmDialogDelegate* delegate_; // weak | |
114 } | |
115 @end | 27 @end |
116 | 28 |
117 @implementation TabModalConfirmDialogMacBridge2 | 29 @implementation TabModalConfirmDialogMacBridge |
118 | 30 |
119 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { | 31 - (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { |
120 if ((self = [super init])) { | 32 if ((self = [super init])) { |
121 delegate_ = delegate; | 33 delegate_ = delegate; |
122 DCHECK(delegate_); | 34 DCHECK(delegate_); |
123 } | 35 } |
124 return self; | 36 return self; |
125 } | 37 } |
126 | 38 |
127 - (void)onAcceptButton:(id)sender { | 39 - (void)onAcceptButton:(id)sender { |
128 delegate_->Accept(); | 40 delegate_->Accept(); |
129 } | 41 } |
130 | 42 |
131 - (void)onCancelButton:(id)sender { | 43 - (void)onCancelButton:(id)sender { |
132 delegate_->Cancel(); | 44 delegate_->Cancel(); |
133 } | 45 } |
134 | 46 |
135 @end | 47 @end |
136 | 48 |
137 TabModalConfirmDialogMac2::TabModalConfirmDialogMac2( | 49 TabModalConfirmDialogMac::TabModalConfirmDialogMac( |
138 TabModalConfirmDialogDelegate* delegate, | 50 TabModalConfirmDialogDelegate* delegate, |
139 content::WebContents* web_contents) | 51 content::WebContents* web_contents) |
140 : delegate_(delegate) { | 52 : delegate_(delegate) { |
141 bridge_.reset([[TabModalConfirmDialogMacBridge2 alloc] | 53 bridge_.reset([[TabModalConfirmDialogMacBridge alloc] |
142 initWithDelegate:delegate]); | 54 initWithDelegate:delegate]); |
143 | 55 |
144 alert_.reset([[ConstrainedWindowAlert alloc] init]); | 56 alert_.reset([[ConstrainedWindowAlert alloc] init]); |
145 [alert_ setMessageText: | 57 [alert_ setMessageText: |
146 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; | 58 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; |
147 [alert_ setInformativeText: | 59 [alert_ setInformativeText: |
148 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; | 60 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; |
149 [alert_ addButtonWithTitle: | 61 [alert_ addButtonWithTitle: |
150 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle()) | 62 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle()) |
151 keyEquivalent:kKeyEquivalentReturn | 63 keyEquivalent:kKeyEquivalentReturn |
152 target:bridge_ | 64 target:bridge_ |
153 action:@selector(onAcceptButton:)]; | 65 action:@selector(onAcceptButton:)]; |
154 [alert_ addButtonWithTitle: | 66 [alert_ addButtonWithTitle: |
155 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle()) | 67 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle()) |
156 keyEquivalent:kKeyEquivalentEscape | 68 keyEquivalent:kKeyEquivalentEscape |
157 target:bridge_ | 69 target:bridge_ |
158 action:@selector(onCancelButton:)]; | 70 action:@selector(onCancelButton:)]; |
159 [[alert_ closeButton] setTarget:bridge_]; | 71 [[alert_ closeButton] setTarget:bridge_]; |
160 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; | 72 [[alert_ closeButton] setAction:@selector(onCancelButton:)]; |
161 [alert_ layout]; | 73 [alert_ layout]; |
162 | 74 |
163 window_.reset(new ConstrainedWindowMac2(this, web_contents, [alert_ window])); | 75 window_.reset(new ConstrainedWindowMac2(this, web_contents, [alert_ window])); |
164 delegate->set_window(window_.get()); | 76 delegate->set_window(window_.get()); |
165 } | 77 } |
166 | 78 |
167 TabModalConfirmDialogMac2::~TabModalConfirmDialogMac2() { | 79 TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { |
168 } | 80 } |
169 | 81 |
170 void TabModalConfirmDialogMac2::AcceptTabModalDialog() { | 82 void TabModalConfirmDialogMac::AcceptTabModalDialog() { |
171 delegate_->Accept(); | 83 delegate_->Accept(); |
172 } | 84 } |
173 | 85 |
174 void TabModalConfirmDialogMac2::CancelTabModalDialog() { | 86 void TabModalConfirmDialogMac::CancelTabModalDialog() { |
175 delegate_->Cancel(); | 87 delegate_->Cancel(); |
176 } | 88 } |
177 | 89 |
178 void TabModalConfirmDialogMac2::OnConstrainedWindowClosed( | 90 void TabModalConfirmDialogMac::OnConstrainedWindowClosed( |
179 ConstrainedWindowMac2* window) { | 91 ConstrainedWindowMac2* window) { |
180 delete this; | 92 delete this; |
181 } | 93 } |
OLD | NEW |