OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h" | 5 #import "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | |
7 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "base/strings/sys_string_conversions.h" | |
8 #include "chrome/browser/translate/translate_ui_delegate.h" | 10 #include "chrome/browser/translate/translate_ui_delegate.h" |
9 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 11 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
10 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | 12 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
11 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 13 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
12 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | 14 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
15 #include "chrome/browser/ui/translate/language_combobox_model.h" | |
13 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | 16 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
14 #import "ui/base/cocoa/flipped_view.h" | 17 #include "content/public/browser/browser_context.h" |
18 #include "grit/generated_resources.h" | |
19 #import "ui/base/cocoa/controls/hyperlink_button_cell.h" | |
15 #import "ui/base/cocoa/window_size_constants.h" | 20 #import "ui/base/cocoa/window_size_constants.h" |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/base/models/combobox_model.h" | |
23 | |
24 class TranslateDenialComboboxModel : public ui::ComboboxModel { | |
25 public: | |
26 explicit TranslateDenialComboboxModel( | |
27 const base::string16& original_language_name) { | |
28 // Dummy menu item, which is shown on the top of a NSPopUpButton. The top | |
29 // text of the denial pop up menu should be IDS_TRANSLATE_BUBBLE_DENY, while | |
30 // it is impossible to use it here because NSPopUpButtons' addItemWithTitle | |
31 // removes a duplicated menu item. Instead, the title will be set later by | |
32 // NSMenuItem's setTitle. | |
33 items_.push_back(base::string16()); | |
34 | |
35 // Menu items in the drop down menu. | |
36 items_.push_back(l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
37 items_.push_back(l10n_util::GetStringFUTF16( | |
38 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_LANG, | |
39 original_language_name)); | |
40 items_.push_back(l10n_util::GetStringUTF16( | |
41 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_SITE)); | |
42 } | |
43 virtual ~TranslateDenialComboboxModel() {} | |
44 | |
45 private: | |
46 // ComboboxModel: | |
47 virtual int GetItemCount() const OVERRIDE { | |
48 return items_.size(); | |
49 } | |
50 virtual base::string16 GetItemAt(int index) OVERRIDE { | |
51 return items_[index]; | |
52 } | |
53 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | |
54 return false; | |
55 } | |
56 virtual int GetDefaultIndex() const OVERRIDE { | |
57 return 0; | |
58 } | |
59 | |
60 std::vector<base::string16> items_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(TranslateDenialComboboxModel); | |
63 }; | |
64 | |
65 const CGFloat kWindowWidth = 320; | |
66 | |
67 // Padding between the window frame and content. | |
68 const CGFloat kFramePadding = 16; | |
69 | |
70 const CGFloat kRelatedControlHorizontalSpacing = -2; | |
71 | |
72 const CGFloat kRelatedControlVerticalSpacing = 4; | |
73 const CGFloat kUnrelatedControlVerticalSpacing = 20; | |
74 | |
75 const CGFloat kContentWidth = kWindowWidth - 2 * kFramePadding; | |
76 | |
77 namespace { | |
78 | |
79 NSNumber* toNSNumber(int value) { | |
80 return [NSNumber numberWithInt:value]; | |
81 } | |
82 | |
83 } // namespace | |
84 | |
85 @interface TranslateBubbleController() | |
86 | |
87 - (void)performLayout; | |
88 - (NSView*)newBeforeTranslateView; | |
89 - (NSView*)newTranslatingView; | |
90 - (NSView*)newAfterTranslateView; | |
91 - (NSView*)newErrorView; | |
92 - (NSView*)newAdvancedView; | |
93 - (void)updateAdvancedView; | |
94 - (NSTextField*)addText:(NSString*)text | |
95 toView:(NSView*)view; | |
96 - (NSButton*)addLinkButtonWithText:(NSString*)text | |
97 action:(SEL)action | |
98 toView:(NSView*)view; | |
99 - (NSButton*)addButton:(NSString*)title | |
100 action:(SEL)action | |
101 toView:(NSView*)view; | |
102 - (NSButton*)addCheckbox:(NSString*)title | |
103 toView:(NSView*)view; | |
104 - (NSPopUpButton*)addPopUpButton:(ui::ComboboxModel*)model | |
105 action:(SEL)action | |
106 toView:(NSView*)view; | |
107 - (void)handleTranslateButtonPressed; | |
108 - (void)handleNopeButtonPressed; | |
109 - (void)handleDoneButtonPressed; | |
110 - (void)handleCancelButtonPressed; | |
111 - (void)handleShowOriginalButtonPressed; | |
112 - (void)handleAdvancedLinkButtonPressed; | |
113 - (void)handleDenialPopUpButtonNopeSelected; | |
114 - (void)handleDenialPopUpButtonNeverTranslateLanguageSelected; | |
115 - (void)handleDenialPopUpButtonNeverTranslateSiteSelected; | |
116 - (void)handleSourceLanguagePopUpButtonSelectedItemChanged:(id)sender; | |
117 - (void)handleTargetLanguagePopUpButtonSelectedItemChanged:(id)sender; | |
118 | |
119 @end | |
16 | 120 |
17 @implementation TranslateBubbleController | 121 @implementation TranslateBubbleController |
18 | 122 |
19 - (id)initWithParentWindow:(BrowserWindowController*)controller | 123 - (id)initWithParentWindow:(BrowserWindowController*)controller |
20 model:(scoped_ptr<TranslateBubbleModel>)model | 124 model:(scoped_ptr<TranslateBubbleModel>)model |
21 webContents:(content::WebContents*)webContents { | 125 webContents:(content::WebContents*)webContents { |
22 NSWindow* parentWindow = [controller window]; | 126 NSWindow* parentWindow = [controller window]; |
23 | 127 |
24 // Use an arbitrary size; it will be changed in performLayout. | 128 // Use an arbitrary size; it will be changed in performLayout. |
25 NSRect contentRect = ui::kWindowSizeDeterminedLater; | 129 NSRect contentRect = ui::kWindowSizeDeterminedLater; |
26 base::scoped_nsobject<InfoBubbleWindow> window( | 130 base::scoped_nsobject<InfoBubbleWindow> window( |
27 [[InfoBubbleWindow alloc] initWithContentRect:contentRect | 131 [[InfoBubbleWindow alloc] initWithContentRect:contentRect |
28 styleMask:NSBorderlessWindowMask | 132 styleMask:NSBorderlessWindowMask |
29 backing:NSBackingStoreBuffered | 133 backing:NSBackingStoreBuffered |
30 defer:NO]); | 134 defer:NO]); |
31 | 135 |
32 if ((self = [super initWithWindow:window | 136 if ((self = [super initWithWindow:window |
33 parentWindow:parentWindow | 137 parentWindow:parentWindow |
34 anchoredAt:NSZeroPoint])) { | 138 anchoredAt:NSZeroPoint])) { |
35 webContents_ = webContents; | 139 webContents_ = webContents; |
36 model_ = model.Pass(); | 140 model_ = model.Pass(); |
37 if (model_->GetViewState() != | 141 if (model_->GetViewState() != |
38 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { | 142 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { |
39 translateExecuted_ = YES; | 143 translateExecuted_ = YES; |
40 } | 144 } |
41 | 145 |
146 views_.reset([@{ | |
147 toNSNumber(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE): | |
groby-ooo-7-16
2014/03/27 00:39:39
nit: To translate from enum to NSNumber, you can u
hajimehoshi
2014/03/27 10:37:28
Done.
| |
148 [self newBeforeTranslateView], | |
149 toNSNumber(TranslateBubbleModel::VIEW_STATE_TRANSLATING): | |
150 [self newTranslatingView], | |
151 toNSNumber(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE): | |
152 [self newAfterTranslateView], | |
153 toNSNumber(TranslateBubbleModel::VIEW_STATE_ERROR): | |
154 [self newErrorView], | |
155 toNSNumber(TranslateBubbleModel::VIEW_STATE_ADVANCED): | |
156 [self newAdvancedView], | |
157 } retain]); | |
158 | |
42 [self performLayout]; | 159 [self performLayout]; |
43 } | 160 } |
44 return self; | 161 return self; |
45 } | 162 } |
46 | 163 |
47 @synthesize webContents = webContents_; | 164 @synthesize webContents = webContents_; |
48 | 165 |
166 - (NSView*)currentView { | |
167 NSNumber* key = toNSNumber(model_->GetViewState()); | |
168 NSView* view = [views_ objectForKey:key]; | |
groby-ooo-7-16
2014/03/27 00:39:39
I think this can be boxed too - @( model_->GetView
hajimehoshi
2014/03/27 10:37:28
Done.
| |
169 DCHECK(view); | |
170 return view; | |
171 } | |
172 | |
49 - (const TranslateBubbleModel*)model { | 173 - (const TranslateBubbleModel*)model { |
50 return model_.get(); | 174 return model_.get(); |
51 } | 175 } |
52 | 176 |
53 - (void)showWindow:(id)sender { | 177 - (void)showWindow:(id)sender { |
54 BrowserWindowController* controller = [[self parentWindow] windowController]; | 178 BrowserWindowController* controller = [[self parentWindow] windowController]; |
55 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; | 179 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; |
56 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; | 180 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; |
57 [self setAnchorPoint:anchorPoint]; | 181 [self setAnchorPoint:anchorPoint]; |
58 [super showWindow:sender]; | 182 [super showWindow:sender]; |
59 } | 183 } |
60 | 184 |
61 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { | 185 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { |
62 if (model_->GetViewState() == viewState) | 186 if (model_->GetViewState() == viewState) |
63 return; | 187 return; |
64 | 188 |
65 model_->SetViewState(viewState); | 189 model_->SetViewState(viewState); |
66 [self performLayout]; | 190 [self performLayout]; |
67 } | 191 } |
68 | 192 |
69 - (void)switchToErrorView:(TranslateErrors::Type)errorType { | 193 - (void)switchToErrorView:(TranslateErrors::Type)errorType { |
70 // TODO(hajimehoshi): Implement this. | 194 // TODO(hajimehoshi): Implement this. |
71 } | 195 } |
72 | 196 |
73 - (void)performLayout { | 197 - (void)performLayout { |
74 // TODO(hajimehoshi): Now this shows just an empty bubble. Implement this. | 198 NSWindow* window = [self window]; |
75 [[self window] display]; | 199 [[window contentView] setSubviews:@[ [self currentView] ]]; |
200 | |
201 CGFloat height = NSHeight([[self currentView] frame]) + | |
202 2 * kFramePadding + info_bubble::kBubbleArrowHeight; | |
203 | |
204 NSRect windowFrame = [window contentRectForFrameRect:[[self window] frame]]; | |
205 NSRect newWindowFrame = [window frameRectForContentRect:NSMakeRect( | |
206 NSMinX(windowFrame), NSMaxY(windowFrame) - height, kWindowWidth, height)]; | |
207 [window setFrame:newWindowFrame | |
208 display:YES | |
209 animate:[[self window] isVisible]]; | |
210 } | |
211 | |
212 - (NSView*)newBeforeTranslateView { | |
213 NSRect contentFrame = NSMakeRect( | |
214 kFramePadding, | |
215 kFramePadding, | |
216 kContentWidth, | |
217 0); | |
218 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
219 | |
220 NSString* message = | |
221 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_BEFORE_TRANSLATE); | |
222 NSTextField* textLabel = [self addText:message | |
223 toView:view]; | |
224 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
225 NSButton* advancedLinkButton = | |
226 [self addLinkButtonWithText:message | |
227 action:@selector(handleAdvancedLinkButtonPressed) | |
228 toView:view]; | |
229 | |
230 NSString* title = | |
231 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
232 NSButton* translateButton = | |
233 [self addButton:title | |
234 action:@selector(handleTranslateButtonPressed) | |
235 toView:view]; | |
236 | |
237 base::string16 originalLanguageName = | |
238 model_->GetLanguageNameAt(model_->GetOriginalLanguageIndex()); | |
239 // TODO(hajimehoshi): When TranslateDenialComboboxModel is factored out as a | |
240 // common model, ui::MenuModel will be used here. | |
241 translateDenialComboboxModel_.reset( | |
242 new TranslateDenialComboboxModel(originalLanguageName)); | |
243 NSPopUpButton* denyPopUpButton = | |
244 [self addPopUpButton:translateDenialComboboxModel_.get() | |
245 action:nil | |
246 toView:view]; | |
247 [denyPopUpButton setPullsDown:YES]; | |
248 [[denyPopUpButton itemAtIndex:1] setTarget:self]; | |
249 [[denyPopUpButton itemAtIndex:1] | |
250 setAction:@selector(handleDenialPopUpButtonNopeSelected)]; | |
251 [[denyPopUpButton itemAtIndex:2] setTarget:self]; | |
252 [[denyPopUpButton itemAtIndex:2] | |
253 setAction:@selector(handleDenialPopUpButtonNeverTranslateLanguageSelected)]; | |
254 [[denyPopUpButton itemAtIndex:3] setTarget:self]; | |
255 [[denyPopUpButton itemAtIndex:3] | |
256 setAction:@selector(handleDenialPopUpButtonNeverTranslateSiteSelected)]; | |
257 | |
258 title = base::SysUTF16ToNSString( | |
259 l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
260 [[denyPopUpButton itemAtIndex:0] setTitle:title]; | |
261 | |
262 // Adjust width for the first item. | |
263 base::scoped_nsobject<NSMenu> originalMenu([[denyPopUpButton menu] copy]); | |
264 [denyPopUpButton removeAllItems]; | |
265 [denyPopUpButton addItemWithTitle:[[originalMenu itemAtIndex:0] title]]; | |
266 [denyPopUpButton sizeToFit]; | |
267 [denyPopUpButton setMenu:originalMenu]; | |
268 | |
269 // Layout | |
270 CGFloat yPos = 0; | |
271 | |
272 [translateButton setFrameOrigin:NSMakePoint( | |
273 kContentWidth - NSWidth([translateButton frame]), yPos)]; | |
274 | |
275 NSRect denyPopUpButtonFrame = [denyPopUpButton frame]; | |
276 CGFloat diffY = [[denyPopUpButton cell] | |
277 titleRectForBounds:[denyPopUpButton bounds]].origin.y; | |
278 [denyPopUpButton setFrameOrigin:NSMakePoint( | |
279 NSMinX([translateButton frame]) - denyPopUpButtonFrame.size.width | |
280 - kRelatedControlHorizontalSpacing, | |
281 yPos + diffY)]; | |
282 | |
283 yPos += NSHeight([translateButton frame]) + | |
284 kUnrelatedControlVerticalSpacing; | |
285 | |
286 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
287 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
288 NSWidth([textLabel frame]), yPos)]; | |
289 | |
290 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
291 | |
292 return view; | |
293 } | |
294 | |
295 - (NSView*)newTranslatingView { | |
296 NSRect contentFrame = NSMakeRect( | |
297 kFramePadding, | |
298 kFramePadding, | |
299 kContentWidth, | |
300 0); | |
301 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
302 | |
303 NSString* message = | |
304 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATING); | |
305 NSTextField* textLabel = [self addText:message | |
306 toView:view]; | |
307 NSString* title = | |
308 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
309 NSButton* showOriginalButton = | |
310 [self addButton:title | |
311 action:@selector(handleShowOriginalButtonPressed) | |
312 toView:view]; | |
313 [showOriginalButton setEnabled:NO]; | |
314 | |
315 // Layout | |
316 // TODO(hajimehoshi): Use l10n_util::VerticallyReflowGroup. | |
317 CGFloat yPos = 0; | |
318 | |
319 [showOriginalButton setFrameOrigin:NSMakePoint( | |
320 kContentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
321 | |
322 yPos += NSHeight([showOriginalButton frame]) + | |
323 kUnrelatedControlVerticalSpacing; | |
324 | |
325 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
326 | |
327 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
328 | |
329 return view; | |
330 } | |
331 | |
332 - (NSView*)newAfterTranslateView { | |
333 NSRect contentFrame = NSMakeRect( | |
334 kFramePadding, | |
335 kFramePadding, | |
336 kContentWidth, | |
337 0); | |
338 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
339 | |
340 NSString* message = | |
341 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATED); | |
342 NSTextField* textLabel = [self addText:message | |
343 toView:view]; | |
344 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
345 NSButton* advancedLinkButton = | |
346 [self addLinkButtonWithText:message | |
347 action:@selector(handleAdvancedLinkButtonPressed) | |
348 toView:view]; | |
349 NSString* title = | |
350 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
351 NSButton* showOriginalButton = | |
352 [self addButton:title | |
353 action:@selector(handleShowOriginalButtonPressed) | |
354 toView:view]; | |
355 | |
356 // Layout | |
357 CGFloat yPos = 0; | |
358 | |
359 [showOriginalButton setFrameOrigin:NSMakePoint( | |
360 kContentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
361 | |
362 yPos += NSHeight([showOriginalButton frame]) + | |
363 kUnrelatedControlVerticalSpacing; | |
364 | |
365 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
366 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
367 NSMaxX([textLabel frame]), yPos)]; | |
368 | |
369 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
370 | |
371 return view; | |
372 } | |
373 | |
374 - (NSView*)newErrorView { | |
375 NSRect contentFrame = NSMakeRect( | |
376 kFramePadding, | |
377 kFramePadding, | |
378 kContentWidth, | |
379 0); | |
380 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
381 | |
382 // TODO(hajimehoshi): Implement this. | |
383 | |
384 return view; | |
385 } | |
386 | |
387 - (NSView*)newAdvancedView { | |
388 NSRect contentFrame = NSMakeRect( | |
389 kFramePadding, | |
390 kFramePadding, | |
391 kContentWidth, | |
392 0); | |
393 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
394 | |
395 NSString* title = l10n_util::GetNSStringWithFixup( | |
396 IDS_TRANSLATE_BUBBLE_PAGE_LANGUAGE); | |
397 NSTextField* sourceLanguageLabel = [self addText:title | |
398 toView:view]; | |
399 title = l10n_util::GetNSStringWithFixup( | |
400 IDS_TRANSLATE_BUBBLE_TRANSLATION_LANGUAGE); | |
401 NSTextField* targetLanguageLabel = [self addText:title | |
402 toView:view]; | |
403 | |
404 // combobox | |
405 int sourceDefaultIndex = model_->GetOriginalLanguageIndex(); | |
406 int targetDefaultIndex = model_->GetTargetLanguageIndex(); | |
407 sourceLanguageComboboxModel_.reset( | |
408 new LanguageComboboxModel(sourceDefaultIndex, model_.get())); | |
409 targetLanguageComboboxModel_.reset( | |
410 new LanguageComboboxModel(targetDefaultIndex, model_.get())); | |
411 SEL action = @selector(handleSourceLanguagePopUpButtonSelectedItemChanged:); | |
412 NSPopUpButton* sourcePopUpButton = | |
413 [self addPopUpButton:sourceLanguageComboboxModel_.get() | |
414 action:action | |
415 toView:view]; | |
416 action = @selector(handleTargetLanguagePopUpButtonSelectedItemChanged:); | |
417 NSPopUpButton* targetPopUpButton = | |
418 [self addPopUpButton:targetLanguageComboboxModel_.get() | |
419 action:action | |
420 toView:view]; | |
421 | |
422 // 'Always translate' checkbox | |
423 BOOL isIncognitoWindow = webContents_ ? | |
424 webContents_->GetBrowserContext()->IsOffTheRecord() : NO; | |
425 if (!isIncognitoWindow) { | |
426 NSString* title = | |
427 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ALWAYS); | |
428 alwaysTranslateCheckbox_ = [self addCheckbox:title | |
429 toView:view]; | |
430 } | |
431 | |
432 // Buttons | |
433 advancedDoneButton_ = | |
434 [self addButton:l10n_util::GetNSStringWithFixup(IDS_DONE) | |
435 action:@selector(handleDoneButtonPressed) | |
436 toView:view]; | |
437 advancedCancelButton_ = | |
438 [self addButton:l10n_util::GetNSStringWithFixup(IDS_CANCEL) | |
439 action:@selector(handleCancelButtonPressed) | |
440 toView:view]; | |
441 | |
442 // Layout | |
443 CGFloat textLabelWidth = NSWidth([sourceLanguageLabel frame]); | |
444 if (textLabelWidth < NSWidth([targetLanguageLabel frame])) | |
445 textLabelWidth = NSWidth([targetLanguageLabel frame]); | |
446 | |
447 CGFloat yPos = 0; | |
448 | |
449 [advancedDoneButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
450 [advancedCancelButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
451 | |
452 yPos += NSHeight([advancedDoneButton_ frame]) + | |
453 kUnrelatedControlVerticalSpacing; | |
454 | |
455 if (alwaysTranslateCheckbox_) { | |
456 [alwaysTranslateCheckbox_ setFrameOrigin:NSMakePoint(textLabelWidth, yPos)]; | |
457 | |
458 yPos += NSHeight([alwaysTranslateCheckbox_ frame]) + | |
459 kRelatedControlVerticalSpacing; | |
460 } | |
461 | |
462 CGFloat diffY = [[sourcePopUpButton cell] | |
463 titleRectForBounds:[sourcePopUpButton bounds]].origin.y; | |
464 | |
465 [targetLanguageLabel setFrameOrigin:NSMakePoint( | |
466 textLabelWidth - NSWidth([targetLanguageLabel frame]), yPos + diffY)]; | |
467 | |
468 NSRect frame = [targetPopUpButton frame]; | |
469 frame.origin = NSMakePoint(textLabelWidth, yPos); | |
470 frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; | |
471 [targetPopUpButton setFrame:frame]; | |
472 | |
473 yPos += NSHeight([targetPopUpButton frame]) + | |
474 kRelatedControlVerticalSpacing; | |
475 | |
476 [sourceLanguageLabel setFrameOrigin:NSMakePoint( | |
477 textLabelWidth - NSWidth([sourceLanguageLabel frame]), yPos + diffY)]; | |
478 | |
479 frame = [sourcePopUpButton frame]; | |
480 frame.origin = NSMakePoint(textLabelWidth, yPos); | |
481 frame.size.width = NSWidth([targetPopUpButton frame]); | |
482 [sourcePopUpButton setFrame:frame]; | |
483 | |
484 [view setFrameSize:NSMakeSize(kContentWidth, | |
485 NSMaxY([sourcePopUpButton frame]))]; | |
486 | |
487 [self updateAdvancedView]; | |
488 | |
489 return view; | |
490 } | |
491 | |
492 - (void)updateAdvancedView { | |
493 NSInteger state = model_->ShouldAlwaysTranslate() ? NSOnState : NSOffState; | |
494 [alwaysTranslateCheckbox_ setState:state]; | |
495 | |
496 NSString* title; | |
497 if (model_->IsPageTranslatedInCurrentLanguages()) | |
498 title = l10n_util::GetNSStringWithFixup(IDS_DONE); | |
499 else | |
500 title = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
501 [advancedDoneButton_ setTitle:title]; | |
502 [advancedDoneButton_ sizeToFit]; | |
503 | |
504 NSRect frame = [advancedDoneButton_ frame]; | |
505 frame.origin.x = (kWindowWidth - 2 * kFramePadding) - NSWidth(frame); | |
506 [advancedDoneButton_ setFrameOrigin:frame.origin]; | |
507 | |
508 frame = [advancedCancelButton_ frame]; | |
509 frame.origin.x = NSMinX([advancedDoneButton_ frame]) - NSWidth(frame) | |
510 - kRelatedControlHorizontalSpacing; | |
511 [advancedCancelButton_ setFrameOrigin:frame.origin]; | |
512 } | |
513 | |
514 - (NSTextField*)addText:(NSString*)text | |
515 toView:(NSView*)view { | |
516 base::scoped_nsobject<NSTextField> textField( | |
517 [[NSTextField alloc] initWithFrame:NSZeroRect]); | |
518 [textField setEditable:NO]; | |
519 [textField setSelectable:YES]; | |
520 [textField setDrawsBackground:NO]; | |
521 [textField setBezeled:NO]; | |
522 [textField setStringValue:text]; | |
523 NSFont* font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; | |
524 [textField setFont:font]; | |
525 [textField setAutoresizingMask:NSViewWidthSizable]; | |
526 [view addSubview:textField.get()]; | |
527 | |
528 [textField sizeToFit]; | |
529 return textField.get(); | |
530 } | |
531 | |
532 - (NSButton*)addLinkButtonWithText:(NSString*)text | |
533 action:(SEL)action | |
534 toView:(NSView*)view { | |
535 base::scoped_nsobject<NSButton> button( | |
536 [[HyperlinkButtonCell buttonWithString:text] retain]); | |
537 | |
538 [button setButtonType:NSMomentaryPushInButton]; | |
539 [button setBezelStyle:NSRegularSquareBezelStyle]; | |
540 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
541 [button sizeToFit]; | |
542 [button setTarget:self]; | |
543 [button setAction:action]; | |
544 | |
545 [view addSubview:button.get()]; | |
546 | |
547 return button.get(); | |
548 } | |
549 | |
550 - (NSButton*)addButton:(NSString*)title | |
551 action:(SEL)action | |
552 toView:(NSView*)view { | |
553 base::scoped_nsobject<NSButton> button( | |
554 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
555 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
556 [button setTitle:title]; | |
557 [button setBezelStyle:NSRoundedBezelStyle]; | |
558 [[button cell] setControlSize:NSSmallControlSize]; | |
559 [button sizeToFit]; | |
560 [button setTarget:self]; | |
561 [button setAction:action]; | |
562 | |
563 [view addSubview:button.get()]; | |
564 | |
565 return button.get(); | |
566 } | |
567 | |
568 - (NSButton*)addCheckbox:(NSString*)title | |
569 toView:(NSView*)view { | |
570 base::scoped_nsobject<NSButton> button( | |
571 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
572 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
573 [button setTitle:title]; | |
574 [[button cell] setControlSize:NSSmallControlSize]; | |
575 [button setButtonType:NSSwitchButton]; | |
576 [button sizeToFit]; | |
577 | |
578 [view addSubview:button.get()]; | |
579 | |
580 return button.get(); | |
581 } | |
582 | |
583 - (NSPopUpButton*)addPopUpButton:(ui::ComboboxModel*)model | |
584 action:(SEL)action | |
585 toView:(NSView*)view { | |
586 base::scoped_nsobject<NSPopUpButton> button( | |
587 [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO]); | |
588 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
589 [button setBordered:YES]; | |
590 [[button cell] setControlSize:NSSmallControlSize]; | |
591 [button setTarget:self]; | |
592 [button setAction:action]; | |
593 | |
594 for (int i = 0; i < model->GetItemCount(); ++i) | |
595 [button addItemWithTitle:base::SysUTF16ToNSString(model->GetItemAt(i))]; | |
596 [button selectItemAtIndex:model->GetDefaultIndex()]; | |
597 | |
598 [button sizeToFit]; | |
599 | |
600 [view addSubview:button.get()]; | |
601 | |
602 return button.get(); | |
603 } | |
604 | |
605 - (void)handleTranslateButtonPressed { | |
606 translateExecuted_ = YES; | |
607 model_->Translate(); | |
608 } | |
609 | |
610 - (void)handleNopeButtonPressed { | |
611 [self close]; | |
612 } | |
613 | |
614 - (void)handleDoneButtonPressed { | |
615 if (alwaysTranslateCheckbox_) { | |
616 model_->SetAlwaysTranslate( | |
617 [alwaysTranslateCheckbox_ state] == NSOnState); | |
618 } | |
619 if (model_->IsPageTranslatedInCurrentLanguages()) { | |
620 model_->GoBackFromAdvanced(); | |
621 [self performLayout]; | |
622 } else { | |
623 translateExecuted_ = true; | |
624 model_->Translate(); | |
625 [self switchView:TranslateBubbleModel::VIEW_STATE_TRANSLATING]; | |
626 } | |
627 } | |
628 | |
629 - (void)handleCancelButtonPressed { | |
630 model_->GoBackFromAdvanced(); | |
631 [self performLayout]; | |
632 } | |
633 | |
634 - (void)handleShowOriginalButtonPressed { | |
635 model_->RevertTranslation(); | |
636 [self close]; | |
637 } | |
638 | |
639 - (void)handleAdvancedLinkButtonPressed { | |
640 [self switchView:TranslateBubbleModel::VIEW_STATE_ADVANCED]; | |
641 } | |
642 | |
643 - (void)handleDenialPopUpButtonNopeSelected { | |
644 [self close]; | |
645 } | |
646 | |
647 - (void)handleDenialPopUpButtonNeverTranslateLanguageSelected { | |
648 model_->SetNeverTranslateLanguage(true); | |
649 [self close]; | |
650 } | |
651 | |
652 - (void)handleDenialPopUpButtonNeverTranslateSiteSelected { | |
653 model_->SetNeverTranslateSite(true); | |
654 [self close]; | |
655 } | |
656 | |
657 - (void)handleSourceLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
658 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
659 model_->UpdateOriginalLanguageIndex([button indexOfSelectedItem]); | |
660 [self updateAdvancedView]; | |
661 } | |
662 | |
663 - (void)handleTargetLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
664 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
665 model_->UpdateTargetLanguageIndex([button indexOfSelectedItem]); | |
666 [self updateAdvancedView]; | |
76 } | 667 } |
77 | 668 |
78 @end | 669 @end |
OLD | NEW |