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 @interface ToolbarButton (Private)
      8 - (BOOL)updateStatus:(NSEvent*)theEvent;
      9 @end
     10 
     11 @implementation ToolbarButton
     12 
     13 @synthesize handleMiddleClick = handleMiddleClick_;
     14 
     15 - (void)mouseDown:(NSEvent*)theEvent {
     16   if (!handlingMiddleClick_)
     17     [super mouseDown:theEvent];
     18 }
     19 
     20 - (void)mouseDragged:(NSEvent*)theEvent {
     21   if (!handlingMiddleClick_)
     22     [super mouseDragged:theEvent];
     23 }
     24 
     25 - (void)mouseUp:(NSEvent*)theEvent {
     26   if (!handlingMiddleClick_)
     27     [super mouseUp:theEvent];
     28 }
     29 
     30 - (void)otherMouseDown:(NSEvent*)theEvent {
     31   if (![self shouldHandleEvent:theEvent])
     32     [super otherMouseDown:theEvent];
     33   else
     34     handlingMiddleClick_ = [self updateStatus:theEvent];
     35 }
     36 
     37 - (void)otherMouseDragged:(NSEvent*)theEvent {
     38   if (!handlingMiddleClick_ || ![self shouldHandleEvent:theEvent])
     39     [super otherMouseDragged:theEvent];
     40   else
     41     [self updateStatus:theEvent];
     42 }
     43 
     44 - (void)otherMouseUp:(NSEvent*)theEvent {
     45   if (!handlingMiddleClick_ || ![self shouldHandleEvent:theEvent]) {
     46     [super otherMouseUp:theEvent];
     47   } else {
     48     if ([self state] == NSOnState)
     49       [self sendAction:[self action] to:[self target]];
     50 
     51     [self setState:NSOffState];
     52     [self highlight:NO];
     53     handlingMiddleClick_ = NO;
     54   }
     55 }
     56 
     57 - (BOOL)updateStatus:(NSEvent*)theEvent {
     58   NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow]
     59                                fromView:nil];
     60   BOOL isInside = [self mouse:mouseLoc inRect:[self bounds]];
     61   [self setState:isInside ? NSOnState : NSOffState];
     62   [self highlight:isInside];
     63   return isInside;
     64 }
     65 
     66 - (BOOL)shouldHandleEvent:(NSEvent*)theEvent {
     67   // |buttonNumber| is the mouse button whose action triggered theEvent.
     68   // 2 corresponds to the middle mouse button.
     69   return handleMiddleClick_ && [theEvent buttonNumber] == 2;
     70 }
     71 
     72 @end
     73