summaryrefslogtreecommitdiffstats
path: root/ui/base/cocoa/three_part_image.mm
blob: 18ec2ddbfd040e04e09e613b046a66ac4ef542d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/cocoa/three_part_image.h"

#include "base/logging.h"
#include "ui/base/resource/resource_bundle.h"

namespace ui {

ThreePartImage::ThreePartImage(NSImage* left, NSImage* middle, NSImage* right) {
  DCHECK(left);
  DCHECK(right);
  leftImage_.reset([left retain]);
  rightImage_.reset([right retain]);
  leftSize_ = [leftImage_ size];
  rightSize_ = [rightImage_ size];

  if (middle) {
    middleImage_.reset([middle retain]);
  }
}

ThreePartImage::~ThreePartImage() {
}

NSRect ThreePartImage::GetLeftRect(NSRect bounds) const {
  NSRect left, right;
  NSDivideRect(bounds, &left, &right, leftSize_.width, NSMinXEdge);
  return left;
}

NSRect ThreePartImage::GetMiddleRect(NSRect bounds) const {
  NSRect left, middle, right;
  NSDivideRect(bounds, &left, &middle, leftSize_.width, NSMinXEdge);
  NSDivideRect(middle, &right, &middle, rightSize_.width, NSMaxXEdge);
  return middle;
}

NSRect ThreePartImage::GetRightRect(NSRect bounds) const {
  NSRect left, right;
  NSDivideRect(bounds, &right, &left, rightSize_.width, NSMaxXEdge);
  return right;
}

void ThreePartImage::DrawInRect(NSRect rect,
                                NSCompositingOperation op,
                                CGFloat alpha) const {
  rect.size.height = leftSize_.height;
  NSDrawThreePartImage(rect, leftImage_, middleImage_, rightImage_,
                       NO, op, alpha, NO);
}

BOOL ThreePartImage::HitTest(NSPoint point, NSRect bounds) const {
  NSRect middleRect = GetMiddleRect(bounds);
  if (NSPointInRect(point, middleRect))
    return middleImage_ ? HitTestImage(point, middleImage_, middleRect) : YES;

  NSRect leftRect = GetLeftRect(bounds);
  if (NSPointInRect(point, leftRect))
    return HitTestImage(point, leftImage_, leftRect);

  NSRect rightRect = GetRightRect(bounds);
  if (NSPointInRect(point, rightRect))
    return HitTestImage(point, rightImage_, rightRect);

  return NO;
}

BOOL ThreePartImage::HitTestImage(NSPoint point,
                                  NSImage* image,
                                  NSRect imageRect) const {
  NSRect pointRect = NSMakeRect(point.x, point.y, 1, 1);
  return [image hitTestRect:pointRect
      withImageDestinationRect:imageRect
                       context:nil
                         hints:nil
                       flipped:NO];
}

}  // namespace ui