Supress proguard warnings for GooglePlayServicesUtil.
[chromium-blink-merge.git] / ui / base / cocoa / three_part_image.mm
blob5fb8916f9f20ba858d640eebec3b2a55a1d5b5d0
1 // Copyright 2015 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 #include "ui/base/cocoa/three_part_image.h"
7 #include "base/logging.h"
8 #include "ui/base/resource/resource_bundle.h"
10 namespace ui {
12 ThreePartImage::ThreePartImage(int left_id, int middle_id, int right_id) {
13   DCHECK(left_id);
14   DCHECK(right_id);
15   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
16   leftImage_.reset(rb.GetNativeImageNamed(left_id).CopyNSImage());
17   rightImage_.reset(rb.GetNativeImageNamed(right_id).CopyNSImage());
18   leftSize_ = [leftImage_ size];
19   rightSize_ = [rightImage_ size];
21   if (middle_id)
22     middleImage_.reset(rb.GetNativeImageNamed(middle_id).CopyNSImage());
25 ThreePartImage::~ThreePartImage() {
28 NSRect ThreePartImage::GetLeftRect(NSRect bounds) const {
29   NSRect left, right;
30   NSDivideRect(bounds, &left, &right, leftSize_.width, NSMinXEdge);
31   return left;
34 NSRect ThreePartImage::GetMiddleRect(NSRect bounds) const {
35   NSRect left, middle, right;
36   NSDivideRect(bounds, &left, &middle, leftSize_.width, NSMinXEdge);
37   NSDivideRect(middle, &right, &middle, rightSize_.width, NSMaxXEdge);
38   return middle;
41 NSRect ThreePartImage::GetRightRect(NSRect bounds) const {
42   NSRect left, right;
43   NSDivideRect(bounds, &right, &left, rightSize_.width, NSMaxXEdge);
44   return right;
47 void ThreePartImage::DrawInRect(NSRect rect,
48                                 NSCompositingOperation op,
49                                 CGFloat alpha) const {
50   rect.size.height = leftSize_.height;
51   NSDrawThreePartImage(rect, leftImage_, middleImage_, rightImage_,
52                        NO, op, alpha, NO);
55 BOOL ThreePartImage::HitTest(NSPoint point, NSRect bounds) const {
56   NSRect middleRect = GetMiddleRect(bounds);
57   if (NSPointInRect(point, middleRect))
58     return middleImage_ ? HitTestImage(point, middleImage_, middleRect) : YES;
60   NSRect leftRect = GetLeftRect(bounds);
61   if (NSPointInRect(point, leftRect))
62     return HitTestImage(point, leftImage_, leftRect);
64   NSRect rightRect = GetRightRect(bounds);
65   if (NSPointInRect(point, rightRect))
66     return HitTestImage(point, rightImage_, rightRect);
68   return NO;
71 BOOL ThreePartImage::HitTestImage(NSPoint point,
72                                   NSImage* image,
73                                   NSRect imageRect) const {
74   NSRect pointRect = NSMakeRect(point.x, point.y, 1, 1);
75   return [image hitTestRect:pointRect
76       withImageDestinationRect:imageRect
77                        context:nil
78                          hints:nil
79                        flipped:NO];
82 }  // namespace ui