Home | History | Annotate | Download | only in cocoa
      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/info_bubble_view.h"
      6 
      7 #include "base/logging.h"
      8 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h"
      9 
     10 @implementation InfoBubbleView
     11 
     12 @synthesize arrowLocation = arrowLocation_;
     13 @synthesize alignment = alignment_;
     14 @synthesize cornerFlags = cornerFlags_;
     15 
     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]);
     22   }
     23   return self;
     24 }
     25 
     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;
     31   }
     32   rect.size.height -= info_bubble::kBubbleArrowHeight;
     33 
     34   float topRadius = cornerFlags_ & info_bubble::kRoundedTopCorners ?
     35       info_bubble::kBubbleCornerRadius : 0;
     36   float bottomRadius = cornerFlags_ & info_bubble::kRoundedBottomCorners ?
     37       info_bubble::kBubbleCornerRadius : 0;
     38 
     39   NSBezierPath* bezier =
     40       [NSBezierPath gtm_bezierPathWithRoundRect:bounds
     41                             topLeftCornerRadius:topRadius
     42                            topRightCornerRadius:topRadius
     43                          bottomLeftCornerRadius:bottomRadius
     44                         bottomRightCornerRadius:bottomRadius];
     45 
     46   // Add the bubble arrow.
     47   CGFloat dX = 0;
     48   switch (arrowLocation_) {
     49     case info_bubble::kTopLeft:
     50       dX = info_bubble::kBubbleArrowXOffset;
     51       break;
     52     case info_bubble::kTopRight:
     53       dX = NSWidth(bounds) - info_bubble::kBubbleArrowXOffset -
     54           info_bubble::kBubbleArrowWidth;
     55       break;
     56     case info_bubble::kNoArrow:
     57       break;
     58     default:
     59       NOTREACHED();
     60       break;
     61   }
     62   NSPoint arrowStart = NSMakePoint(NSMinX(bounds), NSMaxY(bounds));
     63   arrowStart.x += dX;
     64   [bezier moveToPoint:NSMakePoint(arrowStart.x, arrowStart.y)];
     65   if (arrowLocation_ != info_bubble::kNoArrow) {
     66     [bezier lineToPoint:NSMakePoint(arrowStart.x +
     67                                         info_bubble::kBubbleArrowWidth / 2.0,
     68                                     arrowStart.y +
     69                                         info_bubble::kBubbleArrowHeight)];
     70   }
     71   [bezier lineToPoint:NSMakePoint(arrowStart.x + info_bubble::kBubbleArrowWidth,
     72                                   arrowStart.y)];
     73   [bezier closePath];
     74   [backgroundColor_ set];
     75   [bezier fill];
     76 }
     77 
     78 - (NSPoint)arrowTip {
     79   NSRect bounds = [self bounds];
     80   CGFloat tipXOffset =
     81       info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth / 2.0;
     82   CGFloat xOffset =
     83       (arrowLocation_ == info_bubble::kTopRight) ? NSMaxX(bounds) - tipXOffset :
     84                                                    NSMinX(bounds) + tipXOffset;
     85   NSPoint arrowTip = NSMakePoint(xOffset, NSMaxY(bounds));
     86   return arrowTip;
     87 }
     88 
     89 - (NSColor*)backgroundColor {
     90   return backgroundColor_;
     91 }
     92 
     93 - (void)setBackgroundColor:(NSColor*)backgroundColor {
     94   backgroundColor_.reset([backgroundColor retain]);
     95 }
     96 
     97 @end
     98