OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/base/cocoa/three_part_image.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ui/base/resource/resource_bundle.h" |
| 9 |
| 10 namespace ui { |
| 11 |
| 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]; |
| 20 |
| 21 if (middle_id) |
| 22 middleImage_.reset(rb.GetNativeImageNamed(middle_id).CopyNSImage()); |
| 23 } |
| 24 |
| 25 ThreePartImage::~ThreePartImage() { |
| 26 } |
| 27 |
| 28 NSRect ThreePartImage::GetLeftRect(NSRect bounds) const { |
| 29 NSRect left, right; |
| 30 NSDivideRect(bounds, &left, &right, leftSize_.width, NSMinXEdge); |
| 31 return left; |
| 32 } |
| 33 |
| 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; |
| 39 } |
| 40 |
| 41 NSRect ThreePartImage::GetRightRect(NSRect bounds) const { |
| 42 NSRect left, right; |
| 43 NSDivideRect(bounds, &right, &left, rightSize_.width, NSMaxXEdge); |
| 44 return right; |
| 45 } |
| 46 |
| 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); |
| 53 } |
| 54 |
| 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; |
| 59 |
| 60 NSRect leftRect = GetLeftRect(bounds); |
| 61 if (NSPointInRect(point, leftRect)) |
| 62 return HitTestImage(point, leftImage_, leftRect); |
| 63 |
| 64 NSRect rightRect = GetRightRect(bounds); |
| 65 if (NSPointInRect(point, rightRect)) |
| 66 return HitTestImage(point, rightImage_, rightRect); |
| 67 |
| 68 return NO; |
| 69 } |
| 70 |
| 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]; |
| 80 } |
| 81 |
| 82 } // namespace ui |
OLD | NEW |