summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/translate/translate_infobar_base.h
blob: 66612eb14b4182a70a1de7295c46c8e281e8541e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_
#define CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_
#pragma once

#import <Cocoa/Cocoa.h>
#import "chrome/browser/ui/cocoa/infobars/infobar_controller.h"

#import "base/mac/cocoa_protocols.h"
#import "base/scoped_nsobject.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/translate/languages_menu_model.h"
#include "chrome/browser/translate/options_menu_model.h"
#include "chrome/browser/translate/translate_infobar_delegate.h"
#include "chrome/common/translate_errors.h"

class TranslateInfoBarMenuModel;

#pragma mark TranslateInfoBarUtilities helper functions.
namespace TranslateInfoBarUtilities {

// Move the |toMove| view |spacing| pixels before/after the |anchor| view.
// |after| signifies the side of |anchor| on which to place |toMove|.
void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after);

// Vertically center |toMove| in its container.
void VerticallyCenterView(NSView *toMove);
// Check that the control |before| is ordered visually before the |after|
// control.
// Also, check that there is space between them.
bool VerifyControlOrderAndSpacing(id before, id after);

// Creates a label control in the style we need for the translate infobar's
// labels within |bounds|.
NSTextField* CreateLabel(NSRect bounds);

// Adds an item with the specified properties to |menu|.
void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
    int tag, bool enabled, bool checked);

}  // namespace

// The base class for the three translate infobars.  This class does all of the
// heavy UI lifting, while deferring to the subclass to tell it what views
// should be shown and where.  Subclasses need to implement:
// - (void)layout;
// - (void)loadLabelText;
// - (void)visibleControls;
// - (bool)verifyLayout; // For testing.
@interface TranslateInfoBarControllerBase : InfoBarController<NSMenuDelegate> {
 @protected
  scoped_nsobject<NSTextField> label1_;
  scoped_nsobject<NSTextField> label2_;
  scoped_nsobject<NSTextField> label3_;
  scoped_nsobject<NSPopUpButton> fromLanguagePopUp_;
  scoped_nsobject<NSPopUpButton> toLanguagePopUp_;
  scoped_nsobject<NSPopUpButton> optionsPopUp_;
  scoped_nsobject<NSButton> showOriginalButton_;
  // This is the button used in the translate message infobar.  It can either be
  // a "Try Again" button, or a "Show Original" button in the case that the
  // page was translated from an unknown language.
  scoped_nsobject<NSButton> translateMessageButton_;

  // In the current locale, are the "from" and "to" language popup menu
  // flipped from what they'd appear in English.
  bool swappedLanguagePlaceholders_;

  // Space between controls in pixels - read from the NIB.
  CGFloat spaceBetweenControls_;

  scoped_ptr<LanguagesMenuModel> originalLanguageMenuModel_;
  scoped_ptr<LanguagesMenuModel> targetLanguageMenuModel_;
  scoped_ptr<OptionsMenuModel> optionsMenuModel_;
}

// Returns the delegate as a TranslateInfoBarDelegate.
- (TranslateInfoBarDelegate*)delegate;

// Called when the "Show Original" button is pressed.
- (IBAction)showOriginal:(id)sender;

@end

@interface TranslateInfoBarControllerBase (ProtectedAPI)

// Resizes or hides the options button based on how much space is available
// so that it doesn't overlap other buttons.
// lastView is the rightmost view, the first one that the options button
// would overlap with.
- (void)adjustOptionsButtonSizeAndVisibilityForView:(NSView*)lastView;

// Move all the currently visible views into the correct place for the
// current mode.
// Must be implemented by the subclass.
- (void)layout;

// Loads the text for the 3 labels.  There is only one message, but since
// it has controls separating parts of it, it is separated into 3 separate
// labels.
// Must be implemented by the subclass.
- (void)loadLabelText;

// Returns the controls that are visible in the subclasses infobar.  The
// default implementation returns an empty array. The controls should
// be returned in the order they are displayed, otherwise the layout test
// will fail.
// Must be implemented by the subclass.
- (NSArray*)visibleControls;

// Shows the array of controls provided by the subclass.
- (void)showVisibleControls:(NSArray*)visibleControls;

// Hides the OK and Cancel buttons.
- (void)removeOkCancelButtons;

// Called when the source or target language selection changes in a menu.
// |newLanguageIdx| is the index of the newly selected item in the appropriate
// menu.
- (void)sourceLanguageModified:(NSInteger)newLanguageIdx;
- (void)targetLanguageModified:(NSInteger)newLanguageIdx;

// Called when an item in one of the toolbar's language or options
// menus is selected.
- (void)languageMenuChanged:(id)item;
- (void)optionsMenuChanged:(id)item;

// Teardown and rebuild the options menu.  When the infobar is small, the
// options menu is shrunk to just a drop down arrow, so the title needs
// to be empty.
- (void)rebuildOptionsMenu:(BOOL)hideTitle;

// Whether or not this infobar should show the options popup.
- (BOOL)shouldShowOptionsPopUp;

@end // TranslateInfoBarControllerBase (ProtectedAPI)

#pragma mark TestingAPI

@interface TranslateInfoBarControllerBase (TestingAPI)

// All the controls used in any of the translate states.
// This is used for verifying layout and for setting the
// correct styles on each button.
- (NSArray*)allControls;

// Verifies that the layout of the infobar is correct.
// Must be implmented by the subclass.
- (bool)verifyLayout;

// Returns the underlying options menu.
- (NSMenu*)optionsMenu;

// Returns |translateMessageButton_|, see declaration of member
// variable for a full description.
- (NSButton*)translateMessageButton;

@end // TranslateInfoBarControllerBase (TestingAPI)


#endif // CHROME_BROWSER_UI_COCOA_TRANSLATE_INFOBAR_BASE_H_