1// Copyright (c) 2011 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#import "chrome/browser/ui/cocoa/new_tab_button.h"
6#import "chrome/browser/ui/cocoa/image_button_cell.h"
7#include "grit/theme_resources.h"
8#include "ui/base/resource/resource_bundle.h"
9
10// A simple override of the ImageButtonCell to disable handling of
11// -mouseEntered.
12@interface NewTabButtonCell : ImageButtonCell
13
14- (void)mouseEntered:(NSEvent*)theEvent;
15
16@end
17
18@implementation NewTabButtonCell
19
20- (void)mouseEntered:(NSEvent*)theEvent {
21  // Ignore this since the NTB enter is handled by the TabStripController.
22}
23
24@end
25
26
27@implementation NewTabButton
28
29+ (Class)cellClass {
30  return [NewTabButtonCell class];
31}
32
33- (BOOL)pointIsOverButton:(NSPoint)point {
34  NSPoint localPoint = [self convertPoint:point fromView:[self superview]];
35  NSRect pointRect = NSMakeRect(localPoint.x, localPoint.y, 1, 1);
36  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
37  NSImage* buttonMask =
38      bundle.GetNativeImageNamed(IDR_NEWTAB_BUTTON_MASK).ToNSImage();
39  NSRect destinationRect = NSMakeRect(
40      (NSWidth(self.bounds) - [buttonMask size].width) / 2,
41      (NSHeight(self.bounds) - [buttonMask size].height) / 2,
42      [buttonMask size].width, [buttonMask size].height);
43  return [buttonMask hitTestRect:pointRect
44        withImageDestinationRect:destinationRect
45                         context:nil
46                           hints:nil
47                         flipped:YES];
48}
49
50// Override to only accept clicks within the bounds of the defined path, not
51// the entire bounding box. |aPoint| is in the superview's coordinate system.
52- (NSView*)hitTest:(NSPoint)aPoint {
53  if ([self pointIsOverButton:aPoint])
54    return [super hitTest:aPoint];
55  return nil;
56}
57
58@end
59