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/javascript_app_modal_dialog_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #import "base/mac/foundation_util.h" | |
10 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
11 #import "chrome/browser/chrome_browser_application_mac.h" | 13 #import "chrome/browser/chrome_browser_application_mac.h" |
12 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 14 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
13 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
14 #include "grit/ui_strings.h" | 16 #include "grit/ui_strings.h" |
15 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
16 #include "ui/base/ui_base_types.h" | 18 #include "ui/base/ui_base_types.h" |
17 | 19 |
18 // Helper object that receives the notification that the dialog/sheet is | 20 // Helper object that receives the notification that the dialog/sheet is |
19 // going away. Is responsible for cleaning itself up. | 21 // going away. Is responsible for cleaning itself up. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 | 137 |
136 // Show the modal dialog. | 138 // Show the modal dialog. |
137 alert_ = [helper_ alert]; | 139 alert_ = [helper_ alert]; |
138 NSTextField* field = nil; | 140 NSTextField* field = nil; |
139 if (text_field) { | 141 if (text_field) { |
140 field = [helper_ textField]; | 142 field = [helper_ textField]; |
141 [field setStringValue:base::SysUTF16ToNSString( | 143 [field setStringValue:base::SysUTF16ToNSString( |
142 dialog_->default_prompt_text())]; | 144 dialog_->default_prompt_text())]; |
143 } | 145 } |
144 [alert_ setDelegate:helper_]; | 146 [alert_ setDelegate:helper_]; |
145 [alert_ setInformativeText:base::SysUTF16ToNSString(dialog_->message_text())]; | 147 NSString* informative_text = |
148 base::SysUTF16ToNSString(dialog_->message_text()); | |
149 [alert_ setInformativeText:informative_text]; | |
146 [alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; | 150 [alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; |
147 [alert_ addButtonWithTitle:default_button]; | 151 [alert_ addButtonWithTitle:default_button]; |
148 if (!one_button) { | 152 if (!one_button) { |
149 NSButton* other = [alert_ addButtonWithTitle:other_button]; | 153 NSButton* other = [alert_ addButtonWithTitle:other_button]; |
150 [other setKeyEquivalent:@"\e"]; | 154 [other setKeyEquivalent:@"\e"]; |
151 } | 155 } |
152 if (dialog_->display_suppress_checkbox()) { | 156 if (dialog_->display_suppress_checkbox()) { |
153 [alert_ setShowsSuppressionButton:YES]; | 157 [alert_ setShowsSuppressionButton:YES]; |
154 NSString* suppression_title = l10n_util::GetNSStringWithFixup( | 158 NSString* suppression_title = l10n_util::GetNSStringWithFixup( |
155 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | 159 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
156 [[alert_ suppressionButton] setTitle:suppression_title]; | 160 [[alert_ suppressionButton] setTitle:suppression_title]; |
157 } | 161 } |
162 | |
163 // Fix RTL dialogs. | |
164 // | |
165 // Mac OS X will always display an NSAlert's informative text as LTR. A | |
166 // workaround is to manually set the informative text as an attributed string | |
167 // in the implementing NSTextField. This is a basic correctness issue. | |
168 // | |
169 // In addition, for readability, the overall alignment is set based on the | |
170 // directionality of the first strongly-directional character. | |
171 // | |
172 // See http://crbug.com/70806 for more details. | |
173 | |
174 if (base::i18n::StringContainsStrongRTLChars(dialog_->message_text())) { | |
175 // Force layout of the dialog. NSAlert leaves its dialog alone once laid | |
176 // out; if this is not done then all the modifications that are to come will | |
177 // be un-done when the dialog is finally displayed. | |
178 [alert_ layout]; | |
Mark Mentovai
2013/05/17 01:32:57
The indentation is wrong on this line.
Avi (use Gerrit)
2013/05/20 21:55:29
Done.
| |
179 | |
180 // Locate the NSTextField that implements the informative text. This is | |
181 // actually available as the ivar |_informationField| of the NSAlert, but it | |
182 // is safer (and more forward-compatible) to search for it in the subviews. | |
183 NSTextField* informative_text_field = nil; | |
184 for (NSView* view in [[[alert_ window] contentView] subviews]) { | |
185 NSTextField* text_field = base::mac::ObjCCast<NSTextField>(view); | |
186 if ([[text_field stringValue] isEqualTo:informative_text]) { | |
187 informative_text_field = text_field; | |
188 break; | |
189 } | |
190 } | |
191 // This may fail in future OS releases, but it will still work for shipped | |
192 // versions of Chromium. | |
193 DCHECK(informative_text_field); | |
194 | |
195 if (informative_text_field) { | |
196 base::i18n::TextDirection direction = | |
197 base::i18n::GetFirstStrongCharacterDirection(dialog_->message_text()); | |
198 scoped_nsobject<NSMutableParagraphStyle> alignment( | |
199 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]); | |
200 [alignment setAlignment: | |
201 (direction == base::i18n::RIGHT_TO_LEFT) ? NSRightTextAlignment | |
202 : NSLeftTextAlignment]; | |
jeremy
2013/05/19 10:51:50
Do you also need to manually set the directionalit
Avi (use Gerrit)
2013/05/20 21:55:29
It turns out that I don't need to.
As soon as you
| |
203 | |
204 NSDictionary* alignment_attributes = | |
205 @{ NSParagraphStyleAttributeName : alignment }; | |
206 scoped_nsobject<NSAttributedString> attr_string( | |
207 [[NSAttributedString alloc] initWithString:informative_text | |
208 attributes:alignment_attributes]); | |
209 | |
210 [informative_text_field setAttributedStringValue:attr_string]; | |
211 } | |
212 } | |
158 } | 213 } |
159 | 214 |
160 JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() { | 215 JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() { |
161 } | 216 } |
162 | 217 |
163 //////////////////////////////////////////////////////////////////////////////// | 218 //////////////////////////////////////////////////////////////////////////////// |
164 // JavaScriptAppModalDialogCocoa, NativeAppModalDialog implementation: | 219 // JavaScriptAppModalDialogCocoa, NativeAppModalDialog implementation: |
165 | 220 |
166 int JavaScriptAppModalDialogCocoa::GetAppModalDialogButtons() const { | 221 int JavaScriptAppModalDialogCocoa::GetAppModalDialogButtons() const { |
167 // From the above, it is the case that if there is 1 button, it is always the | 222 // From the above, it is the case that if there is 1 button, it is always the |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 268 |
214 //////////////////////////////////////////////////////////////////////////////// | 269 //////////////////////////////////////////////////////////////////////////////// |
215 // NativeAppModalDialog, public: | 270 // NativeAppModalDialog, public: |
216 | 271 |
217 // static | 272 // static |
218 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( | 273 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
219 JavaScriptAppModalDialog* dialog, | 274 JavaScriptAppModalDialog* dialog, |
220 gfx::NativeWindow parent_window) { | 275 gfx::NativeWindow parent_window) { |
221 return new JavaScriptAppModalDialogCocoa(dialog); | 276 return new JavaScriptAppModalDialogCocoa(dialog); |
222 } | 277 } |
OLD | NEW |