Home | History | Annotate | Download | only in location_bar
      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 #include <cmath>
      6 
      7 #import "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
      8 
      9 #import "chrome/browser/ui/cocoa/image_utils.h"
     10 
     11 ImageDecoration::ImageDecoration() {
     12 }
     13 
     14 ImageDecoration::~ImageDecoration() {
     15 }
     16 
     17 NSImage* ImageDecoration::GetImage() {
     18   return image_;
     19 }
     20 
     21 void ImageDecoration::SetImage(NSImage* image) {
     22   image_.reset([image retain]);
     23 }
     24 
     25 NSRect ImageDecoration::GetDrawRectInFrame(NSRect frame) {
     26   NSImage* image = GetImage();
     27   if (!image)
     28     return frame;
     29 
     30   // Center the image within the frame.
     31   const CGFloat delta_height = NSHeight(frame) - [image size].height;
     32   const CGFloat y_inset = std::floor(delta_height / 2.0);
     33   const CGFloat delta_width = NSWidth(frame) - [image size].width;
     34   const CGFloat x_inset = std::floor(delta_width / 2.0);
     35   return NSInsetRect(frame, x_inset, y_inset);
     36 }
     37 
     38 CGFloat ImageDecoration::GetWidthForSpace(CGFloat width) {
     39   NSImage* image = GetImage();
     40   if (image) {
     41     const CGFloat image_width = [image size].width;
     42     if (image_width <= width)
     43       return image_width;
     44   }
     45   return kOmittedWidth;
     46 }
     47 
     48 void ImageDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
     49   [GetImage() drawInRect:GetDrawRectInFrame(frame)
     50                 fromRect:NSZeroRect  // Entire image
     51                operation:NSCompositeSourceOver
     52                 fraction:1.0
     53             neverFlipped:YES];
     54 }
     55