1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_ 6 #define CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_ 7 #pragma once 8 9 #import <Cocoa/Cocoa.h> 10 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" 11 12 #import "base/mac/cocoa_protocols.h" 13 #import "base/memory/scoped_nsobject.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "chrome/browser/translate/languages_menu_model.h" 16 #include "chrome/browser/translate/options_menu_model.h" 17 #include "chrome/browser/translate/translate_infobar_delegate.h" 18 #include "chrome/common/translate_errors.h" 19 20 class TranslateInfoBarMenuModel; 21 22 #pragma mark TranslateInfoBarUtilities helper functions. 23 namespace TranslateInfoBarUtilities { 24 25 // Move the |toMove| view |spacing| pixels before/after the |anchor| view. 26 // |after| signifies the side of |anchor| on which to place |toMove|. 27 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after); 28 29 // Vertically center |toMove| in its container. 30 void VerticallyCenterView(NSView *toMove); 31 // Check that the control |before| is ordered visually before the |after| 32 // control. 33 // Also, check that there is space between them. 34 bool VerifyControlOrderAndSpacing(id before, id after); 35 36 // Creates a label control in the style we need for the translate infobar's 37 // labels within |bounds|. 38 NSTextField* CreateLabel(NSRect bounds); 39 40 // Adds an item with the specified properties to |menu|. 41 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title, 42 int tag, bool enabled, bool checked); 43 44 } // namespace 45 46 // The base class for the three translate infobars. This class does all of the 47 // heavy UI lifting, while deferring to the subclass to tell it what views 48 // should be shown and where. Subclasses need to implement: 49 // - (void)layout; 50 // - (void)loadLabelText; 51 // - (void)visibleControls; 52 // - (bool)verifyLayout; // For testing. 53 @interface TranslateInfoBarControllerBase : InfoBarController<NSMenuDelegate> { 54 @protected 55 scoped_nsobject<NSTextField> label1_; 56 scoped_nsobject<NSTextField> label2_; 57 scoped_nsobject<NSTextField> label3_; 58 scoped_nsobject<NSPopUpButton> fromLanguagePopUp_; 59 scoped_nsobject<NSPopUpButton> toLanguagePopUp_; 60 scoped_nsobject<NSPopUpButton> optionsPopUp_; 61 scoped_nsobject<NSButton> showOriginalButton_; 62 // This is the button used in the translate message infobar. It can either be 63 // a "Try Again" button, or a "Show Original" button in the case that the 64 // page was translated from an unknown language. 65 scoped_nsobject<NSButton> translateMessageButton_; 66 67 // In the current locale, are the "from" and "to" language popup menu 68 // flipped from what they'd appear in English. 69 bool swappedLanguagePlaceholders_; 70 71 // Space between controls in pixels - read from the NIB. 72 CGFloat spaceBetweenControls_; 73 74 scoped_ptr<LanguagesMenuModel> originalLanguageMenuModel_; 75 scoped_ptr<LanguagesMenuModel> targetLanguageMenuModel_; 76 scoped_ptr<OptionsMenuModel> optionsMenuModel_; 77 } 78 79 // Returns the delegate as a TranslateInfoBarDelegate. 80 - (TranslateInfoBarDelegate*)delegate; 81 82 // Called when the "Show Original" button is pressed. 83 - (IBAction)showOriginal:(id)sender; 84 85 @end 86 87 @interface TranslateInfoBarControllerBase (ProtectedAPI) 88 89 // Resizes or hides the options button based on how much space is available 90 // so that it doesn't overlap other buttons. 91 // lastView is the rightmost view, the first one that the options button 92 // would overlap with. 93 - (void)adjustOptionsButtonSizeAndVisibilityForView:(NSView*)lastView; 94 95 // Move all the currently visible views into the correct place for the 96 // current mode. 97 // Must be implemented by the subclass. 98 - (void)layout; 99 100 // Loads the text for the 3 labels. There is only one message, but since 101 // it has controls separating parts of it, it is separated into 3 separate 102 // labels. 103 // Must be implemented by the subclass. 104 - (void)loadLabelText; 105 106 // Returns the controls that are visible in the subclasses infobar. The 107 // default implementation returns an empty array. The controls should 108 // be returned in the order they are displayed, otherwise the layout test 109 // will fail. 110 // Must be implemented by the subclass. 111 - (NSArray*)visibleControls; 112 113 // Shows the array of controls provided by the subclass. 114 - (void)showVisibleControls:(NSArray*)visibleControls; 115 116 // Hides the OK and Cancel buttons. 117 - (void)removeOkCancelButtons; 118 119 // Called when the source or target language selection changes in a menu. 120 // |newLanguageIdx| is the index of the newly selected item in the appropriate 121 // menu. 122 - (void)sourceLanguageModified:(NSInteger)newLanguageIdx; 123 - (void)targetLanguageModified:(NSInteger)newLanguageIdx; 124 125 // Called when an item in one of the toolbar's language or options 126 // menus is selected. 127 - (void)languageMenuChanged:(id)item; 128 - (void)optionsMenuChanged:(id)item; 129 130 // Teardown and rebuild the options menu. When the infobar is small, the 131 // options menu is shrunk to just a drop down arrow, so the title needs 132 // to be empty. 133 - (void)rebuildOptionsMenu:(BOOL)hideTitle; 134 135 // Whether or not this infobar should show the options popup. 136 - (BOOL)shouldShowOptionsPopUp; 137 138 @end // TranslateInfoBarControllerBase (ProtectedAPI) 139 140 #pragma mark TestingAPI 141 142 @interface TranslateInfoBarControllerBase (TestingAPI) 143 144 // All the controls used in any of the translate states. 145 // This is used for verifying layout and for setting the 146 // correct styles on each button. 147 - (NSArray*)allControls; 148 149 // Verifies that the layout of the infobar is correct. 150 // Must be implmented by the subclass. 151 - (bool)verifyLayout; 152 153 // Returns the underlying options menu. 154 - (NSMenu*)optionsMenu; 155 156 // Returns |translateMessageButton_|, see declaration of member 157 // variable for a full description. 158 - (NSButton*)translateMessageButton; 159 160 @end // TranslateInfoBarControllerBase (TestingAPI) 161 162 163 #endif // CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_ 164