| OLD | NEW |
| (Empty) | |
| 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/cocoa/location_bar/bubble_decoration.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "chrome/browser/cocoa/image_utils.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 // Padding between the icon/label and bubble edges. |
| 13 const CGFloat kBubblePadding = 7.0; |
| 14 |
| 15 // How far to inset image from boundary rect. |
| 16 const CGFloat kImageInset = 3.0; |
| 17 |
| 18 // How far to inset the keywork token from sides. |
| 19 const NSInteger kKeywordYInset = 4; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 BubbleDecoration::BubbleDecoration(NSFont* font) { |
| 24 DCHECK(font); |
| 25 if (font) { |
| 26 NSDictionary* attributes = |
| 27 [NSDictionary dictionaryWithObject:font |
| 28 forKey:NSFontAttributeName]; |
| 29 attributes_.reset([attributes retain]); |
| 30 } |
| 31 } |
| 32 |
| 33 BubbleDecoration::~BubbleDecoration() { |
| 34 } |
| 35 |
| 36 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 37 NSString* label) { |
| 38 if (!image && !label) |
| 39 return kOmittedWidth; |
| 40 |
| 41 const CGFloat image_width = image ? [image size].width : 0.0; |
| 42 if (!label) |
| 43 return kBubblePadding + image_width; |
| 44 |
| 45 const CGFloat label_width = [label sizeWithAttributes:attributes_].width; |
| 46 return kBubblePadding + image_width + label_width; |
| 47 } |
| 48 |
| 49 CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width) { |
| 50 const CGFloat all_width = GetWidthForImageAndLabel(image_, label_); |
| 51 if (all_width <= width) |
| 52 return all_width; |
| 53 |
| 54 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); |
| 55 if (image_width <= width) |
| 56 return image_width; |
| 57 |
| 58 return kOmittedWidth; |
| 59 } |
| 60 |
| 61 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 62 const NSRect decorationFrame = NSInsetRect(frame, 0.0, kKeywordYInset); |
| 63 |
| 64 const NSRect bubbleFrame = NSInsetRect(decorationFrame, 0.5, 0.5); |
| 65 NSBezierPath* path = |
| 66 [NSBezierPath bezierPathWithRoundedRect:bubbleFrame |
| 67 xRadius:4.0 |
| 68 yRadius:4.0]; |
| 69 |
| 70 [background_color_ setFill]; |
| 71 [path fill]; |
| 72 |
| 73 [border_color_ setStroke]; |
| 74 [path setLineWidth:1.0]; |
| 75 [path stroke]; |
| 76 |
| 77 NSRect imageRect = NSOffsetRect(decorationFrame, kImageInset, kImageInset); |
| 78 if (image_) { |
| 79 imageRect.size = [image_ size]; |
| 80 [image_ drawInRect:imageRect |
| 81 fromRect:NSZeroRect // Entire image |
| 82 operation:NSCompositeSourceOver |
| 83 fraction:1.0 |
| 84 neverFlipped:YES]; |
| 85 } else { |
| 86 imageRect.size = NSZeroSize; |
| 87 } |
| 88 |
| 89 if (label_) { |
| 90 NSRect textRect = decorationFrame; |
| 91 textRect.origin.x = NSMaxX(imageRect); |
| 92 textRect.size.width = NSMaxX(decorationFrame) - NSMinX(textRect); |
| 93 [text_color_ set]; |
| 94 [label_ drawInRect:textRect withAttributes:attributes_]; |
| 95 } |
| 96 } |
| 97 |
| 98 void BubbleDecoration::SetImage(NSImage* image) { |
| 99 image_.reset([image retain]); |
| 100 } |
| 101 |
| 102 void BubbleDecoration::SetLabel(NSString* label) { |
| 103 // If the initializer was called with |nil|, then the code cannot |
| 104 // process a label. |
| 105 DCHECK(attributes_); |
| 106 if (attributes_) |
| 107 label_.reset([label copy]); |
| 108 } |
| 109 |
| 110 void BubbleDecoration::SetColors(NSColor* border_color, |
| 111 NSColor* background_color, |
| 112 NSColor* text_color) { |
| 113 border_color_.reset([border_color retain]); |
| 114 background_color_.reset([background_color retain]); |
| 115 text_color_.reset([text_color retain]); |
| 116 } |
| OLD | NEW |