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.
5 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
7 #include "base/logging.h"
8 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSBezierPath+RoundRect.h"
10 @implementation InfoBubbleView
12 @synthesize arrowLocation = arrowLocation_;
13 @synthesize alignment = alignment_;
14 @synthesize cornerFlags = cornerFlags_;
16 - (id)initWithFrame:(NSRect)frameRect {
17 if ((self = [super initWithFrame:frameRect])) {
18 arrowLocation_ = info_bubble::kTopLeft;
19 alignment_ = info_bubble::kAlignArrowToAnchor;
20 cornerFlags_ = info_bubble::kRoundedAllCorners;
21 backgroundColor_.reset([[NSColor whiteColor] retain]);
26 - (void)drawRect:(NSRect)rect {
27 // Make room for the border to be seen.
28 NSRect bounds = [self bounds];
29 if (arrowLocation_ != info_bubble::kNoArrow) {
30 bounds.size.height -= info_bubble::kBubbleArrowHeight;
32 rect.size.height -= info_bubble::kBubbleArrowHeight;
34 float topRadius = cornerFlags_ & info_bubble::kRoundedTopCorners ?
35 info_bubble::kBubbleCornerRadius : 0;
36 float bottomRadius = cornerFlags_ & info_bubble::kRoundedBottomCorners ?
37 info_bubble::kBubbleCornerRadius : 0;
39 NSBezierPath* bezier =
40 [NSBezierPath gtm_bezierPathWithRoundRect:bounds
41 topLeftCornerRadius:topRadius
42 topRightCornerRadius:topRadius
43 bottomLeftCornerRadius:bottomRadius
44 bottomRightCornerRadius:bottomRadius];
46 // Add the bubble arrow.
48 switch (arrowLocation_) {
49 case info_bubble::kTopLeft:
50 dX = info_bubble::kBubbleArrowXOffset;
52 case info_bubble::kTopRight:
53 dX = NSWidth(bounds) - info_bubble::kBubbleArrowXOffset -
54 info_bubble::kBubbleArrowWidth;
56 case info_bubble::kTopCenter:
57 dX = NSMidX(bounds) - info_bubble::kBubbleArrowWidth / 2.0;
59 case info_bubble::kNoArrow:
65 NSPoint arrowStart = NSMakePoint(NSMinX(bounds), NSMaxY(bounds));
67 [bezier moveToPoint:NSMakePoint(arrowStart.x, arrowStart.y)];
68 if (arrowLocation_ != info_bubble::kNoArrow) {
69 [bezier lineToPoint:NSMakePoint(arrowStart.x +
70 info_bubble::kBubbleArrowWidth / 2.0,
72 info_bubble::kBubbleArrowHeight)];
74 [bezier lineToPoint:NSMakePoint(arrowStart.x + info_bubble::kBubbleArrowWidth,
77 [backgroundColor_ set];
82 NSRect bounds = [self bounds];
84 info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth / 2.0;
85 CGFloat xOffset = 0.0;
86 switch(arrowLocation_) {
87 case info_bubble::kTopRight:
88 xOffset = NSMaxX(bounds) - tipXOffset;
90 case info_bubble::kTopLeft:
91 xOffset = NSMinX(bounds) + tipXOffset;
93 case info_bubble::kTopCenter:
94 xOffset = NSMidX(bounds);
100 NSPoint arrowTip = NSMakePoint(xOffset, NSMaxY(bounds));
104 - (NSColor*)backgroundColor {
105 return backgroundColor_;
108 - (void)setBackgroundColor:(NSColor*)backgroundColor {
109 backgroundColor_.reset([backgroundColor retain]);