Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2010 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 <Cocoa/Cocoa.h>
      6 
      7 // Class for buttons that can be drag sources. If the mouse is clicked and moved
      8 // more than a given distance, this class will call |-beginDrag:| instead of
      9 // |-performClick:|. Subclasses should override these two methods.
     10 @interface DraggableButton : NSButton {
     11  @private
     12   BOOL draggable_;        // Is this a draggable type of button?
     13   BOOL actionHasFired_;   // Has the action already fired for this click?
     14   BOOL actsOnMouseDown_;  // Does button action happen on mouse down when
     15                           // possible?
     16   NSTimeInterval durationMouseWasDown_;
     17   NSTimeInterval whenMouseDown_;
     18 }
     19 
     20 @property NSTimeInterval durationMouseWasDown;
     21 
     22 @property NSTimeInterval whenMouseDown;
     23 
     24 // Whether the action has already fired for this click.
     25 @property(nonatomic) BOOL actionHasFired;
     26 
     27 // Enable or disable dragability for special buttons like "Other Bookmarks".
     28 @property(nonatomic) BOOL draggable;
     29 
     30 // If it has a popup menu, for example, we want to perform the action on mouse
     31 // down, if possible (as long as user still gets chance to drag, if
     32 // appropriate).
     33 @property(nonatomic) BOOL actsOnMouseDown;
     34 
     35 // Called when a drag should start. Subclasses must override this to do any
     36 // pasteboard manipulation and begin the drag, usually with
     37 // -dragImage:at:offset:event:.  Subclasses must call one of the blocking
     38 // -drag* methods of NSView when overriding this method.
     39 - (void)beginDrag:(NSEvent*)dragEvent;
     40 
     41 
     42 // Override if you want to do any extra work on mouseUp, after a mouseDown
     43 // action has already fired.
     44 - (void)secondaryMouseUpAction:(BOOL)wasInside;
     45 
     46 // This is called internally.
     47 // Decides if we now have enough information to stop tracking the mouse.
     48 // It's the function below, deltaIndicatesDragStartWithXDelta. however, that
     49 // decides whether it's a drag or not.
     50 // Override if you want to do something tricky when making the decision.
     51 // Default impl returns YES if ABS(xDelta) or ABS(yDelta) >= their respective
     52 // hysteresis limit.
     53 - (BOOL)deltaIndicatesConclusionReachedWithXDelta:(float)xDelta
     54                                            yDelta:(float)yDelta
     55                                       xHysteresis:(float)xHysteresis
     56                                       yHysteresis:(float)yHysteresis;
     57 
     58 // This is called internally.
     59 // Decides whether we should treat the click as a cue to start dragging, or
     60 // instead call the mouseDown/mouseUp handler as appropriate.
     61 // Override if you want to do something tricky when making the decision.
     62 // Default impl returns YES if ABS(xDelta) or ABS(yDelta) >= their respective
     63 // hysteresis limit.
     64 - (BOOL)deltaIndicatesDragStartWithXDelta:(float)xDelta
     65                                    yDelta:(float)yDelta
     66                               xHysteresis:(float)xHysteresis
     67                               yHysteresis:(float)yHysteresis;
     68 
     69 
     70 @end  // @interface DraggableButton
     71 
     72 @interface DraggableButton (Private)
     73 
     74 // Resets the draggable state of the button after dragging is finished.  This is
     75 // called by DraggableButton when the beginDrag call returns, it should not be
     76 // called by the subclass.
     77 - (void)endDrag;
     78 
     79 // Called internally if the actsOnMouseDown property is set.
     80 // Fires the button's action and tracks the click.
     81 - (void)performMouseDownAction:(NSEvent*)theEvent;
     82 
     83 
     84 @end  // @interface DraggableButton(Private)
     85