Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / infobars / infobar_utilities.mm
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.
4
5 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h"
6
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
9 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
10 #import "components/infobars/core/infobar.h"
11 #import "ui/base/cocoa/nsview_additions.h"
12
13 @interface InfobarLabelTextField : NSTextField
14 @end
15
16 @implementation InfobarLabelTextField
17
18 - (void)drawRect:(NSRect)rect {
19   NSView* infobarGradientView = [self superview];
20   [self cr_drawUsingAncestor:infobarGradientView inRect:rect];
21   [super drawRect:rect];
22 }
23
24 - (BOOL)isOpaque {
25   return YES;
26 }
27
28 @end
29
30 namespace InfoBarUtilities {
31
32 // Move the |toMove| view |spacing| pixels before/after the |anchor| view.
33 // |after| signifies the side of |anchor| on which to place |toMove|.
34 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) {
35   NSRect anchorFrame = [anchor frame];
36   NSRect toMoveFrame = [toMove frame];
37
38   // At the time of this writing, OS X doesn't natively support BiDi UIs, but
39   // it doesn't hurt to be forward looking.
40   bool toRight = after;
41
42   if (toRight) {
43     toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing;
44   } else {
45     // Place toMove to theleft of anchor.
46     toMoveFrame.origin.x = NSMinX(anchorFrame) -
47         spacing - NSWidth(toMoveFrame);
48   }
49   [toMove setFrame:toMoveFrame];
50 }
51
52 // Check that the control |before| is ordered visually before the |after|
53 // control. Also, check that there is space between them.
54 bool VerifyControlOrderAndSpacing(id before, id after) {
55   NSRect beforeFrame = [before frame];
56   NSRect afterFrame = [after frame];
57   return NSMinX(afterFrame) >= NSMaxX(beforeFrame);
58 }
59
60 // Vertically center |toMove| in its container.
61 void VerticallyCenterView(NSView* toMove) {
62   NSRect superViewFrame = [[toMove superview] frame];
63   NSRect viewFrame = [toMove frame];
64   // If the superview is the infobar view, then subtract out the anti-spoof
65   // height so that the content is centered in the content area of the infobar,
66   // rather than in the total height (which includes the bulge).
67   CGFloat superHeight = NSHeight(superViewFrame);
68   if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]])
69     superHeight = infobars::InfoBar::kDefaultBarTargetHeight;
70   viewFrame.origin.y =
71       floor((superHeight - NSHeight(viewFrame)) / 2.0);
72   [toMove setFrame:viewFrame];
73 }
74
75 // Creates a label control in the style we need for the infobar's labels
76 // within |bounds|.
77 NSTextField* CreateLabel(NSRect bounds) {
78   NSTextField* ret = [[InfobarLabelTextField alloc] initWithFrame:bounds];
79   [ret setEditable:NO];
80   [ret setDrawsBackground:NO];
81   [ret setBordered:NO];
82   return ret;
83 }
84
85 // Adds an item with the specified properties to |menu|.
86 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
87     int tag, bool enabled, bool checked) {
88   if (tag == -1) {
89     [menu addItem:[NSMenuItem separatorItem]];
90   } else {
91     base::scoped_nsobject<NSMenuItem> item(
92         [[NSMenuItem alloc] initWithTitle:title
93                                    action:selector
94                             keyEquivalent:@""]);
95     [item setTag:tag];
96     [menu addItem:item];
97     [item setTarget:target];
98     if (checked)
99       [item setState:NSOnState];
100     if (!enabled)
101       [item setEnabled:NO];
102   }
103 }
104
105 }  // namespace InfoBarUtilities