1 // Copyright (c) 2010 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. 4 5 #include "chrome/browser/ui/cocoa/translate/before_translate_infobar_controller.h" 6 7 #include "base/sys_string_conversions.h" 8 #include "grit/generated_resources.h" 9 #include "ui/base/l10n/l10n_util.h" 10 11 using TranslateInfoBarUtilities::MoveControl; 12 using TranslateInfoBarUtilities::VerifyControlOrderAndSpacing; 13 14 namespace { 15 16 NSButton* CreateNSButtonWithResourceIDAndParameter( 17 int resourceId, const string16& param) { 18 string16 title = l10n_util::GetStringFUTF16(resourceId, param); 19 NSButton* button = [[NSButton alloc] init]; 20 [button setTitle:base::SysUTF16ToNSString(title)]; 21 [button setBezelStyle:NSTexturedRoundedBezelStyle]; 22 return button; 23 } 24 25 } // namespace 26 27 @implementation BeforeTranslateInfobarController 28 29 - (id) initWithDelegate:(InfoBarDelegate *)delegate { 30 if ((self = [super initWithDelegate:delegate])) { 31 [self initializeExtraControls]; 32 } 33 return self; 34 } 35 36 - (void)initializeExtraControls { 37 TranslateInfoBarDelegate* delegate = [self delegate]; 38 const string16& language = delegate->GetLanguageDisplayableNameAt( 39 delegate->original_language_index()); 40 neverTranslateButton_.reset( 41 CreateNSButtonWithResourceIDAndParameter( 42 IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE, language)); 43 [neverTranslateButton_ setTarget:self]; 44 [neverTranslateButton_ setAction:@selector(neverTranslate:)]; 45 46 alwaysTranslateButton_.reset( 47 CreateNSButtonWithResourceIDAndParameter( 48 IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, language)); 49 [alwaysTranslateButton_ setTarget:self]; 50 [alwaysTranslateButton_ setAction:@selector(alwaysTranslate:)]; 51 } 52 53 - (void)layout { 54 MoveControl(label1_, fromLanguagePopUp_, spaceBetweenControls_ / 2, true); 55 MoveControl(fromLanguagePopUp_, label2_, spaceBetweenControls_, true); 56 MoveControl(label2_, okButton_, spaceBetweenControls_, true); 57 MoveControl(okButton_, cancelButton_, spaceBetweenControls_, true); 58 NSView* lastControl = cancelButton_; 59 if (neverTranslateButton_.get()) { 60 MoveControl(lastControl, neverTranslateButton_.get(), 61 spaceBetweenControls_, true); 62 lastControl = neverTranslateButton_.get(); 63 } 64 if (alwaysTranslateButton_.get()) { 65 MoveControl(lastControl, alwaysTranslateButton_.get(), 66 spaceBetweenControls_, true); 67 } 68 } 69 70 - (void)loadLabelText { 71 size_t offset = 0; 72 string16 text = 73 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE, 74 string16(), &offset); 75 NSString* string1 = base::SysUTF16ToNSString(text.substr(0, offset)); 76 NSString* string2 = base::SysUTF16ToNSString(text.substr(offset)); 77 [label1_ setStringValue:string1]; 78 [label2_ setStringValue:string2]; 79 [label3_ setStringValue:@""]; 80 } 81 82 - (NSArray*)visibleControls { 83 NSMutableArray* visibleControls = [NSMutableArray arrayWithObjects: 84 label1_.get(), fromLanguagePopUp_.get(), label2_.get(), 85 okButton_, cancelButton_, nil]; 86 87 if ([self delegate]->ShouldShowNeverTranslateButton()) 88 [visibleControls addObject:neverTranslateButton_.get()]; 89 90 if ([self delegate]->ShouldShowAlwaysTranslateButton()) 91 [visibleControls addObject:alwaysTranslateButton_.get()]; 92 93 return visibleControls; 94 } 95 96 // This is called when the "Never Translate [language]" button is pressed. 97 - (void)neverTranslate:(id)sender { 98 [self delegate]->NeverTranslatePageLanguage(); 99 } 100 101 // This is called when the "Always Translate [language]" button is pressed. 102 - (void)alwaysTranslate:(id)sender { 103 [self delegate]->AlwaysTranslatePageLanguage(); 104 } 105 106 - (bool)verifyLayout { 107 if ([optionsPopUp_ isHidden]) 108 return false; 109 return [super verifyLayout]; 110 } 111 112 @end 113 114 @implementation BeforeTranslateInfobarController (TestingAPI) 115 116 - (NSButton*)alwaysTranslateButton { 117 return alwaysTranslateButton_.get(); 118 } 119 - (NSButton*)neverTranslateButton { 120 return neverTranslateButton_.get(); 121 } 122 123 @end 124