Home | History | Annotate | Download | only in toolbar
      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/toolbar/toolbar_button.h"
      6 
      7 @implementation ToolbarButton
      8 
      9 @synthesize handleMiddleClick = handleMiddleClick_;
     10 
     11 - (void)otherMouseDown:(NSEvent*)theEvent {
     12   if (![self shouldHandleEvent:theEvent]) {
     13     [super otherMouseDown:theEvent];
     14     return;
     15   }
     16 
     17   NSEvent* nextEvent = theEvent;
     18   BOOL isInside;
     19 
     20   // Loop until middle button is released. Also, the mouse cursor is outside of
     21   // the button, the button should not be highlighted.
     22   do {
     23     NSPoint mouseLoc = [self convertPoint:[nextEvent locationInWindow]
     24                                  fromView:nil];
     25     isInside = [self mouse:mouseLoc inRect:[self bounds]];
     26     [self highlight:isInside];
     27     [self setState:isInside ? NSOnState : NSOffState];
     28 
     29     NSUInteger mask = NSOtherMouseDraggedMask | NSOtherMouseUpMask;
     30     nextEvent = [[self window] nextEventMatchingMask:mask];
     31   } while (!([nextEvent buttonNumber] == 2 &&
     32              [nextEvent type] == NSOtherMouseUp));
     33 
     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];
     38 
     39   [self highlight:NO];
     40   [self setState:NSOffState];
     41   if (isInside)
     42     [self sendAction:[self action] to:[self target]];
     43 }
     44 
     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;
     49 }
     50 
     51 @end
     52