info_bubble_view.mm revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/info_bubble_view.h"
6
7#include "base/logging.h"
8#include "base/scoped_nsobject.h"
9
10@implementation InfoBubbleView
11
12@synthesize arrowLocation = arrowLocation_;
13
14- (id)initWithFrame:(NSRect)frameRect {
15  if ((self = [super initWithFrame:frameRect])) {
16    arrowLocation_ = info_bubble::kTopLeft;
17  }
18  return self;
19}
20
21- (void)drawRect:(NSRect)rect {
22  // Make room for the border to be seen.
23  NSRect bounds = [self bounds];
24  bounds.size.height -= info_bubble::kBubbleArrowHeight;
25  NSBezierPath* bezier = [NSBezierPath bezierPath];
26  rect.size.height -= info_bubble::kBubbleArrowHeight;
27
28  // Start with a rounded rectangle.
29  [bezier appendBezierPathWithRoundedRect:bounds
30                                  xRadius:info_bubble::kBubbleCornerRadius
31                                  yRadius:info_bubble::kBubbleCornerRadius];
32
33  // Add the bubble arrow.
34  CGFloat dX = 0;
35  switch (arrowLocation_) {
36    case info_bubble::kTopLeft:
37      dX = info_bubble::kBubbleArrowXOffset;
38      break;
39    case info_bubble::kTopRight:
40      dX = NSWidth(bounds) - info_bubble::kBubbleArrowXOffset -
41          info_bubble::kBubbleArrowWidth;
42      break;
43    default:
44      NOTREACHED();
45      break;
46  }
47  NSPoint arrowStart = NSMakePoint(NSMinX(bounds), NSMaxY(bounds));
48  arrowStart.x += dX;
49  [bezier moveToPoint:NSMakePoint(arrowStart.x, arrowStart.y)];
50  [bezier lineToPoint:NSMakePoint(arrowStart.x +
51                                      info_bubble::kBubbleArrowWidth / 2.0,
52                                  arrowStart.y +
53                                      info_bubble::kBubbleArrowHeight)];
54  [bezier lineToPoint:NSMakePoint(arrowStart.x + info_bubble::kBubbleArrowWidth,
55                                  arrowStart.y)];
56  [bezier closePath];
57  [[NSColor whiteColor] set];
58  [bezier fill];
59}
60
61- (NSPoint)arrowTip {
62  NSRect bounds = [self bounds];
63  CGFloat tipXOffset =
64      info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth / 2.0;
65  CGFloat xOffset =
66      (arrowLocation_ == info_bubble::kTopRight) ? NSMaxX(bounds) - tipXOffset :
67                                                   NSMinX(bounds) + tipXOffset;
68  NSPoint arrowTip = NSMakePoint(xOffset, NSMaxY(bounds));
69  return arrowTip;
70}
71
72@end
73