summaryrefslogtreecommitdiffstats
path: root/ui/base/cocoa/hover_image_button.mm
blob: 75eea36a158b1d4746723ee2e239fef12f16ba10 (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
// Copyright (c) 2011 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.

#import "ui/base/cocoa/hover_image_button.h"

@implementation HoverImageButton

@synthesize disableActivationOnClick = disableActivationOnClick_;

- (void)drawRect:(NSRect)rect {
  if (hoverState_ == kHoverStateMouseDown && pressedImage_) {
    [super setImage:pressedImage_.get()];
  } else if (hoverState_ == kHoverStateMouseOver && hoverImage_) {
    [super setImage:hoverImage_.get()];
  } else {
    [super setImage:defaultImage_.get()];
  }

  [super drawRect:rect];
}

- (void)setDefaultImage:(NSImage*)image {
  defaultImage_.reset([image retain]);
}

- (void)setHoverImage:(NSImage*)image {
  hoverImage_.reset([image retain]);
}

- (void)setPressedImage:(NSImage*)image {
  pressedImage_.reset([image retain]);
}

- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)theEvent {
  // To avoid activating the app on a click inside the button, first tell
  // the Appkit not to immediately order the HoverImageButton's window front in
  // response to theEvent.
  return disableActivationOnClick_;
}

- (void)mouseDown:(NSEvent*)mouseDownEvent {
  // If disabling activation on click, tell the Appkit to cancel window ordering
  // for this mouse down.
  if (disableActivationOnClick_) {
    [[NSApplication sharedApplication] preventWindowOrdering];
  }

  [super mouseDown:mouseDownEvent];
}

@end