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"
12 ThreePartImage::ThreePartImage(int left_id, int middle_id, int 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];
22 middleImage_.reset(rb.GetNativeImageNamed(middle_id).CopyNSImage());
25 ThreePartImage::~ThreePartImage() {
28 NSRect ThreePartImage::GetLeftRect(NSRect bounds) const {
30 NSDivideRect(bounds, &left, &right, leftSize_.width, NSMinXEdge);
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);
41 NSRect ThreePartImage::GetRightRect(NSRect bounds) const {
43 NSDivideRect(bounds, &right, &left, rightSize_.width, NSMaxXEdge);
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_,
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);
71 BOOL ThreePartImage::HitTestImage(NSPoint point,
73 NSRect imageRect) const {
74 NSRect pointRect = NSMakeRect(point.x, point.y, 1, 1);
75 return [image hitTestRect:pointRect
76 withImageDestinationRect:imageRect