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.
5 #import "chrome/browser/ui/cocoa/toolbar/toolbar_button.h"
7 @implementation ToolbarButton
9 @synthesize handleMiddleClick = handleMiddleClick_;
11 - (void)otherMouseDown:(NSEvent*)theEvent {
12 if (![self shouldHandleEvent:theEvent]) {
13 [super otherMouseDown:theEvent];
17 NSEvent* nextEvent = theEvent;
20 // Loop until middle button is released. Also, the mouse cursor is outside of
21 // the button, the button should not be highlighted.
23 NSPoint mouseLoc = [self convertPoint:[nextEvent locationInWindow]
25 isInside = [self mouse:mouseLoc inRect:[self bounds]];
26 [self highlight:isInside];
27 [self setState:isInside ? NSOnState : NSOffState];
29 NSUInteger mask = NSOtherMouseDraggedMask | NSOtherMouseUpMask;
30 nextEvent = [[self window] nextEventMatchingMask:mask];
31 } while (!([nextEvent buttonNumber] == 2 &&
32 [nextEvent type] == NSOtherMouseUp));
34 // Discard the events before the middle button up event.
35 // If we don't discard it, the events will be re-processed later.
36 [[self window] discardEventsMatchingMask:NSAnyEventMask
37 beforeEvent:nextEvent];
40 [self setState:NSOffState];
42 [self sendAction:[self action] to:[self target]];
45 - (BOOL)shouldHandleEvent:(NSEvent*)theEvent {
46 // |buttonNumber| is the mouse button whose action triggered theEvent.
47 // 2 corresponds to the middle mouse button.
48 return handleMiddleClick_ && [theEvent buttonNumber] == 2;