OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/infobars/translate_infobar_base.h" | 5 #import "chrome/browser/ui/cocoa/infobars/translate_infobar_base.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
11 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
12 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
13 #include "chrome/browser/translate/translate_infobar_delegate.h" | 11 #include "chrome/browser/translate/translate_infobar_delegate.h" |
14 #import "chrome/browser/ui/cocoa/hover_close_button.h" | 12 #import "chrome/browser/ui/cocoa/hover_close_button.h" |
15 #include "chrome/browser/ui/cocoa/infobars/after_translate_infobar_controller.h" | 13 #include "chrome/browser/ui/cocoa/infobars/after_translate_infobar_controller.h" |
16 #import "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h" | 14 #import "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h" |
17 #include "chrome/browser/ui/cocoa/infobars/infobar.h" | 15 #include "chrome/browser/ui/cocoa/infobars/infobar.h" |
18 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 16 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" |
19 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | 17 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" |
20 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h" | 18 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h" |
| 19 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h" |
21 #include "chrome/browser/ui/cocoa/infobars/translate_message_infobar_controller.
h" | 20 #include "chrome/browser/ui/cocoa/infobars/translate_message_infobar_controller.
h" |
22 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
23 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 22 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
24 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
25 | 24 |
26 using TranslateInfoBarUtilities::MoveControl; | 25 using InfoBarUtilities::MoveControl; |
27 using TranslateInfoBarUtilities::VerticallyCenterView; | 26 using InfoBarUtilities::VerticallyCenterView; |
28 using TranslateInfoBarUtilities::VerifyControlOrderAndSpacing; | 27 using InfoBarUtilities::VerifyControlOrderAndSpacing; |
29 using TranslateInfoBarUtilities::CreateLabel; | 28 using InfoBarUtilities::CreateLabel; |
30 using TranslateInfoBarUtilities::AddMenuItem; | 29 using InfoBarUtilities::AddMenuItem; |
31 | |
32 #pragma mark TranslateInfoBarUtilities helper functions. | |
33 | |
34 namespace TranslateInfoBarUtilities { | |
35 | |
36 // Move the |toMove| view |spacing| pixels before/after the |anchor| view. | |
37 // |after| signifies the side of |anchor| on which to place |toMove|. | |
38 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) { | |
39 NSRect anchorFrame = [anchor frame]; | |
40 NSRect toMoveFrame = [toMove frame]; | |
41 | |
42 // At the time of this writing, OS X doesn't natively support BiDi UIs, but | |
43 // it doesn't hurt to be forward looking. | |
44 bool toRight = after; | |
45 | |
46 if (toRight) { | |
47 toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing; | |
48 } else { | |
49 // Place toMove to theleft of anchor. | |
50 toMoveFrame.origin.x = NSMinX(anchorFrame) - | |
51 spacing - NSWidth(toMoveFrame); | |
52 } | |
53 [toMove setFrame:toMoveFrame]; | |
54 } | |
55 | |
56 // Check that the control |before| is ordered visually before the |after| | |
57 // control. | |
58 // Also, check that there is space between them. | |
59 bool VerifyControlOrderAndSpacing(id before, id after) { | |
60 NSRect beforeFrame = [before frame]; | |
61 NSRect afterFrame = [after frame]; | |
62 return NSMinX(afterFrame) >= NSMaxX(beforeFrame); | |
63 } | |
64 | |
65 // Vertically center |toMove| in its container. | |
66 void VerticallyCenterView(NSView* toMove) { | |
67 NSRect superViewFrame = [[toMove superview] frame]; | |
68 NSRect viewFrame = [toMove frame]; | |
69 // If the superview is the infobar view, then subtract out the anti-spoof | |
70 // height so that the content is centered in the content area of the infobar, | |
71 // rather than in the total height (which includes the bulge). | |
72 CGFloat superHeight = NSHeight(superViewFrame); | |
73 if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]]) | |
74 superHeight = infobars::kBaseHeight; | |
75 viewFrame.origin.y = | |
76 floor((superHeight - NSHeight(viewFrame)) / 2.0); | |
77 [toMove setFrame:viewFrame]; | |
78 } | |
79 | |
80 // Creates a label control in the style we need for the translate infobar's | |
81 // labels within |bounds|. | |
82 NSTextField* CreateLabel(NSRect bounds) { | |
83 NSTextField* ret = [[NSTextField alloc] initWithFrame:bounds]; | |
84 [ret setEditable:NO]; | |
85 [ret setDrawsBackground:NO]; | |
86 [ret setBordered:NO]; | |
87 return ret; | |
88 } | |
89 | |
90 // Adds an item with the specified properties to |menu|. | |
91 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title, | |
92 int tag, bool enabled, bool checked) { | |
93 if (tag == -1) { | |
94 [menu addItem:[NSMenuItem separatorItem]]; | |
95 } else { | |
96 NSMenuItem* item = [[[NSMenuItem alloc] | |
97 initWithTitle:title | |
98 action:selector | |
99 keyEquivalent:@""] autorelease]; | |
100 [item setTag:tag]; | |
101 [menu addItem:item]; | |
102 [item setTarget:target]; | |
103 if (checked) | |
104 [item setState:NSOnState]; | |
105 if (!enabled) | |
106 [item setEnabled:NO]; | |
107 } | |
108 } | |
109 | |
110 } // namespace TranslateInfoBarUtilities | |
111 | 30 |
112 // TranslateInfoBarDelegate views specific method: | 31 // TranslateInfoBarDelegate views specific method: |
113 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* owner) { | 32 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* owner) { |
114 TranslateInfoBarControllerBase* infobar_controller = NULL; | 33 TranslateInfoBarControllerBase* infobar_controller = NULL; |
115 switch (type_) { | 34 switch (type_) { |
116 case BEFORE_TRANSLATE: | 35 case BEFORE_TRANSLATE: |
117 infobar_controller = | 36 infobar_controller = |
118 [[BeforeTranslateInfobarController alloc] initWithDelegate:this | 37 [[BeforeTranslateInfobarController alloc] initWithDelegate:this |
119 owner:owner]; | 38 owner:owner]; |
120 break; | 39 break; |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 [[control description] UTF8String] <<[title UTF8String]; | 570 [[control description] UTF8String] <<[title UTF8String]; |
652 return false; | 571 return false; |
653 } | 572 } |
654 previousControl = control; | 573 previousControl = control; |
655 } | 574 } |
656 | 575 |
657 return true; | 576 return true; |
658 } | 577 } |
659 | 578 |
660 @end // TranslateInfoBarControllerBase (TestingAPI) | 579 @end // TranslateInfoBarControllerBase (TestingAPI) |
OLD | NEW |