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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h"
9 #include "grit/generated_resources.h"
10 #include "ui/base/l10n/l10n_util.h"
12 using InfoBarUtilities::MoveControl;
13 using InfoBarUtilities::VerifyControlOrderAndSpacing;
17 NSButton* CreateNSButtonWithResourceIDAndParameter(
18 int resourceId, const base::string16& param) {
19 base::string16 title = l10n_util::GetStringFUTF16(resourceId, param);
20 NSButton* button = [[NSButton alloc] init];
21 [button setTitle:base::SysUTF16ToNSString(title)];
22 [button setBezelStyle:NSTexturedRoundedBezelStyle];
23 // Round textured buttons have a different font size than the default button.
24 NSFont* font = [NSFont systemFontOfSize:
25 [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
26 [[button cell] setFont:font];
32 @implementation BeforeTranslateInfobarController
35 [neverTranslateButton_ setTarget:nil];
36 [alwaysTranslateButton_ setTarget:nil];
40 - (id)initWithInfoBar:(InfoBarCocoa*)infobar {
41 if ((self = [super initWithInfoBar:infobar])) {
42 [self initializeExtraControls];
47 - (void)initializeExtraControls {
48 TranslateInfoBarDelegate* delegate = [self delegate];
49 const base::string16& language =
50 delegate->language_name_at(delegate->original_language_index());
51 neverTranslateButton_.reset(
52 CreateNSButtonWithResourceIDAndParameter(
53 IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE, language));
54 [neverTranslateButton_ setTarget:self];
55 [neverTranslateButton_ setAction:@selector(neverTranslate:)];
57 alwaysTranslateButton_.reset(
58 CreateNSButtonWithResourceIDAndParameter(
59 IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, language));
60 [alwaysTranslateButton_ setTarget:self];
61 [alwaysTranslateButton_ setAction:@selector(alwaysTranslate:)];
65 MoveControl(label1_, fromLanguagePopUp_, spaceBetweenControls_ / 2, true);
66 MoveControl(fromLanguagePopUp_, label2_, spaceBetweenControls_, true);
67 MoveControl(label2_, cancelButton_, spaceBetweenControls_, true);
68 MoveControl(cancelButton_, okButton_, spaceBetweenControls_, true);
69 NSView* lastControl = okButton_;
70 if (neverTranslateButton_.get()) {
71 MoveControl(lastControl, neverTranslateButton_.get(),
72 spaceBetweenControls_, true);
73 lastControl = neverTranslateButton_.get();
75 if (alwaysTranslateButton_.get()) {
76 MoveControl(lastControl, alwaysTranslateButton_.get(),
77 spaceBetweenControls_, true);
81 - (void)loadLabelText {
84 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE,
85 base::string16(), &offset);
86 NSString* string1 = base::SysUTF16ToNSString(text.substr(0, offset));
87 NSString* string2 = base::SysUTF16ToNSString(text.substr(offset));
88 [label1_ setStringValue:string1];
89 [label2_ setStringValue:string2];
90 [label3_ setStringValue:@""];
93 - (NSArray*)visibleControls {
94 NSMutableArray* visibleControls = [NSMutableArray arrayWithObjects:
95 label1_.get(), fromLanguagePopUp_.get(), label2_.get(),
96 cancelButton_, okButton_, nil];
98 if ([self delegate]->ShouldShowNeverTranslateShortcut())
99 [visibleControls addObject:neverTranslateButton_.get()];
101 if ([self delegate]->ShouldShowAlwaysTranslateShortcut())
102 [visibleControls addObject:alwaysTranslateButton_.get()];
104 return visibleControls;
107 // This is called when the "Never Translate [language]" button is pressed.
108 - (void)neverTranslate:(id)sender {
111 [self delegate]->NeverTranslatePageLanguage();
114 // This is called when the "Always Translate [language]" button is pressed.
115 - (void)alwaysTranslate:(id)sender {
118 [self delegate]->AlwaysTranslatePageLanguage();
121 - (bool)verifyLayout {
122 if ([optionsPopUp_ isHidden])
124 return [super verifyLayout];
129 @implementation BeforeTranslateInfobarController (TestingAPI)
131 - (NSButton*)alwaysTranslateButton {
132 return alwaysTranslateButton_.get();
134 - (NSButton*)neverTranslateButton {
135 return neverTranslateButton_.get();