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